Skip to content

Null Aware ?? loose type check. #29607

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

Closed
kelegorm opened this issue May 12, 2017 · 1 comment
Closed

Null Aware ?? loose type check. #29607

kelegorm opened this issue May 12, 2017 · 1 comment

Comments

@kelegorm
Copy link

kelegorm commented May 12, 2017

Next code doesn't show warning, it should be type error:

int b;
int a = b ?? '';
a;

Dart 1.20.1

@eernstg
Copy link
Member

eernstg commented May 15, 2017

This is a real problem. The corresponding situation with the conditional operator :? is discussed in #25368, and that issue already mentions that ?? is similar.

The core problem is the drastic upcast in the ?? operator: the static type of e1 ?? e2 is the least upper bound of the static type of e1 and of e2. Whenever those two types are unrelated (like here) we get Object, and that allows for a downcast (in this case to int, but we can downcast to anything from Object). The consequence is that the initialization of a is accepted in all cases, as if the type check had been turned off completely.

I'll close this as a duplicate of #25368, adding a reference to this issue to emphasize that the problem affects ?? as well as ?:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants