@@ -23,7 +23,7 @@ import Inferencing._
2323import reporting ._
2424import transform .TypeUtils ._
2525import transform .SymUtils ._
26- import Nullables ._
26+ import Nullables ._ , NullOpsDecorator . *
2727import config .Feature
2828
2929import collection .mutable
@@ -340,6 +340,12 @@ object Applications {
340340 val getter = findDefaultGetter(fn, n, testOnly)
341341 if getter.isEmpty then getter
342342 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)
343349}
344350
345351trait Applications extends Compatibility {
@@ -936,6 +942,21 @@ trait Applications extends Compatibility {
936942 /** Type application where arguments come from prototype, and no implicits are inserted */
937943 def simpleApply (fun1 : Tree , proto : FunProto )(using Context ): Tree =
938944 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)
939960 case funRef : TermRef =>
940961 val app = ApplyTo (tree, fun1, funRef, proto, pt)
941962 convertNewGenericArray(
0 commit comments