Skip to content

Commit

Permalink
Avoid embedding SelectionProtos in Conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jun 2, 2023
1 parent d0b790e commit c32adc2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
24 changes: 18 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1122,17 +1122,29 @@ trait Implicits:
adapt(generated, pt.widenExpr, locked)
else {
def untpdGenerated = untpd.TypedSplice(generated)
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 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) => pt match
case selProto: SelectionProto =>
info.baseType(defn.ConversionClass) match
case AppliedType(_, List(_, restpe)) if selProto.isMatchedBy(restpe) =>
// if we embed the SelectionProto as the Conversion result type
// it might end up within a GADT cast type
// so instead replace it with the targeted conversion type, if it matches
// see tests/pos/i15867.scala.
restpe
case _ => pt
case _ => pt
case _ => NoType
def tryConversion(using Context) = {
val restpeConv = if ref.symbol.is(Given) then conversionResultType(ref.symbol.info) else NoType
val untpdConv =
if ref.symbol.is(Given) && producesConversion(ref.symbol.info) then
if restpeConv.exists then
untpd.Select(
untpd.TypedSplice(
adapt(generated,
defn.ConversionClass.typeRef.appliedTo(argument.tpe, pt),
defn.ConversionClass.typeRef.appliedTo(argument.tpe, restpeConv),
locked)),
nme.apply)
else untpdGenerated
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotc/pos-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ i4176-gadt.scala

# GADT difference
i13974a.scala
i15867.scala

java-inherited-type1

Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i15867.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum SUB[-A, +B]:
case Refl[S]() extends SUB[S, S]

class Pow(self: Int):
def **(other: Int): Int = math.pow(self, other).toInt

given fromList[T]: Conversion[List[T], Pow] = ???

given fromInt: Conversion[Int, Pow] = Pow(_)

def foo[T](t1: T, ev: T SUB List[Int]) =
ev match { case SUB.Refl() =>
t1 ** 2 // error
}

def baz[T](t2: T, ev: T SUB Int) =
ev match { case SUB.Refl() =>
t2 ** 2 // works
}

0 comments on commit c32adc2

Please sign in to comment.