-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SI-8197 clarify overloading resolution with default args #3583
Conversation
Review by @lrytz. This is a minimal fix. TODOs added for consideration in 2.11.1/2.12. It's too late for an overhaul now. |
One (minor?) wrinkle: assignments need not be of type |
Oh, I didn't know you were also working on it.. my version is here: lrytz@5ab7a7b |
Indeed, I never thought of this case. The following (wrongly) prints
|
thanks; I have to say I'm not sure about the implications of setting |
Good point. I'll think about this some more, but my intention was to implement "Otherwise, let C be the set of applicable alternatives which don’t employ any default argument in the application to e1, ..., em.". To rule out "employment" of default arguments, my interpretation is that SI-8197 shows that you can't allow tupling. |
It seems to me tupling should not be part of applicability, as it's a fall-back after trying everything else. |
I'd prefer to fix spec and implementation, but I guess the time for that has run out.. I'm just a bit worried that the changes in this PR and/or d5bb19f introduce more issues that we didn't see so far. But since I could not come up with counter-examples, LGTM. As a way forward, I see two options:
I think it would be good to migrate Scala to 2. |
The first problem is that auto-tupling is not in the spec (as far as i can see).. But I think it needs to be checked in applicability, otherwise a valid invocation would not be applicable (?). Btw, are there plans to deprecate auto-tupling?
I don't see how SI-8197 is related to auto-tupling.. |
This little excursion has convinced me to start with method/function application as my first exercise in aligning the spec & implementation. I agree we should try to simplify. In the end, the only change by the SI-8197 PRs has been to remove a no-op (checking the default param flag) and disallowing tuple conversion during this part of overload resolution (so that the original check actually excluded default arguments by comparing arities more strictly). |
my reconstruction is that the original arity check was indirectly checking that no default arguments were used, which was then weakened to allow tupling, and thus no longer excluded defaults when tupling could resolve the arity mismatch |
This theory comes from flipping the tuplingAllowed bit and watching t8197 flip between compiling and not compiling. |
in my patch (lrytz@5ab7a7b), tupling conversion and default arguments are both checked.
I think this is not what happend; IRC we initially (before pauls commit) checked for both tupling and the use of defaults |
You're right. I was just looking at that:
|
Ok, to avoid regressions at this late stage, I'll incorporate your approach. |
OK, thanks. I'll create a ticket summarizing the current situation and the plan for the future. I also found a number of other bugs while looking at this thing. Here's one that crashes the compiler
|
This commit was co-authored with Lukas. His analysis is below. If there are multiple applicable alternatives, drop those that use default arguments. This is done indirectly by checking applicability based on arity. TODO: this `namesMatch` business is not spec'ed, and is the wrong fix for SI-4592. We should instead clarify what the spec means by "typing each argument with an undefined expected type". What does typing a named argument entail when we don't know what the valid parameter names are? (Since we're doing overload resolution, there are multiple alternatives that can define different names.) Luckily, the next step checks applicability to the individual alternatives, so it knows whether an assignment is: - a valid named argument - a well-typed assignment (which doesn't necessarily have type `Unit`!) - an error (e.g., rhs does not refer to a variable) I propose the following solution (as a TODO): check whether a named argument (when typing it in `doTypedApply`) could be interpreted as an assign; `infer.checkNames` should use the type of the well-typed assignment instead of `Unit`. Lukas's analysis: 990fa04 misunderstood the spec of overloading resolution with defaults. it should not discard applicable alternatives that define defaults, but only those that use defaults in the given invocation. bugs were shadowed because the refactoring used `alt.hasDefault` to check whether the alternative has some parameters with defaults, but this never worked. d5bb19f fixed that part by checking the flags of parameters, which fixed some but but un-shadowed others: ``` object t { def f(x: String = "") = 1; def f(x: Object) = 2 } scala> t.f("") // should return 1, but returns 2 after d5bb19f ``` The commit message of d5bb19f also mentions that its test "should fail to compile according to SI-4728", which is another misunderstanding. ``` class A; class B extends A object t { def f(x: A = null) = 1; def f(x: B*) = 2 } t.f() ``` This should return `2`: both alternatives are applicable, so the one that uses a default is eliminated, and we're left with the vararg one. SI-4728 is different in that it uses explicit parameters, `t.f(new B)` is ambiguous.
Diff with previous commit: --- c/src/compiler/scala/tools/nsc/typechecker/Infer.scala
+++ w/src/compiler/scala/tools/nsc/typechecker/Infer.scala
@@ -582,13 +582,17 @@ trait Infer extends Checkable {
alts exists (alt => isApplicableBasedOnArity(pre memberType alt, argsCount, varargsStar, tuplingAllowed))
case _ =>
val paramsCount = tpe.params.length
+ // simpleMatch implies we're not using defaults
val simpleMatch = paramsCount == argsCount
val varargsTarget = isVarArgsList(tpe.params)
- // vararg parameter lists cannot have default arguments, so we know that a mismatch
- // of paramsCount and argsCount cannot be due to defaults.
+ // varargsMatch implies we're not using defaults, as varargs and defaults are mutually exclusive
def varargsMatch = varargsTarget && (paramsCount - 1) <= argsCount
+ // another reason why auto-tupling is a bad idea: it can hide the use of defaults, so must rule those out explicitly
def tuplingMatch = tuplingAllowed && eligibleForTupleConversion(paramsCount, argsCount, varargsTarget)
+ // varargs and defaults are mutually exclusive, so not using defaults if `varargsTarget`
+ // we're not using defaults if there are (at least as many) arguments as parameters (not using exact match to allow for tupling)
+ def notUsingDefaults = varargsTarget || paramsCount <= argsCount
// A varargs star call, e.g. (x, y:_*) can only match a varargs method
// with the same number of parameters. See SI-5859 for an example of what
@@ -596,7 +600,7 @@ trait Infer extends Checkable {
if (varargsStar)
varargsTarget && simpleMatch
else
- simpleMatch || varargsMatch || tuplingMatch
+ simpleMatch || varargsMatch || (tuplingMatch && notUsingDefaults)
}
private[typechecker] def followApply(tp: Type): Type = tp match {
@@ -1342,9 +1346,10 @@ trait Infer extends Checkable {
case namesMatch if namesMatch.nonEmpty => namesMatch // TODO: this has no basis in the spec, remove!
case _ =>
// If there are multiple applicable alternatives, drop those using default arguments.
- // This is done indirectly by checking applicability based on arity (disallowing tupling to ensure exact arity matching!).
+ // This is done indirectly by checking applicability based on arity in `isApplicableBasedOnArity`.
// If defaults are required in the application, the arities won't match up exactly.
- eligible filter (alt => isApplicableBasedOnArity(alt.tpe, argtpes.length, varargsStar, tuplingAllowed = false))
+ // TODO: should we really allow tupling here?? (If we don't, this is the only call-site with `tuplingAllowed = true`)
+ eligible filter (alt => isApplicableBasedOnArity(alt.tpe, argtpes.length, varargsStar, tuplingAllowed = true))
}
} |
Since the |
LGTM! |
Thanks for all the clarifications! I feel much better about this now. (We have a ways to go, obviously, in aligning spec & implementation, but I'm making that my priority once RC1 is done.) |
Woo-hoo! This will get us to inbox-zero!! 🍻 |
SI-8197 clarify overloading resolution with default args
For some reason 🍻 shows two full steins, but mine is already drained. |
If there are multiple applicable alternatives, drop those
that use default arguments. This is done indirectly by
checking applicability based on arity
(disallowing tupling to ensure exact arity matching!).
If defaults are required in the application, the arities
won't match up exactly.
TODO this
namesMatch
business is not spec'ed, and isthe wrong fix for SI-4592. We should instead clarify what
the spec means by "typing each argument with an undefined expected type".
What does typing a named argument entail when we don't know what
the valid parameter names are? (Since we're doing overload resolution,
there are multiple alternatives that can define different names.)
Luckily, the next step checks applicability to the individual alternatives,
so it knows whether an assignment is:
I propose the following solution (as a TODO):
check whether a named argument (when typing it in
doTypedApply
)could be interpreted as an assign;
infer.checkNames
must not useUnitType
for its type unless it's a legal assignment