Skip to content

Commit c14c9c0

Browse files
committed
Move the logic of ExpandSAMs.isJvmSam to Platform.isSam.
Whether a language SAM type is also a valid SAM type for the back-end is a platform-specific thing. On Scala.js, for example, the rules are completely different than for the JVM. This commit therefore moves the logic of the predicate used by ExpandSAMs to decide whether to expand a SAM as an anonymous class to the Platform.
1 parent a509267 commit c14c9c0

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/dotty/tools/dotc/config/JavaPlatform.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ClassPath.{ JavaContext, DefaultJavaContext }
77
import core._
88
import Symbols._, Types._, Contexts._, Denotations._, SymDenotations._, StdNames._, Names._
99
import Flags._, Scopes._, Decorators._, NameOps._, util.Positions._
10+
import transform.ExplicitOuter, transform.SymUtils._
1011

1112
class JavaPlatform extends Platform {
1213

@@ -38,6 +39,14 @@ class JavaPlatform extends Platform {
3839

3940
def rootLoader(root: TermSymbol)(implicit ctx: Context): SymbolLoader = new ctx.base.loaders.PackageLoader(root, classPath)
4041

42+
/** Is the SAMType `cls` also a SAM under the rules of the JVM? */
43+
def isSam(cls: ClassSymbol)(implicit ctx: Context): Boolean =
44+
cls.is(NoInitsTrait) &&
45+
cls.superClass == defn.ObjectClass &&
46+
cls.directlyInheritedTraits.forall(_.is(NoInits)) &&
47+
!ExplicitOuter.needsOuterIfReferenced(cls) &&
48+
cls.typeRef.fields.isEmpty // Superaccessors already show up as abstract methods here, so no test necessary
49+
4150
/** We could get away with excluding BoxedBooleanClass for the
4251
* purpose of equality testing since it need not compare equal
4352
* to anything but other booleans, but it should be present in

src/dotty/tools/dotc/config/Platform.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ abstract class Platform {
2727
/** Any platform-specific phases. */
2828
//def platformPhases: List[SubComponent]
2929

30+
/** Is the SAMType `cls` also a SAM under the rules of the platform? */
31+
def isSam(cls: ClassSymbol)(implicit ctx: Context): Boolean
32+
3033
/** The various ways a boxed primitive might materialize at runtime. */
3134
def isMaybeBoxed(sym: ClassSymbol)(implicit ctx: Context): Boolean
3235

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,17 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
2525

2626
import ast.tpd._
2727

28-
/** Is SAMType `cls` also a SAM under the rules of the JVM? */
29-
def isJvmSam(cls: ClassSymbol)(implicit ctx: Context): Boolean =
30-
cls.is(NoInitsTrait) &&
31-
cls.superClass == defn.ObjectClass &&
32-
cls.directlyInheritedTraits.forall(_.is(NoInits)) &&
33-
!ExplicitOuter.needsOuterIfReferenced(cls) &&
34-
cls.typeRef.fields.isEmpty // Superaccessors already show up as abstract methods here, so no test necessary
28+
/** Is the SAMType `cls` also a SAM under the rules of the platform? */
29+
def isPlatformSam(cls: ClassSymbol)(implicit ctx: Context): Boolean =
30+
ctx.platform.isSam(cls)
3531

3632
override def transformBlock(tree: Block)(implicit ctx: Context, info: TransformerInfo): Tree = tree match {
3733
case Block(stats @ (fn: DefDef) :: Nil, Closure(_, fnRef, tpt)) if fnRef.symbol == fn.symbol =>
3834
tpt.tpe match {
3935
case NoType => tree // it's a plain function
4036
case tpe @ SAMType(_) if tpe.isRef(defn.PartialFunctionClass) =>
4137
toPartialFunction(tree)
42-
case tpe @ SAMType(_) if isJvmSam(tpe.classSymbol.asClass) =>
38+
case tpe @ SAMType(_) if isPlatformSam(tpe.classSymbol.asClass) =>
4339
tree
4440
case tpe =>
4541
cpy.Block(tree)(stats,

0 commit comments

Comments
 (0)