@@ -23,7 +23,7 @@ import Inferencing._
23
23
import reporting ._
24
24
import transform .TypeUtils ._
25
25
import transform .SymUtils ._
26
- import Nullables ._
26
+ import Nullables ._ , NullOpsDecorator . *
27
27
import config .Feature
28
28
29
29
import collection .mutable
@@ -340,6 +340,12 @@ object Applications {
340
340
val getter = findDefaultGetter(fn, n, testOnly)
341
341
if getter.isEmpty then getter
342
342
else spliceMeth(getter.withSpan(fn.span), fn)
343
+
344
+ def retypeSignaturePolymorphicFn (fun : Tree , methType : Type )(using Context ): Tree =
345
+ val sym1 = fun.symbol
346
+ val flags2 = sym1.flags | NonMember // ensures Select typing doesn't let TermRef#withPrefix revert the type
347
+ val sym2 = sym1.copy(info = methType, flags = flags2) // symbol not entered, to avoid overload resolution problems
348
+ fun.withType(sym2.termRef)
343
349
}
344
350
345
351
trait Applications extends Compatibility {
@@ -936,6 +942,21 @@ trait Applications extends Compatibility {
936
942
/** Type application where arguments come from prototype, and no implicits are inserted */
937
943
def simpleApply (fun1 : Tree , proto : FunProto )(using Context ): Tree =
938
944
methPart(fun1).tpe match {
945
+ case funRef : TermRef if funRef.symbol.isSignaturePolymorphic =>
946
+ // synthesize a method type based on the types at the call site.
947
+ // one can imagine the original signature-polymorphic method as
948
+ // being infinitely overloaded, with each individual overload only
949
+ // being brought into existence as needed
950
+ val originalResultType = funRef.symbol.info.resultType.stripNull
951
+ val resultType =
952
+ if ! originalResultType.isRef(defn.ObjectClass ) then originalResultType
953
+ else AvoidWildcardsMap ()(proto.resultType.deepenProtoTrans) match
954
+ case SelectionProto (nme.asInstanceOf_, PolyProto (_, resTp), _, _) => resTp
955
+ case resTp if isFullyDefined(resTp, ForceDegree .all) => resTp
956
+ case _ => defn.ObjectType
957
+ val methType = MethodType (proto.typedArgs().map(_.tpe.widen), resultType)
958
+ val fun2 = Applications .retypeSignaturePolymorphicFn(fun1, methType)
959
+ simpleApply(fun2, proto)
939
960
case funRef : TermRef =>
940
961
val app = ApplyTo (tree, fun1, funRef, proto, pt)
941
962
convertNewGenericArray(
0 commit comments