Skip to content

Commit 21c0cc9

Browse files
committed
Narrow Java exception to inheritance rule
Excepted are only Serializable and Comparable. This follows scalac's behavior.
1 parent 96419b4 commit 21c0cc9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,9 @@ trait Checking {
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`.
606606
*
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
607+
* An exception is made if `cls` extends `Any`, and `parent` is `java.io.Serializable`
608+
* or `java.lang.Comparable`. These two classes are treated by Scala as universal
609+
* traits. E.g. the following is OK:
609610
*
610611
* ... extends Any with java.io.Serializable
611612
*
@@ -617,7 +618,8 @@ trait Checking {
617618
val psuper = parent.superClass
618619
val csuper = cls.superClass
619620
val ok = csuper.derivesFrom(psuper) ||
620-
parent.is(JavaDefined) && csuper == defn.AnyClass && psuper == defn.ObjectClass
621+
parent.is(JavaDefined) && csuper == defn.AnyClass &&
622+
(parent == defn.JavaSerializableClass || parent == defn.ComparableClass)
621623
if (!ok)
622624
ctx.error(em"illegal trait inheritance: super$csuper does not derive from $parent's super$psuper", pos)
623625
case _ =>

0 commit comments

Comments
 (0)