Skip to content

Commit db7c99c

Browse files
committed
Handle bidirectional typebounds in AvoidMap
An example of a bidirectional TypeBounds is i15523: `[A, B >: A]`, as an ordering: `A <: B`, as types and their bounds: `A >: Nothing <: B` and `B >: A <: Any`. It's not an fatal cycle, such as `A <: B <: A`. It's more like "My father is DJ, DJ's son is me" - if you then go ask who DJ's son's father is... you might never get an answer back.
1 parent c0a7d12 commit db7c99c

File tree

6 files changed

+98
-2
lines changed

6 files changed

+98
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ object TypeOps:
456456
sym.is(Package) || sym.isStatic && isStaticPrefix(pre.prefix)
457457
case _ => true
458458

459+
private var alreadyExpanding: List[TypeRef] = Nil
460+
459461
override def apply(tp: Type): Type =
460462
try
461463
tp match
@@ -468,8 +470,12 @@ object TypeOps:
468470
tp.info match {
469471
case info: AliasingBounds =>
470472
apply(info.alias)
471-
case TypeBounds(lo, hi) =>
472-
range(atVariance(-variance)(apply(lo)), apply(hi))
473+
case bounds: TypeBounds if !alreadyExpanding.contains(tp) =>
474+
val saved = alreadyExpanding
475+
try
476+
alreadyExpanding ::= tp
477+
expandBounds(bounds)
478+
finally alreadyExpanding = saved
473479
case info: ClassInfo =>
474480
range(defn.NothingType, apply(classBound(info)))
475481
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[+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)