Skip to content

Commit 2201e3d

Browse files
committed
Only track the most specific case and report an error directly, otherwise
1 parent 77ddadf commit 2201e3d

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -791,37 +791,29 @@ object RefChecks {
791791
val seenParents = mutable.HashSet[Type]()
792792
val baseClasses: List[ClassSymbol] = clazz.info.baseClasses
793793

794-
// tracks types that we have seen for a particular base class in baseClasses
795-
val seenTypes = MutableSymbolMap[List[Type]](baseClasses.size)
794+
// tracks the most specific type that we have seen for a particular base class
795+
val seenTypes = MutableSymbolMap[Type](baseClasses.size)
796796

797797
// validate all base types of a class in reverse linear order.
798798
def register(tp: Type): Unit = {
799799
val baseClass = tp.classSymbol
800800
if (baseClasses contains baseClass) {
801-
val alreadySeen = seenTypes.getOrElse(baseClass, Nil)
802-
if (alreadySeen.forall(tp1 => !(tp1 <:< tp)))
803-
seenTypes.update(baseClass, tp :: alreadySeen.filter(tp1 => !(tp <:< tp1)))
801+
seenTypes.get(baseClass) match {
802+
case Some(alreadySeen) if !(tp <:< alreadySeen) =>
803+
val msg =
804+
em"""illegal inheritance;
805+
|
806+
| $clazz inherits different type instances of $baseClass:
807+
| $tp and $alreadySeen"""
808+
report.error(msg, clazz.srcPos)
809+
case _ => seenTypes.update(baseClass, tp)
810+
}
804811
}
805812
val remaining = tp.parents filterNot seenParents
806813
seenParents ++= remaining
807814
remaining foreach register
808815
}
809816
register(tpe)
810-
811-
seenTypes.iterator.foreach {
812-
case (cls, Nil) =>
813-
assert(false) // this case should not be reachable
814-
case (cls, _ :: Nil) =>
815-
() // Ok
816-
case (cls, tp1 :: tp2 :: _) =>
817-
val msg =
818-
em"""illegal inheritance;
819-
|
820-
| $clazz inherits different type instances of $cls:
821-
| $tp1 and $tp2"""
822-
823-
report.error(msg, clazz.srcPos)
824-
}
825817
}
826818

827819
checkParameterizedTraitsOK()

0 commit comments

Comments
 (0)