@@ -1616,7 +1616,7 @@ trait Applications extends Compatibility {
1616
1616
* called twice from the public `resolveOverloaded` method, once with
1617
1617
* implicits and SAM conversions enabled, and once without.
1618
1618
*/
1619
- private def resolveOverloaded (alts : List [TermRef ], pt : Type , targs : List [Type ])(implicit ctx : Context ): List [TermRef ] = {
1619
+ private def resolveOverloaded (alts : List [TermRef ], pt : Type , targs : List [Type ])(implicit ctx : Context ): List [TermRef ] =
1620
1620
record(" resolveOverloaded/2" )
1621
1621
1622
1622
def isDetermined (alts : List [TermRef ]) = alts.isEmpty || alts.tail.isEmpty
@@ -1785,28 +1785,38 @@ trait Applications extends Compatibility {
1785
1785
candidates.flatMap(cloneCandidate)
1786
1786
}
1787
1787
1788
+ def resultIsMethod (tp : Type ): Boolean = tp.widen.stripPoly match
1789
+ case tp : MethodType => tp.resultType.isInstanceOf [MethodType ]
1790
+ case _ => false
1791
+
1788
1792
val found = narrowMostSpecific(candidates)
1789
1793
if (found.length <= 1 ) found
1790
- else pt match {
1791
- case pt @ FunProto (_, resType : FunProto ) =>
1792
- // try to narrow further with snd argument list
1793
- val advanced = advanceCandidates(pt.typedArgs().tpes)
1794
- resolveOverloaded(advanced.map(_._1), resType, Nil ) // resolve with candidates where first params are stripped
1795
- .map(advanced.toMap) // map surviving result(s) back to original candidates
1796
- case _ =>
1797
- val noDefaults = alts.filter(! _.symbol.hasDefaultParams)
1798
- val noDefaultsCount = noDefaults.length
1799
- if (noDefaultsCount == 1 )
1800
- noDefaults // return unique alternative without default parameters if it exists
1801
- else if (noDefaultsCount > 1 && noDefaultsCount < alts.length)
1802
- resolveOverloaded(noDefaults, pt, targs) // try again, dropping defult arguments
1803
- else {
1804
- val deepPt = pt.deepenProto
1805
- if (deepPt ne pt) resolveOverloaded(alts, deepPt, targs)
1806
- else candidates
1807
- }
1808
- }
1809
- }
1794
+ else
1795
+ val deepPt = pt.deepenProto
1796
+ deepPt match
1797
+ case pt @ FunProto (_, resType : FunProto ) =>
1798
+ // try to narrow further with snd argument list
1799
+ val advanced = advanceCandidates(pt.typedArgs().tpes)
1800
+ resolveOverloaded(advanced.map(_._1), resType, Nil ) // resolve with candidates where first params are stripped
1801
+ .map(advanced.toMap) // map surviving result(s) back to original candidates
1802
+ case _ =>
1803
+ // prefer alternatives that need no eta expansion
1804
+ val noCurried = alts.filter(! resultIsMethod(_))
1805
+ if noCurried.length == 1 then
1806
+ noCurried
1807
+ else
1808
+ // prefer alternatves that match without default parameters
1809
+ val noDefaults = noCurried.filter(! _.symbol.hasDefaultParams)
1810
+ val noDefaultsCount = noDefaults.length
1811
+ if noDefaultsCount == 1 then
1812
+ noDefaults // return unique alternative without default parameters if it exists
1813
+ else if noDefaultsCount > 1 && noDefaultsCount < alts.length then
1814
+ resolveOverloaded(noDefaults, pt, targs) // try again, dropping defult arguments
1815
+ else if deepPt ne pt then
1816
+ // try again with a deeper known expected type
1817
+ resolveOverloaded(alts, deepPt, targs)
1818
+ else candidates
1819
+ end resolveOverloaded
1810
1820
1811
1821
/** Try to typecheck any arguments in `pt` that are function values missing a
1812
1822
* parameter type. If the formal parameter types corresponding to a closure argument
0 commit comments