Skip to content

Type unsafety of ?? #465

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
duzenko opened this issue Jul 22, 2019 · 8 comments
Closed

Type unsafety of ?? #465

duzenko opened this issue Jul 22, 2019 · 8 comments

Comments

@duzenko
Copy link

duzenko commented Jul 22, 2019

Consider the following code

    num n = 1;
    String s = n ?? List();

It gives an obvious runtime error.
I'm not so much interested in why it's considered a valid syntax as in getting the analyzer report this at compile time. I have bumped into this a few times too many.

@srawlins
Copy link
Member

I suspect this is an implicit cast. The same thing could be done with:

num n = 1;
String s = n == null ? List() : n;

The type of the right-hand-side is the Least Upper Bound of List<dynamic> and num, which is Object. So an Object is being assigned to a String, via an implicit downcast. See dart-lang/sdk#31410 for some history. This is going away with the Non-nullable type system. You can also ban it from your codebase with implicit-casts: false in your analysis_options.yaml.

@duzenko
Copy link
Author

duzenko commented Jul 22, 2019

I suspect this is an implicit cast. The same thing could be done with:

num n = 1;
String s = n == null ? List() : n;

The type of the right-hand-side is the Least Upper Bound of List<dynamic> and num, which is Object. So an Object is being assigned to a String, via an implicit downcast. See dart-lang/sdk#31410 for some history. This is going away with the Non-nullable type system. You can also ban it from your codebase with implicit-casts: false in your analysis_options.yaml.

Sorry, I think implicit-casts: false is a bit of an overreaction to such a localized issue.
I don't have a problem with implicit downcast per se. It's about implicit upcast/downcast combination that the compiler is doing to implement this single operator.
And FWIW I would like the same analyzer hint coming up with ?: as well.

@munificent
Copy link
Member

Closing this because it will get addressed by NNBD which removes implicit downcasts.

@natebosch
Copy link
Member

Closing this because it will get addressed by NNBD which removes implicit downcasts.

This case won't be fixed assuming we continue to allow implicit downcast from dynamic.

#192 (comment)

@duzenko
Copy link
Author

duzenko commented Feb 27, 2020

reopen?

@natebosch
Copy link
Member

Hmm, trying to repro I'm seeing that the type of n ?? List() is Object and not dynamic like I thought, so this case will indeed be solved.

@leafpetersen
Copy link
Member

This is mostly solved. The case that isn't solved is if one of elements of the lub is dynamic.

void main() {
  dynamic y;
  int x = y ?? "hello";
}

@duzenko
Copy link
Author

duzenko commented Feb 28, 2020

No, dynamic is not a problem

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

5 participants