Skip to content

Commit 74c693c

Browse files
committed
Try to detecting cycles at instantiation time
1 parent 0181ef9 commit 74c693c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4751,6 +4751,9 @@ object Types {
47514751
assert(!myInst.exists, i"$origin is already instantiated to $myInst but we attempted to instantiate it to $tp")
47524752
typr.println(i"instantiating $this with $tp")
47534753

4754+
if origin.occursIn(tp) then
4755+
throw new TypeError(i"cyclic instantiation of $origin to $tp")
4756+
47544757
if Config.checkConstraintsSatisfiable then
47554758
assert(currentEntry.bounds.contains(tp),
47564759
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")

tests/neg/indirect-cycle.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class LT[-X, +Y] // error: cyclic instantiation of T to D[D[T]]
2+
object LT {
3+
given lt[X <: Y, Y]: LT[X, Y] = ???
4+
}
5+
6+
trait C[X]
7+
trait D[X] extends C[X]
8+
trait A { // error: cyclic instantiation of T to D[D[T]]
9+
def foo[S <: C[T], T <: C[S]](using LT[D[T], S], LT[D[S], T]): S = ???
10+
11+
foo
12+
}

0 commit comments

Comments
 (0)