Skip to content

Commit 0176104

Browse files
committed
Refine checkTraitInheritance condition
Need to take account of situations like extends Any with java.io.Serializable which occur in stdlib.
1 parent cadba16 commit 0176104

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,12 +603,25 @@ trait Checking {
603603

604604
/** Given a parent `parent` of a class `cls`, if `parent` is a trait check that
605605
* the superclass of `cls` derived from the superclass of `parent`.
606+
*
607+
* An exception is made if `cls` extends `Any`, and `parent` is a Java class
608+
* that extends `Object`. For instance, we accept code like
609+
*
610+
* ... extends Any with java.io.Serializable
611+
*
612+
* The standard library relies on this idiom.
606613
*/
607-
def checkTraitInheritance(parent: Symbol, cls: ClassSymbol, pos: Position)(implicit ctx: Context): Unit =
614+
def checkTraitInheritance(parent: Symbol, cls: ClassSymbol, pos: Position)(implicit ctx: Context): Unit = {
608615
parent match {
609-
case parent: ClassSymbol if parent.is(Trait) && !cls.superClass.derivesFrom(parent.superClass) =>
610-
ctx.error(em"illegal trait inheritance: super${cls.superClass} does not derive from $parent's super${parent.superClass}", pos)
616+
case parent: ClassSymbol if parent is Trait =>
617+
val psuper = parent.superClass
618+
val csuper = cls.superClass
619+
val ok = csuper.derivesFrom(psuper) ||
620+
parent.is(JavaDefined) && csuper == defn.AnyClass && psuper == defn.ObjectClass
621+
if (!ok)
622+
ctx.error(em"illegal trait inheritance: super$csuper does not derive from $parent's super$psuper", pos)
611623
case _ =>
624+
}
612625
}
613626
}
614627

0 commit comments

Comments
 (0)