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