From 6805c393db1c057d2ce927858cf3474f6f662798 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 26 Aug 2019 18:49:12 +0200 Subject: [PATCH] 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. --- .../src/dotty/tools/dotc/typer/Applications.scala | 8 ++++++-- tests/pos/i7082.scala | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i7082.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 49a124823534..e23d605641a9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -1721,11 +1721,15 @@ trait Applications extends Compatibility { self: Typer with Dynamic => .map(advanced.toMap) // map surviving result(s) back to original candidates case _ => val noDefaults = alts.filter(!_.symbol.hasDefaultParams) - if (noDefaults.length == 1) noDefaults // return unique alternative without default parameters if it exists + val noDefaultsCount = noDefaults.length + if (noDefaultsCount == 1) + noDefaults // return unique alternative without default parameters if it exists + else if (noDefaultsCount > 1 && noDefaultsCount < alts.length) + resolveOverloaded(noDefaults, pt, targs) // try again, dropping defult arguments else { val deepPt = pt.deepenProto if (deepPt ne pt) resolveOverloaded(alts, deepPt, targs) - else alts + else candidates } } } diff --git a/tests/pos/i7082.scala b/tests/pos/i7082.scala new file mode 100644 index 000000000000..a7d5ec572cb9 --- /dev/null +++ b/tests/pos/i7082.scala @@ -0,0 +1,11 @@ +object Overloads { + + def foo[V](x: Int = 0, y: Int = 0, z: Int = 0): Nothing = ??? + + def foo[V](x: Int, y: Int): Nothing = ??? + + def foo[V](x: Int): Nothing = ??? + + foo(1) + +} \ No newline at end of file