-
Notifications
You must be signed in to change notification settings - Fork 1.7k
analyzer doesn't trust the "is" operator enough #28586
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
Sect. '16.34 Type Test' in the spec says 'Let v be a local variable or a formal parameter. An is-expression of the form So the problem is that We could allow for this loss of information (and rename the feature to something like 're-typing' rather than 'type promotion'), or we could introduce intersection types (such that we could know that We are working on generalizations of the type promotion mechanism, and I listed this issue as one of the subtopics that we'd address when doing that. With that, I'll close this issue as a duplicate of #18921 (which is not quite true, but that's an old issue which deals with the overall topic of improved promotion, and the list of issues that I created for the language team includes 12 issues in the area, so we won't forget them). |
Oops, forgot to mention this workaround which might be useful—uperform an upcast before promoting: class A { }
class B { }
class X extends A implements B { }
B test(A o) {
Object x = o;
if (x is B)
return x;
return null;
}
void main() {
A a = new X();
print(test(a));
} We can only promote variables, not expressions (there's another enhancement to promotion that we are looking at ;-), so we have to introduce the variable |
I think the observation is that in this case we really don't care that The question I would ask is: for all of the is-tests of this form (where the type in the expression is not more specific than the static type of the variable), what percentage of the time is the variable used inside the promotion region as if it also had it's original static type (that is, the variable is treated as if it had an intersection type). Or rather, ask whether it would have been treated that way had it been possible to do so. |
Performing 're-typing' rather than 'type promotion' in some cases (and hence getting type As I mentioned, this issue is linked from the language-team issue about enhanced promotion that I created earlier today, and it says that the linked issues (open as well as closed ones) should be used as sources of information about what's needed, so your comments are included. |
Based on past experience, I'd hesitate to use intersection types if users can't express them syntactically. But mostly I was suggesting that we could get data to use when making the decision :-) |
This code is correct - on the line with the error, we can prove that
o
is aB
, since it wouldn't get there otherwise.For now we're working around this with
as dynamic
on thereturn
line, but that's ugly and loses type checking.IMHO the analyzer should be able to track that a particular variable is known to implement multiple interfaces at particular points in the code flow.
cc @abarth who just ran into this.
The text was updated successfully, but these errors were encountered: