Skip to content

Fix error when adding refined terms to SAMs #2542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ class Definitions {
val cls = denot.asClass.classSymbol
val decls = newScope
val arity = name.functionArity
val paramNamePrefix = tpnme.scala_ ++ str.NAME_JOIN ++ name ++ str.EXPAND_SEPARATOR
val argParams =
for (i <- List.range(0, arity)) yield
enterTypeParam(cls, name ++ "$T" ++ i.toString, Contravariant, decls)
val resParam = enterTypeParam(cls, name ++ "$R", Covariant, decls)
for (i <- List.range(1, arity + 1)) yield
enterTypeParam(cls, paramNamePrefix ++ "T" ++ i.toString, Contravariant, decls)
val resParam = enterTypeParam(cls, paramNamePrefix ++ "R", Covariant, decls)
val (methodType, parentTraits) =
if (name.firstPart.startsWith(str.ImplicitFunction)) {
val superTrait =
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TreeTransforms._
import SymUtils._
import ast.untpd
import ast.Trees._
import dotty.tools.dotc.util.Positions.Position

/** Expand SAM closures that cannot be represented by the JVM as lambdas to anonymous classes.
* These fall into five categories
Expand All @@ -34,10 +35,13 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
tpt.tpe match {
case NoType => tree // it's a plain function
case tpe @ SAMType(_) if tpe.isRef(defn.PartialFunctionClass) =>
checkRefinements(tpe, fn.pos)
toPartialFunction(tree)
case tpe @ SAMType(_) if isPlatformSam(tpe.classSymbol.asClass) =>
checkRefinements(tpe, fn.pos)
tree
case tpe =>
checkRefinements(tpe, fn.pos)
val Seq(samDenot) = tpe.abstractTermMembers.filter(!_.symbol.isSuperAccessor)
cpy.Block(tree)(stats,
AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, samDenot.symbol.asTerm.name :: Nil))
Expand Down Expand Up @@ -83,4 +87,13 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
val anonCls = AnonClass(tpt.tpe :: Nil, List(applyFn, isDefinedAtFn), List(nme.apply, nme.isDefinedAt))
cpy.Block(tree)(List(applyDef, isDefinedAtDef), anonCls)
}

private def checkRefinements(tpe: Type, pos: Position)(implicit ctx: Context): Unit = tpe match {
case RefinedType(parent, name, _) =>
if (name.isTermName && tpe.member(name).symbol.ownersIterator.isEmpty) // if member defined in the refinement
ctx.error("Lambda does not define " + name, pos)
checkRefinements(parent, pos)
case _ =>
}

}
17 changes: 17 additions & 0 deletions tests/neg/i2539.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object Foo {
val f0: Function0[Int]{ def foo(): Int } = () => 42 // error

val f1: Function1[Int, Int]{ def foo(): Int } = x1 => 42 // error

val f26: Function26[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]{ def foo(): Int } =
(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26) => 42 // error

val if1: ImplicitFunction1[Int, Int]{ def foo(): Int } = implicit x1 => 42 // error

abstract class Fun0[X] extends Function0[X]
val fun0a: Fun0[Int] = () => 42
val fun0b: Fun0[Int] { def foo(): Int } = () => 42 // error

val pf: PartialFunction[Int, Int]{ def foo(): Int } = { case x1 => 42 } // error

}