@@ -1254,8 +1254,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1254
1254
* If that tournament yields a draw, a tiebreak is applied where
1255
1255
* an alternative that takes more implicit parameters wins over one
1256
1256
* that takes fewer.
1257
+ *
1258
+ * @param followApply if true consider `apply` members when comparing with a method reference
1257
1259
*/
1258
- def compare (alt1 : TermRef , alt2 : TermRef )(implicit ctx : Context ): Int = track(" compare" ) { trace(i " compare( $alt1, $alt2) " , overload) {
1260
+ def compare (alt1 : TermRef , alt2 : TermRef , followApply : Boolean )(implicit ctx : Context ): Int = track(" compare" ) { trace(i " compare( $alt1, $alt2) " , overload) {
1259
1261
1260
1262
assert(alt1 ne alt2)
1261
1263
@@ -1279,8 +1281,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1279
1281
val formals1 =
1280
1282
if (tp1.isVarArgsMethod && tp2.isVarArgsMethod) tp1.paramInfos.map(_.repeatedToSingle)
1281
1283
else tp1.paramInfos
1282
- isApplicableMethodRef(alt2, formals1, WildcardType ) ||
1283
- tp1.paramInfos.isEmpty && tp2.isInstanceOf [LambdaType ]
1284
+ val isAsSpecificMethod =
1285
+ if (followApply) isApplicableType(alt2, formals1, WildcardType )
1286
+ else isApplicableMethodRef(alt2, formals1, WildcardType )
1287
+ isAsSpecificMethod || tp1.paramInfos.isEmpty && tp2.isInstanceOf [LambdaType ]
1284
1288
case tp1 : PolyType => // (2)
1285
1289
val nestedCtx = ctx.fresh.setExploreTyperState()
1286
1290
@@ -1426,20 +1430,20 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1426
1430
else compareWithTypes(fullType1, fullType2) // continue by comparing implicits parameters
1427
1431
}}
1428
1432
1429
- def narrowMostSpecific (alts : List [TermRef ])(implicit ctx : Context ): List [TermRef ] = track(" narrowMostSpecific" ) {
1433
+ def narrowMostSpecific (alts : List [TermRef ], followApply : Boolean )(implicit ctx : Context ): List [TermRef ] = track(" narrowMostSpecific" ) {
1430
1434
alts match {
1431
1435
case Nil => alts
1432
1436
case _ :: Nil => alts
1433
1437
case alt1 :: alt2 :: Nil =>
1434
- compare(alt1, alt2) match {
1438
+ compare(alt1, alt2, followApply ) match {
1435
1439
case 1 => alt1 :: Nil
1436
1440
case - 1 => alt2 :: Nil
1437
1441
case 0 => alts
1438
1442
}
1439
1443
case alt :: alts1 =>
1440
1444
def survivors (previous : List [TermRef ], alts : List [TermRef ]): List [TermRef ] = alts match {
1441
1445
case alt :: alts1 =>
1442
- compare(previous.head, alt) match {
1446
+ compare(previous.head, alt, followApply ) match {
1443
1447
case 1 => survivors(previous, alts1)
1444
1448
case - 1 => survivors(alt :: previous.tail, alts1)
1445
1449
case 0 => survivors(alt :: previous, alts1)
@@ -1449,7 +1453,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1449
1453
val best :: rest = survivors(alt :: Nil , alts1)
1450
1454
def asGood (alts : List [TermRef ]): List [TermRef ] = alts match {
1451
1455
case alt :: alts1 =>
1452
- if (compare(alt, best) < 0 ) asGood(alts1) else alt :: asGood(alts1)
1456
+ if (compare(alt, best, followApply ) < 0 ) asGood(alts1) else alt :: asGood(alts1)
1453
1457
case nil =>
1454
1458
Nil
1455
1459
}
@@ -1550,9 +1554,13 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1550
1554
def narrowByTypes (alts : List [TermRef ], argTypes : List [Type ], resultType : Type ): List [TermRef ] =
1551
1555
alts filter (isApplicableType(_, argTypes, resultType))
1552
1556
1557
+ val numArgs = pt match {
1558
+ case pt @ FunProto (args, resultType) => args.length
1559
+ case _ => 0
1560
+ }
1561
+
1553
1562
val candidates = pt match {
1554
1563
case pt @ FunProto (args, resultType) =>
1555
- val numArgs = args.length
1556
1564
val normArgs = args.mapConserve {
1557
1565
case Block (Nil , expr) => expr
1558
1566
case x => x
@@ -1660,7 +1668,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1660
1668
candidates.flatMap(cloneCandidate)
1661
1669
}
1662
1670
1663
- val found = narrowMostSpecific(candidates)
1671
+ val found = narrowMostSpecific(candidates, followApply = numArgs != 0 )
1664
1672
if (found.length <= 1 ) found
1665
1673
else pt match {
1666
1674
case pt @ FunProto (_, resType : FunProto ) =>
0 commit comments