Skip to content

Commit 9000ebf

Browse files
committed
Avoid GADT casting with ProtoTypes
1 parent eb28fd4 commit 9000ebf

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4560,7 +4560,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45604560
AnnotatedType(conj, Annotation(defn.UncheckedStableAnnot, tree.symbol.span))
45614561
else conj
45624562
else pt
4563-
gadts.println(i"insert GADT cast from $tree to $target")
4564-
tree.cast(target)
4563+
if target.existsPart(_.isInstanceOf[ProtoType]) then
4564+
// we want to avoid embedding a SelectionProto in a Conversion, as the result type
4565+
// as it might end up within a GADT cast type, e.g. tests/pos/i15867.scala
4566+
// so we just bail - in that example, a GADT cast will be insert on application, so it compiles.
4567+
// but tests/pos/i18062.scala is an example with a polymorphic method, which requires type variables to
4568+
// be applied to the tree and then constrained before they match the prototype.
4569+
// so rather than try to handle all that before calling adapt, let's just bail on this side.
4570+
tree
4571+
else
4572+
gadts.println(i"insert GADT cast from $tree to $target")
4573+
tree.cast(target)
45654574
end insertGadtCast
45664575
}

tests/pos/i18062.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait CB[X] { def get: X }
2+
3+
trait WrapperConvert[F[_], G[_]]:
4+
def conv[X](fx: F[X]): G[X]
5+
6+
object WrapperConvert:
7+
implicit def id[F[_]]: WrapperConvert[F, F] = new WrapperConvert[F, F]:
8+
def conv[X](fx: F[X]): F[X] = fx
9+
10+
transparent inline given convertX[F[_], X](using wc: WrapperConvert[F, CB]): Conversion[F[X], X] =
11+
new Conversion[F[X], X]:
12+
def apply(fx: F[X]) = wc.conv(fx).get
13+
14+
def test(cb: CB[Int], x: Int): Int = cb + x

0 commit comments

Comments
 (0)