From 4b10e2b7127f3644453a00fe54fec0212614c944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Doeraene?= Date: Sat, 5 Mar 2016 09:51:50 +0100 Subject: [PATCH] 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. --- src/dotty/tools/dotc/transform/ExpandSAMs.scala | 3 ++- tests/run/non-jvm-sam-non-apply.check | 1 + tests/run/non-jvm-sam-non-apply.scala | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/run/non-jvm-sam-non-apply.check create mode 100644 tests/run/non-jvm-sam-non-apply.scala diff --git a/src/dotty/tools/dotc/transform/ExpandSAMs.scala b/src/dotty/tools/dotc/transform/ExpandSAMs.scala index 5de778fabe24..fd556b572fd9 100644 --- a/src/dotty/tools/dotc/transform/ExpandSAMs.scala +++ b/src/dotty/tools/dotc/transform/ExpandSAMs.scala @@ -42,8 +42,9 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer => case tpe @ SAMType(_) if isJvmSam(tpe.classSymbol.asClass) => tree case tpe => + val Seq(samDenot) = tpe.abstractTermMembers.filter(!_.symbol.is(SuperAccessor)) cpy.Block(tree)(stats, - AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, nme.apply :: Nil)) + AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, samDenot.symbol.asTerm.name :: Nil)) } case _ => tree diff --git a/tests/run/non-jvm-sam-non-apply.check b/tests/run/non-jvm-sam-non-apply.check new file mode 100644 index 000000000000..b8626c4cff28 --- /dev/null +++ b/tests/run/non-jvm-sam-non-apply.check @@ -0,0 +1 @@ +4 diff --git a/tests/run/non-jvm-sam-non-apply.scala b/tests/run/non-jvm-sam-non-apply.scala new file mode 100644 index 000000000000..e3bfac9283d5 --- /dev/null +++ b/tests/run/non-jvm-sam-non-apply.scala @@ -0,0 +1,11 @@ +// shouldn't result in an abstract method error when run +abstract class NonJVMSam { + def foo(x: Int): Int +} + +object Test { + def main(args: Array[String]): Unit = { + val f: NonJVMSam = x => x + 1 + println(f.foo(3)) + } +}