@@ -3424,42 +3424,59 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34243424 ErrorReporting .missingArgs(tree, mt)
34253425 tree.withType(mt.resultType)
34263426
3427- def adaptOverloaded (ref : TermRef ) = {
3427+ def adaptOverloaded (ref : TermRef ) =
3428+ // get all the alternatives
34283429 val altDenots =
34293430 val allDenots = ref.denot.alternatives
34303431 if pt.isExtensionApplyProto then allDenots.filter(_.symbol.is(ExtensionMethod ))
34313432 else allDenots
3433+
34323434 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+
34333448 def altRef (alt : SingleDenotation ) = TermRef (ref.prefix, ref.name, alt)
34343449 val alts = altDenots.map(altRef)
3435- resolveOverloaded(alts, pt) match {
3450+
3451+ resolveOverloaded(alts, pt) match
34363452 case alt :: Nil =>
34373453 readaptSimplified(tree.withType(alt))
34383454 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:
34403456 // 1. If context is an application, try to insert an apply or implicit
34413457 // 2. If context is not an application, pick a alternative that does
34423458 // 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
34473463 case pt : FunOrPolyProto if pt.applyKind != ApplyKind .Using =>
34483464 // insert apply or convert qualifier, but only for a regular application
3449- tryInsertApplyOrImplicit(tree, pt, locked)(noMatches )
3465+ tryInsertApplyOrImplicit(tree, pt, locked)(errorNoMatch )
34503466 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+
34603469 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 =
34633480 val remainingDenots = altDenots.filter(denot => ambiAlts.contains(altRef(denot)))
34643481 val addendum =
34653482 if ambiAlts.exists(! _.symbol.exists) then
@@ -3468,8 +3485,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34683485 |Note: Overloaded definitions introduced by refinements cannot be resolved """
34693486 else " "
34703487 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
34733501
34743502 def adaptToArgs (wtp : Type , pt : FunProto ): Tree = wtp match {
34753503 case wtp : MethodOrPoly =>
0 commit comments