Skip to content

Commit

Permalink
Refix avoid GADT casting with ProtoTypes (#18085)
Browse files Browse the repository at this point in the history
Revert #17755, refix #15867, fixes  #18062
  • Loading branch information
odersky committed Jun 28, 2023
2 parents 08de2ba + 9000ebf commit f3787eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
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 @@ -4561,7 +4561,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 f3787eb

Please sign in to comment.