Skip to content

Commit b4aa178

Browse files
committed
Fix #1146: Fix bug of ExpandSAMs with non-apply SAM methods.
When expanding a SAM, ExpandSAMs always used the name `apply` for the generated forwarder, instead of the name of the method which is abstract in the SAM type. This commit fixes this issue.
1 parent a509267 commit b4aa178

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
4242
case tpe @ SAMType(_) if isJvmSam(tpe.classSymbol.asClass) =>
4343
tree
4444
case tpe =>
45+
val abstractMethods = tpe.abstractTermMembers.filter(!_.symbol.is(SuperAccessor))
46+
assert(abstractMethods.size == 1)
47+
val samDenot = abstractMethods.head
4548
cpy.Block(tree)(stats,
46-
AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, nme.apply :: Nil))
49+
AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, samDenot.symbol.asTerm.name :: Nil))
4750
}
4851
case _ =>
4952
tree

tests/run/non-jvm-sam-non-apply.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

tests/run/non-jvm-sam-non-apply.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// shouldn't result in an abstract method error when run
2+
abstract class NonJVMSam {
3+
def foo(x: Int): Int
4+
}
5+
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
val f: NonJVMSam = x => x + 1
9+
println(f.foo(3))
10+
}
11+
}

0 commit comments

Comments
 (0)