Skip to content

Commit 3224f33

Browse files
committed
Fix parent class for anonymous class
`Any` can be parent class of traits, in that case we should choose `AnyRef`.
1 parent 0f50d77 commit 3224f33

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
308308
def AnonClass(parents: List[Type], fns: List[TermSymbol], methNames: List[TermName])(implicit ctx: Context): Block = {
309309
val owner = fns.head.owner
310310
val parents1 =
311-
if (parents.head.classSymbol.is(Trait)) parents.head.parents.head :: parents
311+
if (parents.head.classSymbol.is(Trait)) {
312+
val head = parents.head.parents.head
313+
if (head.isRef(defn.AnyClass)) defn.AnyRefType :: parents else head :: parents
314+
}
312315
else parents
313316
val cls = ctx.newNormalizedClassSymbol(owner, tpnme.ANON_CLASS, Synthetic | Final, parents1,
314317
coord = fns.map(_.span).reduceLeft(_ union _))

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ class Typer extends Namer
515515
case templ: untpd.Template =>
516516
import untpd._
517517
var templ1 = templ
518-
def isEligible(tp: Type) = tp.exists && !tp.typeSymbol.is(Final) && tp.classSymbol != defn.AnyClass
518+
def isEligible(tp: Type) = tp.exists && !tp.typeSymbol.is(Final) && !tp.isRef(defn.AnyClass)
519519
if (templ1.parents.isEmpty &&
520520
isFullyDefined(pt, ForceDegree.noBottom) &&
521521
isEligible(pt.underlyingClassRef(refinementOK = false)))

0 commit comments

Comments
 (0)