Skip to content

Commit c395a25

Browse files
committed
Generalize tpd.AnonClass
Now takes a list of parent types. We needed only one parent for SAM implementation but it makes sense to generalize this. Also, removed redundant code accidentally left in.
1 parent 464ef81 commit c395a25

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,21 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
247247

248248
/** An anonymous class
249249
*
250-
* new parent { forwarders }
250+
* new parents { forwarders }
251251
*
252252
* where `forwarders` contains forwarders for all functions in `fns`.
253-
* `fns` must be non-empty. The class has the same owner as the first function in `fns`.
253+
* @param parents a non-empty list of class types
254+
* @param fns a non-empty of functions for which forwarders should be defined in the class.
255+
* The class has the same owner as the first function in `fns`.
254256
* Its position is the union of all functions in `fns`.
255257
*/
256-
def AnonClass(parent: Type, fns: List[TermSymbol], methNames: List[TermName])(implicit ctx: Context): Block = {
257-
def methName(fnName: TermName): TermName = if (fnName == nme.ANON_FUN) nme.apply else fnName
258+
def AnonClass(parents: List[Type], fns: List[TermSymbol], methNames: List[TermName])(implicit ctx: Context): Block = {
258259
val owner = fns.head.owner
259-
val parents =
260-
if (parent.classSymbol.is(Trait)) defn.ObjectClass.typeRef :: parent :: Nil
261-
else parent :: Nil
262-
val cls = ctx.newNormalizedClassSymbol(owner, tpnme.ANON_FUN, Synthetic, parents,
260+
val parents1 =
261+
if (parents.head.classSymbol.is(Trait)) defn.ObjectClass.typeRef :: parents
262+
else parents
263+
val cls = ctx.newNormalizedClassSymbol(owner, tpnme.ANON_FUN, Synthetic, parents1,
263264
coord = fns.map(_.pos).reduceLeft(_ union _))
264-
println(i"creating anon class with parent $parent -> ${cls.info.parents}%, %")
265-
println(cls.classInfo.classParents)
266265
val constr = ctx.newConstructor(cls, Synthetic, Nil, Nil).entered
267266
def forwarder(fn: TermSymbol, name: TermName) = {
268267
val fwdMeth = fn.copy(cls, name, Synthetic | Method).entered.asTerm

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
3333
else tree
3434
case tpe =>
3535
cpy.Block(tree)(stats,
36-
AnonClass(tpe, fn.symbol.asTerm :: Nil, nme.apply :: Nil))
36+
AnonClass(tpe :: Nil, fn.symbol.asTerm :: Nil, nme.apply :: Nil))
3737
}
3838
case _ =>
3939
tree
@@ -72,7 +72,7 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
7272
tru
7373
}
7474
val isDefinedAtDef = transformFollowingDeep(DefDef(isDefinedAtFn, isDefinedAtRhs(_)))
75-
val anonCls = AnonClass(tpt.tpe, List(applyFn, isDefinedAtFn), List(nme.apply, nme.isDefinedAt))
75+
val anonCls = AnonClass(tpt.tpe :: Nil, List(applyFn, isDefinedAtFn), List(nme.apply, nme.isDefinedAt))
7676
cpy.Block(tree)(List(applyDef, isDefinedAtDef), anonCls)
7777
}
7878
}

0 commit comments

Comments
 (0)