Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refix avoid GADT casting with ProtoTypes #18085

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1122,33 +1122,17 @@ trait Implicits:
adapt(generated, pt.widenExpr, locked)
else {
def untpdGenerated = untpd.TypedSplice(generated)
def conversionResultType(info: Type): Type = info match
case info: PolyType => conversionResultType(info.resType)
case info: MethodType if info.isImplicitMethod => conversionResultType(info.resType)
case _ =>
if info.derivesFrom(defn.ConversionClass) then
pt match
case selProto: SelectionProto =>
// 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, if we can find the target result type - as in,
// if it matches the selection prototype, then let's adapt to that instead
// otherwise just skip adapting with a prototype (by returning NoType)
info.baseType(defn.ConversionClass) match
case AppliedType(_, List(_, restpe)) if selProto.isMatchedBy(restpe) =>
restpe
case _ => NoType // can't find conversion result type, avoid adapting with SelectionProto
case _: ProtoType => NoType // avoid adapting with ProtoType
case _ => pt // not a ProtoType, so use it for adapting
else NoType // not a Conversion, don't adapt
def producesConversion(info: Type): Boolean = info match
case info: PolyType => producesConversion(info.resType)
case info: MethodType if info.isImplicitMethod => producesConversion(info.resType)
case _ => info.derivesFrom(defn.ConversionClass)
def tryConversion(using Context) = {
val restpeConv = if ref.symbol.is(Given) then conversionResultType(ref.widenTermRefExpr) else NoType
val untpdConv =
if restpeConv.exists then
if ref.symbol.is(Given) && producesConversion(ref.symbol.info) then
untpd.Select(
untpd.TypedSplice(
adapt(generated,
defn.ConversionClass.typeRef.appliedTo(argument.tpe, restpeConv),
defn.ConversionClass.typeRef.appliedTo(argument.tpe, pt),
locked)),
nme.apply)
else untpdGenerated
Expand Down
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
Loading