Skip to content

Commit

Permalink
Avoid embedding SelectionProtos in Conversions (#17755)
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jun 6, 2023
2 parents 0749271 + 701254f commit 233c8ec
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
28 changes: 22 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,33 @@ 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) 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 tryConversion(using Context) = {
val restpeConv = if ref.symbol.is(Given) then conversionResultType(ref.widenTermRefExpr) 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
}
9 changes: 9 additions & 0 deletions tests/pos/i15867.specs2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo:
given Conversion[String, Data] with
def apply(str: String): Data = new Data(str)

class Data(str: String):
def |(str: String) = new Data(this.str + str)

class Bar extends Foo:
"str" | "ing"

0 comments on commit 233c8ec

Please sign in to comment.