Skip to content

Commit

Permalink
Avoid GADT casting with ProtoTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 28, 2023
1 parent eb28fd4 commit 9000ebf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4560,7 +4560,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
AnnotatedType(conj, Annotation(defn.UncheckedStableAnnot, tree.symbol.span))
else conj
else pt
gadts.println(i"insert GADT cast from $tree to $target")
tree.cast(target)
if target.existsPart(_.isInstanceOf[ProtoType]) then
// we want to avoid embedding a SelectionProto in a Conversion, as the result type
// as it might end up within a GADT cast type, e.g. tests/pos/i15867.scala
// so we just bail - in that example, a GADT cast will be insert on application, so it compiles.
// but tests/pos/i18062.scala is an example with a polymorphic method, which requires type variables to
// be applied to the tree and then constrained before they match the prototype.
// so rather than try to handle all that before calling adapt, let's just bail on this side.
tree
else
gadts.println(i"insert GADT cast from $tree to $target")
tree.cast(target)
end insertGadtCast
}
14 changes: 14 additions & 0 deletions tests/pos/i18062.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait CB[X] { def get: X }

trait WrapperConvert[F[_], G[_]]:
def conv[X](fx: F[X]): G[X]

object WrapperConvert:
implicit def id[F[_]]: WrapperConvert[F, F] = new WrapperConvert[F, F]:
def conv[X](fx: F[X]): F[X] = fx

transparent inline given convertX[F[_], X](using wc: WrapperConvert[F, CB]): Conversion[F[X], X] =
new Conversion[F[X], X]:
def apply(fx: F[X]) = wc.conv(fx).get

def test(cb: CB[Int], x: Int): Int = cb + x

0 comments on commit 9000ebf

Please sign in to comment.