Skip to content

Commit 6805c39

Browse files
committed
Fix #7082: Refine overloading resolution with default arguments
We previously had a fallback to pick methods without default arguments over methods with default arguments as a final tie breaker. (Rolling the distinction into the specificity test was tried previously, but without success). The fallback only worked if there was a single alternative without default arguments. This is now generalized to the case where there are several such alternatives. Also, we now avoid mentioning inapplicable alternatives in overloading ambiguity error messages.
1 parent 840a122 commit 6805c39

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,11 +1721,15 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
17211721
.map(advanced.toMap) // map surviving result(s) back to original candidates
17221722
case _ =>
17231723
val noDefaults = alts.filter(!_.symbol.hasDefaultParams)
1724-
if (noDefaults.length == 1) noDefaults // return unique alternative without default parameters if it exists
1724+
val noDefaultsCount = noDefaults.length
1725+
if (noDefaultsCount == 1)
1726+
noDefaults // return unique alternative without default parameters if it exists
1727+
else if (noDefaultsCount > 1 && noDefaultsCount < alts.length)
1728+
resolveOverloaded(noDefaults, pt, targs) // try again, dropping defult arguments
17251729
else {
17261730
val deepPt = pt.deepenProto
17271731
if (deepPt ne pt) resolveOverloaded(alts, deepPt, targs)
1728-
else alts
1732+
else candidates
17291733
}
17301734
}
17311735
}

tests/pos/i7082.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Overloads {
2+
3+
def foo[V](x: Int = 0, y: Int = 0, z: Int = 0): Nothing = ???
4+
5+
def foo[V](x: Int, y: Int): Nothing = ???
6+
7+
def foo[V](x: Int): Nothing = ???
8+
9+
foo(1)
10+
11+
}

0 commit comments

Comments
 (0)