Skip to content

Commit 82c8c63

Browse files
committed
Keep HasDefault flag updated in overriding methods
Fixes #16006 #16006 started failing since we now count the number of default parameters for better precision in overloading resolution. But that count was wrong since it did not take inherited default getters from overridden methods into account. We fix this by setting the HasDefault flag also for parameters that don't have a default value themselves, but that correspond to parameters with default values in overridden methods.
1 parent a503b7a commit 82c8c63

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,27 @@ object SymDenotations {
938938

939939
/** Does this symbol have defined or inherited default parameters?
940940
* Default parameters are recognized until erasure.
941+
* As a side effect, sets HasDefault flag of parameters in methods that
942+
* override a method with a corresponding default parameter.
941943
*/
942944
def hasDefaultParams(using Context): Boolean =
943945
if ctx.erasedTypes then false
944946
else if is(HasDefaultParams) then true
945947
else if is(NoDefaultParams) || !is(Method) then false
946948
else
947-
val result =
948-
rawParamss.nestedExists(_.is(HasDefault))
949-
|| allOverriddenSymbols.exists(_.hasDefaultParams)
949+
val inheritedDefaultParamNames = util.HashSet[Name]()
950+
for prev <- allOverriddenSymbols do
951+
if prev.hasDefaultParams then
952+
for params <- prev.rawParamss do
953+
for param <- params do
954+
if param.is(HasDefault) then inheritedDefaultParamNames += param.name
955+
if !inheritedDefaultParamNames.isEmpty then
956+
for params <- rawParamss do
957+
for param <- params do
958+
if inheritedDefaultParamNames.contains(param.name) then
959+
param.setFlag(HasDefault)
960+
val result = rawParamss.nestedExists(_.is(HasDefault))
961+
if allOverriddenSymbols.exists(_.hasDefaultParams) && !ctx.isAfterTyper then assert(result)
950962
setFlag(if result then HasDefaultParams else NoDefaultParams)
951963
result
952964

tests/run/i16006.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
overriden (3, 10)

tests/run/i16006.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class X:
2+
def toString(maxLines: Int = 10, maxWidth: Int = 10): String = (maxLines -> maxWidth).toString
3+
4+
class Foo extends X:
5+
override def toString(maxLines: Int, maxWidth: Int): String = s"overriden ($maxLines, $maxWidth)"
6+
override def toString(): String = toString(maxLines = 3)
7+
8+
9+
@main def Test = {
10+
println(Foo().toString())
11+
}

0 commit comments

Comments
 (0)