-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Specify treatment of missing type arguments in redirecting factory constructors #32049
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
Comments
No milestone now: This will not block Dart 2. |
Note that we already have the following in
This implies that the static checks applied to the invocation of the redirecting factory constructor This makes it a natural generalization (when the language specification is updated to cover inference) that inference of type arguments for So the current language specification already says what it should say in order to "generalize naturally" to do what this issue requests. Hence, this issue exists simply as a reminder to double-check that the update to include inference does indeed satisfy this expectation. |
The current specification seems to allow (or even mandate) that a redirecting constructor (factory or generative) must be considered as a syntactic shorthand for an invocation of the redirection target, with no checks applied statically or dynamically according to the declarations in the redirected constructor. So the following would be OK: class A { A(num n) { print(n); } A.foo(int i): this(i); } main() => new A.foo(3.0); This CL changes dartLangSpec.tex to mandate all the checks (static and dynamic) for the declaration of the redirecting constructor as well as each one of the redirection targets. Note that the analyzer already rejects the above program, which lessens the disruption and the implementation burden, but compilers would presumably need to have the dynamic checks implemented. Underlying issues: #31590, #32049, #32511. Change-Id: Icc15da6b817e4e678cdfc8829a1e06458756eb4b Reviewed-on: https://dart-review.googlesource.com/28140 Reviewed-by: Leaf Petersen <[email protected]> Reviewed-by: Lasse R.H. Nielsen <[email protected]>
Note the following example: #32988 (comment). It shows how the current treatment allows for a heap invariant violation ( So that's a case where the situation addressed in this issue plays a major role: If that type argument had been chosen by inference then the code could have been in good shape (using |
I think we should specify that we perform inference on the redirectee, if necessary. In almost all cases, it will be a simple copying over of the type arguments. In a few cases, there will be more complicated situations, but no more complicated than if we had just written If we do not infer, then it must be a compile-time error if a constructor does not guarantee that the created object is a subtype of the constructed class's type. |
The current specification seems to allow (or even mandate) that a redirecting constructor (factory or generative) must be considered as a syntactic shorthand for an invocation of the redirection target, with no checks applied statically or dynamically according to the declarations in the redirected constructor. So the following would be OK: class A { A(num n) { print(n); } A.foo(int i): this(i); } main() => new A.foo(3.0); This CL changes dartLangSpec.tex to mandate all the checks (static and dynamic) for the declaration of the redirecting constructor as well as each one of the redirection targets. Note that the analyzer already rejects the above program, which lessens the disruption and the implementation burden, but compilers would presumably need to have the dynamic checks implemented. Underlying issues: dart-lang/sdk#31590, dart-lang/sdk#32049, dart-lang/sdk#32511. Change-Id: Icc15da6b817e4e678cdfc8829a1e06458756eb4b Reviewed-on: https://dart-review.googlesource.com/28140 Reviewed-by: Leaf Petersen <[email protected]> Reviewed-by: Lasse R.H. Nielsen <[email protected]>
With 9282a34 that is indeed a compile-time error (and the spec currently always assumes that inference has already taken place). |
Closing: We've decided that we will add a specification of inference (of any kind) in a later edition of the language specification, and until then the specification will express everything under the assumption that inference has taken place already. This means that this issue can be closed: Inference for this case will be specified elsewhere (than dartLangSpec.tex), and if there is no result from inference then we will use instantiate to bound, which is handled in instantiate-to-bound.md, which is being integrated into the language specification in a separate effort. |
Currently we do not specify explicitly how to deal with the case where a redirecting factory constructor omits actual type arguments from the redirectee:
This issue is intended to ensure that the specification treats the above class
A
in the same way as the following variant:for which we already have a well-established inference process. Note that this approach was discussed in #30855 first, this issue just exists in order to focus on the specification task which is not the main topic of that issue.
This may be added to dartLangSpec.tex when we specify instantiate-to-bound in general, or it may be added in some feature spec before that (but we don't currently have any feature specs where it fits naturally).
The text was updated successfully, but these errors were encountered: