@@ -1122,17 +1122,33 @@ trait Implicits:
1122
1122
adapt(generated, pt.widenExpr, locked)
1123
1123
else {
1124
1124
def untpdGenerated = untpd.TypedSplice (generated)
1125
- def producesConversion (info : Type ): Boolean = info match
1126
- case info : PolyType => producesConversion(info.resType)
1127
- case info : MethodType if info.isImplicitMethod => producesConversion(info.resType)
1128
- case _ => info.derivesFrom(defn.ConversionClass )
1125
+ def conversionResultType (info : Type ): Type = info match
1126
+ case info : PolyType => conversionResultType(info.resType)
1127
+ case info : MethodType if info.isImplicitMethod => conversionResultType(info.resType)
1128
+ case _ =>
1129
+ if info.derivesFrom(defn.ConversionClass ) then
1130
+ pt match
1131
+ case selProto : SelectionProto =>
1132
+ // we want to avoid embedding a SelectionProto in a Conversion, as the result type
1133
+ // as it might end up within a GADT cast type, e.g. tests/pos/i15867.scala
1134
+ // so, if we can find the target result type - as in,
1135
+ // if it matches the selection prototype, then let's adapt to that instead
1136
+ // otherwise just skip adapting with a prototype (by returning NoType)
1137
+ info.baseType(defn.ConversionClass ) match
1138
+ case AppliedType (_, List (_, restpe)) if selProto.isMatchedBy(restpe) =>
1139
+ restpe
1140
+ case _ => NoType // can't find conversion result type, avoid adapting with SelectionProto
1141
+ case _ : ProtoType => NoType // avoid adapting with ProtoType
1142
+ case _ => pt // not a ProtoType, so use it for adapting
1143
+ else NoType // not a Conversion, don't adapt
1129
1144
def tryConversion (using Context ) = {
1145
+ val restpeConv = if ref.symbol.is(Given ) then conversionResultType(ref.widenTermRefExpr) else NoType
1130
1146
val untpdConv =
1131
- if ref.symbol.is( Given ) && producesConversion(ref.symbol.info) then
1147
+ if restpeConv.exists then
1132
1148
untpd.Select (
1133
1149
untpd.TypedSplice (
1134
1150
adapt(generated,
1135
- defn.ConversionClass .typeRef.appliedTo(argument.tpe, pt ),
1151
+ defn.ConversionClass .typeRef.appliedTo(argument.tpe, restpeConv ),
1136
1152
locked)),
1137
1153
nme.apply)
1138
1154
else untpdGenerated
0 commit comments