Skip to content

Commit 70b5514

Browse files
committed
Fix scala#1750: Handle illegal class overrides better
Illegal class overrides are fundamentally at odds with the way dotty represents types and therefore can cause lots of low-level problems. Two measures in this commit First, we detect direct illegal class overrides on completion instead of during RefChecks. Break the override by making the previously overriding type private. This fixes i1750.scala, but still fails for indirect overrides between two unrelated outer traits/classes that are inherited by the same class or trait. We fix this by catching the previously thrown ClassCastException in both ExtractAPI and RefChecks. Test case for indirect overrides is in i1750a.scala.
1 parent da8da4d commit 70b5514

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import core._, core.Decorators._
66
import Annotations._, Contexts._, Flags._, Phases._, Trees._, Types._, Symbols._
77
import Names._, NameOps._, StdNames._
88
import typer.Inliner
9+
import typer.ErrorReporting.cyclicErrorMsg
910

1011
import dotty.tools.io.Path
1112
import java.io.PrintWriter
@@ -190,7 +191,16 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
190191
def apiClassStructure(csym: ClassSymbol): api.Structure = {
191192
val cinfo = csym.classInfo
192193

193-
val bases = linearizedAncestorTypes(cinfo)
194+
val bases =
195+
try linearizedAncestorTypes(cinfo)
196+
catch {
197+
case ex: CyclicReference =>
198+
// See neg/i1750a for an example where a cyclic error can arise.
199+
// The root cause in this example is an illegal "override" of an inner trait
200+
ctx.error(cyclicErrorMsg(ex), csym.pos)
201+
defn.ObjectType :: Nil
202+
}
203+
194204
val apiBases = bases.map(apiType)
195205

196206
// Synthetic methods that are always present do not affect the API

tests/neg/overrides.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ package p2 { // all being in the same package compiles fine
3434
}
3535
}
3636

37-
abstract class T3 extends T2 {
38-
class A { // error: classes cannot be overridden
39-
bug()
40-
}
41-
}
4237
}
4338

4439
class A[T] {

tests/neg/t7278.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class A { class E }
2-
class B extends A { class E }
2+
class B extends A { class EB }
33
trait C { type E = Int }
44
trait D { type E = String }
55
trait EC { type E }

0 commit comments

Comments
 (0)