-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analysis of void return type with arrow causes bad API restrictions #28763
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
My vote would be to allow |
There's also a runtime component to this. The following code: void foo() => bar();
dynamic bar() => 1;
void main() {
foo();
} throws
in checked mode. So in order to make option 1 work, we may also need some sort or rule for the checked-mode VM that it |
@natebosch I also think a hint or nothing is a better option than a warning. |
dart2js, dartium, and vm are likely all impacted in checked mode. DDC might just work as long as the analyzer is OK with it |
(fyi -- I don't think it makes sense to tag other impls until we know what the language team perspective is on changes w.r.t. checked mode. There's already a process in place to go from language team issue -> farm out to implementations.) |
Some thoughts regarding the two options:
@leafpetersen -- language team has discussed "void" treated as "Object" in runtime checks, right? Is there an issue for it? DDC has already implemented it. Once that is available, we could eliminate the error as this suggests. (Currently the error protects against the runtime failure, if I'm understanding correctly.)
Without the above checked mode change, this would be the right approach. My guess, we didn't think through the API evolution implications when |
FYI -- these analyzer changes are quite simple either way. I'm happy to throw together a quick patch once we decide how "void" runtime subtype check is going to work. |
@lrhn is looking at various aspects of our treatment of void from the language perspective. Lasse, will your proposal cover the issues discussed here? |
The current design will allow anything to be assigned to |
Ah that sounds great! Is there an existing issue to get void changed on VM/dart2js in checked mode? If not, can we file them? edit: an issue like this one, for moving Null to bottom? #28024 |
Analyzer side: https://codereview.chromium.org/2699053002 |
I think this is stale/resolved. |
It's resolved: #28939 allows void arrow functions to have a returned expression (its "body") of any type, |
Right now,
void foo() => bar()
is allowed ifbar()
returnsvoid
ordynamic
, and produces an error if it has any other return type. This makes sense on the surface, but it has pretty negative consequences. In particular, it means that it's not safe for an API to add a return type where none previously existed. This is a common refactor that would otherwise be totally backwards-compatible.I can see three solutions to this problem:
bar()
has a return type. If it's valuable information, it could be downgraded to a warning or a hint.=>
in a void context, so that thevoid foo() => bar()
pattern doesn't occur.I strongly prefer option 1, but I'd still choose option 2 over option 3. Under option 3 (the current state of the world), it's likely that API designers won't consider this edge case and will continue to add return types to their void methods, thus potentially breaking downstream users.
The text was updated successfully, but these errors were encountered: