Skip to content

Commit 0e6aa7e

Browse files
authored
Merge pull request #9001 from dotty-staging/fix-toplevel-matchtypes-cyclic
Fix #9000: Avoid spurious cyclic errors for toplevel matches
2 parents db4be22 + 1e30c39 commit 0e6aa7e

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Decorators._
1010
import util.Stats._
1111
import Names._
1212
import NameOps._
13+
import Flags.Module
1314
import Variances.variancesConform
1415
import dotty.tools.dotc.config.Config
1516

@@ -145,7 +146,11 @@ class TypeApplications(val self: Type) extends AnyVal {
145146
final def typeParams(implicit ctx: Context): List[TypeParamInfo] = {
146147
record("typeParams")
147148
def isTrivial(prefix: Type, tycon: Symbol) = prefix match {
148-
case prefix: ThisType => prefix.cls `eq` tycon.owner
149+
case prefix: ThisType =>
150+
prefix.cls eq tycon.owner
151+
case prefix: TermRef =>
152+
val sym = prefix.symbol
153+
sym.is(Module) && sym.isStatic && (sym.moduleClass eq tycon.owner)
149154
case NoPrefix => true
150155
case _ => false
151156
}

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ class CyclicReference private (val denot: SymDenotation) extends TypeError {
154154
object CyclicReference {
155155
def apply(denot: SymDenotation)(implicit ctx: Context): CyclicReference = {
156156
val ex = new CyclicReference(denot)
157-
if (!(ctx.mode is Mode.CheckCyclic)) {
158-
cyclicErrors.println(s"Cyclic reference involving $denot")
157+
if (!(ctx.mode is Mode.CheckCyclic) || ctx.settings.Ydebug.value) {
158+
cyclicErrors.println(s"Cyclic reference involving! $denot")
159159
for (elem <- ex.getStackTrace take 200)
160160
cyclicErrors.println(elem.toString)
161161
}

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,11 @@ object messages {
22382238

22392239
class IllegalCyclicTypeReference(sym: Symbol, where: String, lastChecked: Type)(using ctx: Context)
22402240
extends CyclicMsg(IllegalCyclicTypeReferenceID) {
2241-
def msg = i"illegal cyclic type reference: ${where} ${hl(lastChecked.show)} of $sym refers back to the type itself"
2241+
def msg =
2242+
val lastCheckedStr =
2243+
try lastChecked.show
2244+
catch case ex: CyclicReference => "..."
2245+
i"illegal cyclic type reference: ${where} ${hl(lastCheckedStr)} of $sym refers back to the type itself"
22422246
def explain = ""
22432247
}
22442248

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
type A = B // error: recursion limit exceeded
2-
1+
type A = B

tests/pos/toplevel-match.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class A
2+
class B
3+
4+
trait HList
5+
class HCons[A, As <: HList] extends HList
6+
class HNil[A] extends HList
7+
8+
type AtoB[Xs <: HList] <: HList = Xs match
9+
case HNil[a] => HNil[B]
10+
case HCons[a, as] => HCons[B, AtoB[as]]
11+
12+
//type A2B[Xs <: Tuple] <: Tuple = Xs match
13+
// case Unit => Unit
14+
// case a *: as => B *: A2B[as]

0 commit comments

Comments
 (0)