@@ -474,21 +474,24 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
474
474
type TypedArg = Arg
475
475
type Result = Unit
476
476
477
+ protected def argOK (arg : TypedArg , formal : Type ) = argType(arg, formal) match {
478
+ case ref : TermRef if ref.denot.isOverloaded =>
479
+ // in this case we could not resolve overloading because no alternative
480
+ // matches expected type
481
+ false
482
+ case argtpe =>
483
+ def SAMargOK = formal match {
484
+ case SAMType (meth) => argtpe <:< meth.info.toFunctionType()
485
+ case _ => false
486
+ }
487
+ isCompatible(argtpe, formal) || ctx.mode.is(Mode .ImplicitsEnabled ) && SAMargOK
488
+ }
489
+
477
490
/** The type of the given argument */
478
491
protected def argType (arg : Arg , formal : Type ): Type
479
492
480
493
def typedArg (arg : Arg , formal : Type ): Arg = arg
481
- def addArg (arg : TypedArg , formal : Type ) =
482
- ok = ok & {
483
- argType(arg, formal) match {
484
- case ref : TermRef if ref.denot.isOverloaded =>
485
- // in this case we could not resolve overloading because no alternative
486
- // matches expected type
487
- false
488
- case argtpe =>
489
- isCompatible(argtpe, formal)
490
- }
491
- }
494
+ final def addArg (arg : TypedArg , formal : Type ) = ok = ok & argOK(arg, formal)
492
495
def makeVarArg (n : Int , elemFormal : Type ) = {}
493
496
def fail (msg : => Message , arg : Arg ) =
494
497
ok = false
@@ -512,11 +515,10 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
512
515
}
513
516
514
517
/** Subclass of Application for applicability tests with type arguments and value
515
- * argument trees.
516
- */
518
+ * argument trees.
519
+ */
517
520
class ApplicableToTreesDirectly (methRef : TermRef , targs : List [Type ], args : List [Tree ], resultType : Type )(implicit ctx : Context ) extends ApplicableToTrees (methRef, targs, args, resultType)(ctx) {
518
- override def addArg (arg : TypedArg , formal : Type ) =
519
- ok = ok & (argType(arg, formal) <:< formal)
521
+ override def argOK (arg : TypedArg , formal : Type ) = argType(arg, formal) <:< formal
520
522
}
521
523
522
524
/** Subclass of Application for applicability tests with value argument types. */
@@ -1197,7 +1199,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1197
1199
/** Resolve overloaded alternative `alts`, given expected type `pt` and
1198
1200
* possibly also type argument `targs` that need to be applied to each alternative
1199
1201
* to form the method type.
1200
- * todo: use techniques like for implicits to pick candidates quickly?
1202
+ * Two trials: First, without implicits or SAM conversions enabled. Then,
1203
+ * if the fist finds no eligible candidates, with implicits and SAM conversions enabled.
1201
1204
*/
1202
1205
def resolveOverloaded (alts : List [TermRef ], pt : Type )(implicit ctx : Context ): List [TermRef ] = track(" resolveOverloaded" ) {
1203
1206
@@ -1222,7 +1225,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1222
1225
* fallback to `chosen`.
1223
1226
*
1224
1227
* Note this order of events is done for speed. One might be tempted to
1225
- * preselect alternatives by result type. But is slower, because it discriminates
1228
+ * preselect alternatives by result type. But this is slower, because it discriminates
1226
1229
* less. The idea is when searching for a best solution, as is the case in overloading
1227
1230
* resolution, we should first try criteria which are cheap and which have a high
1228
1231
* probability of pruning the search. result type comparisons are neither cheap nor
@@ -1258,7 +1261,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1258
1261
/** This private version of `resolveOverloaded` does the bulk of the work of
1259
1262
* overloading resolution, but does not do result adaptation. It might be
1260
1263
* called twice from the public `resolveOverloaded` method, once with
1261
- * implicits enabled, and once without.
1264
+ * implicits and SAM conversions enabled, and once without.
1262
1265
*/
1263
1266
private def resolveOverloaded (alts : List [TermRef ], pt : Type , targs : List [Type ])(implicit ctx : Context ): List [TermRef ] = track(" resolveOverloaded" ) {
1264
1267
@@ -1338,7 +1341,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
1338
1341
if (isDetermined(alts1)) alts1
1339
1342
else {
1340
1343
val alts2 = narrowByShapes(alts1)
1341
- // ctx.log(i"narrowed by shape: ${alts1 .map(_.symbol.showDcl)}%, %")
1344
+ // ctx.log(i"narrowed by shape: ${alts2 .map(_.symbol.showDcl)}%, %")
1342
1345
if (isDetermined(alts2)) alts2
1343
1346
else {
1344
1347
pretypeArgs(alts2, pt)
0 commit comments