@@ -3427,42 +3427,59 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3427
3427
ErrorReporting .missingArgs(tree, mt)
3428
3428
tree.withType(mt.resultType)
3429
3429
3430
- def adaptOverloaded (ref : TermRef ) = {
3430
+ def adaptOverloaded (ref : TermRef ) =
3431
+ // get all the alternatives
3431
3432
val altDenots =
3432
3433
val allDenots = ref.denot.alternatives
3433
3434
if pt.isExtensionApplyProto then allDenots.filter(_.symbol.is(ExtensionMethod ))
3434
3435
else allDenots
3436
+
3435
3437
typr.println(i " adapt overloaded $ref with alternatives ${altDenots map (_.info)}% \n\n % " )
3438
+
3439
+ /** Search for an alternative that does not take parameters.
3440
+ * If there is one, return it, otherwise emit an error.
3441
+ */
3442
+ def tryParameterless (alts : List [TermRef ])(error : => tpd.Tree ): Tree =
3443
+ alts.filter(_.info.isParameterless) match
3444
+ case alt :: Nil => readaptSimplified(tree.withType(alt))
3445
+ case _ =>
3446
+ if altDenots.exists(_.info.paramInfoss == ListOfNil ) then
3447
+ typed(untpd.Apply (untpd.TypedSplice (tree), Nil ), pt, locked)
3448
+ else
3449
+ error
3450
+
3436
3451
def altRef (alt : SingleDenotation ) = TermRef (ref.prefix, ref.name, alt)
3437
3452
val alts = altDenots.map(altRef)
3438
- resolveOverloaded(alts, pt) match {
3453
+
3454
+ resolveOverloaded(alts, pt) match
3439
3455
case alt :: Nil =>
3440
3456
readaptSimplified(tree.withType(alt))
3441
3457
case Nil =>
3442
- // If alternative matches, there are still two ways to recover:
3458
+ // If no alternative matches, there are still two ways to recover:
3443
3459
// 1. If context is an application, try to insert an apply or implicit
3444
3460
// 2. If context is not an application, pick a alternative that does
3445
3461
// not take parameters.
3446
- def noMatches =
3447
- errorTree(tree, NoMatchingOverload (altDenots, pt))
3448
- def hasEmptyParams ( denot : SingleDenotation ) = denot.info.paramInfoss == ListOfNil
3449
- pt match {
3462
+
3463
+ def errorNoMatch = errorTree(tree, NoMatchingOverload (altDenots, pt))
3464
+
3465
+ pt match
3450
3466
case pt : FunOrPolyProto if pt.applyKind != ApplyKind .Using =>
3451
3467
// insert apply or convert qualifier, but only for a regular application
3452
- tryInsertApplyOrImplicit(tree, pt, locked)(noMatches )
3468
+ tryInsertApplyOrImplicit(tree, pt, locked)(errorNoMatch )
3453
3469
case _ =>
3454
- alts.filter(_.info.isParameterless) match {
3455
- case alt :: Nil => readaptSimplified(tree.withType(alt))
3456
- case _ =>
3457
- if (altDenots exists (_.info.paramInfoss == ListOfNil ))
3458
- typed(untpd.Apply (untpd.TypedSplice (tree), Nil ), pt, locked)
3459
- else
3460
- noMatches
3461
- }
3462
- }
3470
+ tryParameterless(alts)(errorNoMatch)
3471
+
3463
3472
case ambiAlts =>
3464
- if tree.tpe.isErroneous || pt.isErroneous then tree.withType(UnspecifiedErrorType )
3465
- else
3473
+ // If there are ambiguous alternatives, and:
3474
+ // 1. the types aren't erroneous
3475
+ // 2. the expected type is not a function type
3476
+ // 3. there exist a parameterless alternative
3477
+ //
3478
+ // Then, pick the parameterless alternative.
3479
+ // See tests/pos/i10715-scala and tests/pos/i10715-java.
3480
+
3481
+ /** Constructs an "ambiguous overload" error */
3482
+ def errorAmbiguous =
3466
3483
val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
3467
3484
val addendum =
3468
3485
if ambiAlts.exists(! _.symbol.exists) then
@@ -3471,8 +3488,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3471
3488
|Note: Overloaded definitions introduced by refinements cannot be resolved """
3472
3489
else " "
3473
3490
errorTree(tree, AmbiguousOverload (tree, remainingDenots, pt, addendum))
3474
- }
3475
- }
3491
+ end errorAmbiguous
3492
+
3493
+ if tree.tpe.isErroneous || pt.isErroneous then
3494
+ tree.withType(UnspecifiedErrorType )
3495
+ else
3496
+ pt match
3497
+ case _ : FunProto =>
3498
+ errorAmbiguous
3499
+ case _ =>
3500
+ tryParameterless(alts)(errorAmbiguous)
3501
+
3502
+ end match
3503
+ end adaptOverloaded
3476
3504
3477
3505
def adaptToArgs (wtp : Type , pt : FunProto ): Tree = wtp match {
3478
3506
case wtp : MethodOrPoly =>
0 commit comments