@@ -1122,17 +1122,29 @@ 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 _ if info.derivesFrom(defn.ConversionClass ) => pt match
1129
+ case selProto : SelectionProto =>
1130
+ info.baseType(defn.ConversionClass ) match
1131
+ case AppliedType (_, List (_, restpe)) if selProto.isMatchedBy(restpe) =>
1132
+ // if we embed the SelectionProto as the Conversion result type
1133
+ // it might end up within a GADT cast type
1134
+ // so instead replace it with the targeted conversion type, if it matches
1135
+ // see tests/pos/i15867.scala.
1136
+ restpe
1137
+ case _ => pt
1138
+ case _ => pt
1139
+ case _ => NoType
1129
1140
def tryConversion (using Context ) = {
1141
+ val restpeConv = if ref.symbol.is(Given ) then conversionResultType(ref.symbol.info) else NoType
1130
1142
val untpdConv =
1131
- if ref.symbol.is( Given ) && producesConversion(ref.symbol.info) then
1143
+ if restpeConv.exists then
1132
1144
untpd.Select (
1133
1145
untpd.TypedSplice (
1134
1146
adapt(generated,
1135
- defn.ConversionClass .typeRef.appliedTo(argument.tpe, pt ),
1147
+ defn.ConversionClass .typeRef.appliedTo(argument.tpe, restpeConv ),
1136
1148
locked)),
1137
1149
nme.apply)
1138
1150
else untpdGenerated
0 commit comments