Skip to content

Commit 5ec0977

Browse files
authored
Merge pull request #14851 from dotty-staging/fix-2887
Guard against stackoverflows when instantiating HK-Lambdas
2 parents cc7ae5e + f84ed9d commit 5ec0977

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,15 @@ class TypeApplications(val self: Type) extends AnyVal {
343343
}
344344
}
345345
if ((dealiased eq stripped) || followAlias)
346-
try {
346+
try
347347
val instantiated = dealiased.instantiate(args)
348348
if (followAlias) instantiated.normalized else instantiated
349-
}
350-
catch { case ex: IndexOutOfBoundsException => AppliedType(self, args) }
349+
catch
350+
case ex: IndexOutOfBoundsException =>
351+
AppliedType(self, args)
352+
case ex: Throwable =>
353+
handleRecursive("try to instantiate", i"$dealiased[$args%, %]", ex)
354+
351355
else AppliedType(self, args)
352356
}
353357
else dealiased.resType match {

tests/neg/i2887b.check

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- Error: tests/neg/i2887b.scala:11:7 ----------------------------------------------------------------------------------
2+
11 | baz(new A { type S[X[_] <: [_] =>> Any, Y[_]] = [Z] =>> X[Z][Y[Z]]; type I[X] = X })(1) // error
3+
| ^
4+
| Recursion limit exceeded.
5+
| Maybe there is an illegal cyclic reference?
6+
| If that's not the case, you could also try to increase the stacksize using the -Xss JVM option.
7+
| A recurring operation is (inner to outer):
8+
|
9+
| try to instantiate Z[Z]
10+
| try to instantiate Z[Z]
11+
| try to instantiate Z[Z]
12+
| try to instantiate Z[Z]
13+
| try to instantiate Z[Z]
14+
| try to instantiate Z[Z]
15+
| try to instantiate Z[Z]
16+
| try to instantiate Z[Z]
17+
| try to instantiate Z[Z]
18+
| try to instantiate Z[Z]
19+
| ...
20+
|
21+
| try to instantiate Z[Z]
22+
| try to instantiate Z[Z]
23+
| try to instantiate Z[Z]
24+
| try to instantiate Z[Z]
25+
| try to instantiate Z[Z]
26+
| try to instantiate Z[Z]
27+
| try to instantiate Z[Z]
28+
| try to instantiate Z[Z]
29+
| try to instantiate Z[Z]
30+
| try to instantiate Z[Z]

tests/neg/i2887b.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
trait A { type S[X[_] <: [_] => Any, Y[_]] <: [_] => Any; type I[_] } // error // error
2-
trait B { type S[X[_],Y[_]]; type I[_] <: [_] => Any } // error
1+
trait A { type S[X[_] <: [_] =>> Any, Y[_]] <: [_] =>> Any; type I[_] }
2+
trait B { type S[X[_],Y[_]]; type I[_] <: [_] =>> Any }
33
trait C { type M <: B }
44
trait D { type M >: A }
55

66
object Test {
77
def test(x: C with D): Unit = {
8-
def foo(a: A, b: B)(z: a.S[b.I,a.I][b.S[a.I,a.I]]) = z // error
8+
def foo(a: A, b: B)(z: a.S[b.I,a.I][b.S[a.I,a.I]]) = z
99
def bar(a: A, y: x.M) = foo(a,y)
1010
def baz(a: A) = bar(a, a)
11-
baz(new A { type S[X[_] <: [_] => Any, Y[_]] = [Z] => X[Z][Y[Z]]; type I[X] = X })(1) // error // error
11+
baz(new A { type S[X[_] <: [_] =>> Any, Y[_]] = [Z] =>> X[Z][Y[Z]]; type I[X] = X })(1) // error
1212
}
13-
}
13+
}

0 commit comments

Comments
 (0)