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