Skip to content

Commit 9e7f1e0

Browse files
committed
Fix a recursive TypeBound with some half answers
1 parent 7939ddb commit 9e7f1e0

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ object TypeOps:
423423
sym.is(Package) || sym.isStatic && isStaticPrefix(pre.prefix)
424424
case _ => true
425425

426+
private var halfAnswers: util.SimpleIdentityMap[Type, Type] = util.SimpleIdentityMap.empty
427+
426428
override def apply(tp: Type): Type = tp match
427429
case tp: TermRef
428430
if toAvoid(tp) =>
@@ -434,8 +436,14 @@ object TypeOps:
434436
tp.info match {
435437
case info: AliasingBounds =>
436438
apply(info.alias)
439+
case TypeBounds(lo, hi) if halfAnswers.contains(tp) =>
440+
halfAnswers(tp)
437441
case TypeBounds(lo, hi) =>
438-
range(atVariance(-variance)(apply(lo)), apply(hi))
442+
val hi2 = apply(hi)
443+
halfAnswers = halfAnswers.updated(tp, range(defn.NothingType, hi2))
444+
val res = range(atVariance(-variance)(apply(lo)), hi2)
445+
halfAnswers = halfAnswers.remove(tp)
446+
res
439447
case info: ClassInfo =>
440448
range(defn.NothingType, apply(classBound(info)))
441449
case _ =>

tests/pos/i14287.min.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum Foo[+H[_]]:
2+
case Bar[F[_]](f: Foo[F]) extends Foo[F]
3+
case Baz()
4+
5+
def test: Foo[H] = this match
6+
case Bar(Bar(f)) => Bar(f)
7+
case _ => this

tests/pos/i14287.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enum Free[+F[_], A]:
2+
case Return(a: A)
3+
case Suspend(s: F[A])
4+
case FlatMap[F[_], A, B](
5+
s: Free[F, A],
6+
f: A => Free[F, B]) extends Free[F, B]
7+
8+
def flatMap[F2[x] >: F[x], B](f: A => Free[F2,B]): Free[F2,B] =
9+
FlatMap(this, f)
10+
11+
@annotation.tailrec
12+
final def step: Free[F, A] = this match
13+
case FlatMap(FlatMap(fx, f), g) => fx.flatMap(x => f(x).flatMap(y => g(y))).step
14+
case FlatMap(Return(x), f) => f(x).step
15+
case _ => this

0 commit comments

Comments
 (0)