-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Inferring void for a toplevel is strange #34171
Comments
One good reason for having a variable of type abstract class I<X> {
X get x;
// ...
}
class C implements I<void> {
void x; // Required by `implements I<void>`.
// ...
}
class D implements I<void> {
var x; // Inferred type, due to member signature inference based on `I<void>`.
} I think it would very likely be a good idea to flag (lint?) all variable declarations whose type is |
I'm going to close this and file for a lint, unless anyone objects. |
There's no good way to avoid inferring variables as having type void in general, because of int foo() => 3;
void main() {
var x = foo();
x.length; // error, x has no length field
} |
Yeah, didn't think about how this is what Future.then() does. Obviously its suspect to make language rules where that's allowed for parameters but not locals etc. It threw me off, and I implemented it! But I could have just been having a brain fart. In most languages, A quick guinea pig test shows I'm not the only one that wishes "removed" were flagged, but, a lint should be just fine, and its not like this will cause soundness issues in peoples' code or anything. |
Yeah, |
This is a downside of allowing void-to-void assignment that I don't think we fully considered.
This is not very good locality for the error here. It is extremely unlikely that the user meant the first line. For instance, I ran into this when testing if
List.removeWhere()
returned anIterable
of the removed items, or int count, or just void. I did not get any errors for assigning it to a variable, which I thought ruled out the last case.In fact, it gets worse. In the first example,
y
gets highlighted instead of thex
on the second line! (orx.y
for that matter).This makes sense for
f().y
, but in this example, the highlight + bad locatily + error message combine poorly, and it looks likex
is of some type that has some membery
of type void, and then you are sitting there wondering how that would be possible (a void getter?) and why that would be a problem (to get a void getter?). So the real problem is not obvious.This could probably be covered as part of void_checks in the linter, but should it be? Should it maybe instead be a rule in the language spec, that a variable cannot have the type void inferred?
@eernstg @leafpetersen
The text was updated successfully, but these errors were encountered: