Skip to content

Commit e7a2ea2

Browse files
committed
Run checkNonCyclic on GADT symbol bounds
1 parent 97641d3 commit e7a2ea2

File tree

7 files changed

+113
-4
lines changed

7 files changed

+113
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ object Checking {
280280
case tp @ MatchAlias(alias) =>
281281
tp.derivedAlias(checkUpper(alias, "match"))
282282
case tp @ TypeBounds(lo, hi) =>
283-
tp.derivedTypeBounds(checkPart(lo, "lower bound"), checkUpper(hi, "upper bound"))
283+
val lo1 = checkPart(lo, "lower bound")
284+
val hi1 = checkUpper(hi, "upper bound")
285+
if lo1.exists && hi1.exists then tp.derivedTypeBounds(lo1, hi1)
286+
else NoType
284287
case _ =>
285288
tp
286289
}
@@ -336,9 +339,23 @@ object Checking {
336339
if (locked.contains(tp) || tp.symbol.infoOrCompleter.isInstanceOf[NoCompleter])
337340
throw CyclicReference(tp.symbol)
338341
locked += tp
339-
try if (!tp.symbol.isClass) checkInfo(tp.info)
342+
try if tp.symbol.isClass then
343+
tp.withPrefix(pre1)
344+
else
345+
val info1 = tp.info
346+
var bail = false
347+
val info2 =
348+
try checkInfo(info1)
349+
catch case ex: CyclicReference if ex.denot.symbol != sym =>
350+
bail = true
351+
NoType
352+
if bail then
353+
NoType
354+
else if info1.exists && !info2.exists then
355+
if nestedCycleOK then defn.AnyType else defn.NothingType
356+
else
357+
tp.withPrefix(pre1)
340358
finally locked -= tp
341-
tp.withPrefix(pre1)
342359
}
343360
else tp
344361
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
17651765
else report.error(new DuplicateBind(b, cdef), b.srcPos)
17661766
if (!ctx.isAfterTyper) {
17671767
val bounds = ctx.gadt.fullBounds(sym)
1768-
if (bounds != null) sym.info = bounds
1768+
if (bounds != null)
1769+
sym.info = bounds
1770+
sym.info = checkNonCyclic(sym, bounds, reportErrors = true)
17691771
}
17701772
b
17711773
case t: UnApply if t.symbol.is(Inline) => Inlines.inlinedUnapply(t)

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[+F[_]]:
2+
case Bar[B[_]](value: Foo[B]) extends Foo[B]
3+
4+
class Test:
5+
def test[X[_]](foo: Foo[X]): Foo[X] = foo match
6+
case Foo.Bar(Foo.Bar(x)) => Foo.Bar(x)
7+
case _ => foo

tests/pos/i14287.scala

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

tests/pos/i15523.avoid.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// scalac: -Werror
2+
// like the original, but with a case body `a`
3+
// which caused type avoidance to infinitely recurse
4+
sealed trait Parent
5+
final case class Leaf[A, B >: A](a: A, b: B) extends Parent
6+
7+
def run(x: Parent) = x match
8+
case Leaf(a, _) => a

tests/pos/i15523.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// scalac: -Werror
2+
sealed trait Parent
3+
final case class Leaf[A, B >: A](a: A, b: B) extends Parent
4+
5+
def run(x: Parent): Unit = x match {
6+
case Leaf(a, b) =>
7+
}

tests/pos/i16777.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// scalac: -Ykind-projector:underscores
2+
3+
sealed abstract class Free[+S[_, _], +E, +A] {
4+
@inline final def flatMap[S1[e, a] >: S[e, a], B, E1 >: E](fun: A => Free[S1, E1, B]): Free[S1, E1, B] = Free.FlatMapped[S1, E, E1, A, B](this, fun)
5+
@inline final def map[B](fun: A => B): Free[S, E, B] = flatMap(a => Free.pure[S, B](fun(a)))
6+
@inline final def as[B](as: => B): Free[S, E, B] = map(_ => as)
7+
@inline final def *>[S1[e, a] >: S[e, a], B, E1 >: E](sc: Free[S1, E1, B]): Free[S1, E1, B] = flatMap(_ => sc)
8+
@inline final def <*[S1[e, a] >: S[e, a], B, E1 >: E](sc: Free[S1, E1, B]): Free[S1, E1, A] = flatMap(r => sc.as(r))
9+
10+
@inline final def void: Free[S, E, Unit] = map(_ => ())
11+
12+
// FIXME: Scala 3.1.4 bug: false unexhaustive match warning
13+
/// @nowarn("msg=pattern case: Free.FlatMapped")
14+
@inline final def foldMap[S1[e, a] >: S[e, a], G[+_, +_]](transform: S1 ~>> G)(implicit G: Monad2[G]): G[E, A] = {
15+
this match {
16+
case Free.Pure(a) => G.pure(a)
17+
case Free.Suspend(a) => transform.apply(a)
18+
case Free.FlatMapped(sub, cont) =>
19+
sub match {
20+
case Free.FlatMapped(sub2, cont2) => sub2.flatMap(a => cont2(a).flatMap(cont)).foldMap(transform)
21+
case another => G.flatMap(another.foldMap(transform))(cont(_).foldMap(transform))
22+
}
23+
}
24+
}
25+
}
26+
27+
trait ~>>[-F[_, _], +G[_, _]] {
28+
def apply[E, A](f: F[E, A]): G[E, A]
29+
}
30+
31+
object Free {
32+
@inline def pure[S[_, _], A](a: A): Free[S, Nothing, A] = Pure(a)
33+
@inline def lift[S[_, _], E, A](s: S[E, A]): Free[S, E, A] = Suspend(s)
34+
35+
final case class Pure[S[_, _], A](a: A) extends Free[S, Nothing, A] {
36+
override def toString: String = s"Pure:[$a]"
37+
}
38+
final case class Suspend[S[_, _], E, A](a: S[E, A]) extends Free[S, E, A] {
39+
override def toString: String = s"Suspend:[$a]"
40+
}
41+
final case class FlatMapped[S[_, _], E, E1 >: E, A, B](sub: Free[S, E, A], cont: A => Free[S, E1, B]) extends Free[S, E1, B] {
42+
override def toString: String = s"FlatMapped:[sub=$sub]"
43+
}
44+
}
45+
46+
type Monad2[F[+_, +_]] = Monad3[λ[(`-R`, `+E`, `+A`) => F[E, A]]]
47+
48+
trait Monad3[F[-_, +_, +_]] {
49+
def flatMap[R, E, A, B](r: F[R, E, A])(f: A => F[R, E, B]): F[R, E, B]
50+
def flatten[R, E, A](r: F[R, E, F[R, E, A]]): F[R, E, A] = flatMap(r)(identity)
51+
def pure[A](a: A): F[Any, Nothing, A]
52+
}

0 commit comments

Comments
 (0)