From a17d247a8acea2daaddb39263ebf1dcf5673bcba Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Fri, 25 Mar 2016 18:41:48 -0700 Subject: SAM conversion can be disabled using `-Xsource:2.11` For completeness, `-Xsource:2.11 -Xexperimental` does enable it. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 2 ++ src/reflect/scala/reflect/internal/Definitions.scala | 4 +++- src/reflect/scala/reflect/internal/settings/MutableSettings.scala | 1 + src/reflect/scala/reflect/runtime/Settings.scala | 1 + test/files/neg/sammy_disabled.check | 4 ++++ test/files/neg/sammy_disabled.flags | 1 + test/files/neg/sammy_disabled.scala | 3 +++ 7 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/sammy_disabled.check create mode 100644 test/files/neg/sammy_disabled.flags create mode 100644 test/files/neg/sammy_disabled.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index ba6a9e20ea..35cfc644ab 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2752,6 +2752,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper def inferSamType(fun: Tree, pt: Type, mode: Mode): SAMFunction = { val sam = if (fun.isInstanceOf[Function] && !isFunctionType(pt)) { + // TODO: can we ensure there's always a SAMFunction attachment, instead of looking up the sam again??? + // seems like overloading complicates things? val sam = samOf(pt) if (samMatchesFunctionBasedOnArity(sam, fun.asInstanceOf[Function].vparams)) sam else NoSymbol diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index 28f6afee39..72c56def80 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -829,13 +829,15 @@ trait Definitions extends api.StandardDefinitions { (sym eq PartialFunctionClass) || (sym eq AbstractPartialFunctionClass) } + private[this] val doSam = settings.isScala212 || (settings.isScala211 && settings.Xexperimental) + /** The single abstract method declared by type `tp` (or `NoSymbol` if it cannot be found). * * The method must be monomorphic and have exactly one parameter list. * The class defining the method is a supertype of `tp` that * has a public no-arg primary constructor. */ - def samOf(tp: Type): Symbol = { + def samOf(tp: Type): Symbol = if (!doSam) NoSymbol else { // look at erased type because we (only) care about what ends up in bytecode // (e.g., an alias type or intersection type is fine as long as the intersection dominator compiles to an interface) val tpSym = erasure.javaErasure(tp).typeSymbol diff --git a/src/reflect/scala/reflect/internal/settings/MutableSettings.scala b/src/reflect/scala/reflect/internal/settings/MutableSettings.scala index 38893d8db3..e75b3dff3d 100644 --- a/src/reflect/scala/reflect/internal/settings/MutableSettings.scala +++ b/src/reflect/scala/reflect/internal/settings/MutableSettings.scala @@ -58,6 +58,7 @@ abstract class MutableSettings extends AbsSettings { def maxClassfileName: IntSetting def isScala211: Boolean + def isScala212: Boolean } object MutableSettings { diff --git a/src/reflect/scala/reflect/runtime/Settings.scala b/src/reflect/scala/reflect/runtime/Settings.scala index 27d574b1de..b1d7fde1b4 100644 --- a/src/reflect/scala/reflect/runtime/Settings.scala +++ b/src/reflect/scala/reflect/runtime/Settings.scala @@ -51,4 +51,5 @@ private[reflect] class Settings extends MutableSettings { val Yrecursion = new IntSetting(0) val maxClassfileName = new IntSetting(255) def isScala211 = true + def isScala212 = true } diff --git a/test/files/neg/sammy_disabled.check b/test/files/neg/sammy_disabled.check new file mode 100644 index 0000000000..66db9dd5f2 --- /dev/null +++ b/test/files/neg/sammy_disabled.check @@ -0,0 +1,4 @@ +sammy_disabled.scala:3: error: missing parameter type +class C { val f: F = x => "a" } + ^ +one error found diff --git a/test/files/neg/sammy_disabled.flags b/test/files/neg/sammy_disabled.flags new file mode 100644 index 0000000000..cf42e9f940 --- /dev/null +++ b/test/files/neg/sammy_disabled.flags @@ -0,0 +1 @@ +-Xsource:2.11 diff --git a/test/files/neg/sammy_disabled.scala b/test/files/neg/sammy_disabled.scala new file mode 100644 index 0000000000..12000a3e12 --- /dev/null +++ b/test/files/neg/sammy_disabled.scala @@ -0,0 +1,3 @@ +trait F { def apply(x: Int): String } + +class C { val f: F = x => "a" } -- cgit v1.2.3