Skip to content

Nullability promotion after bool field access in an if with a coalesce #2295

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
frmrgr opened this issue Jun 13, 2022 · 2 comments
Closed

Nullability promotion after bool field access in an if with a coalesce #2295

frmrgr opened this issue Jun 13, 2022 · 2 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@frmrgr
Copy link

frmrgr commented Jun 13, 2022

It would be great if the analyzer was able to promote a type after a bool is accessed on a nullable reference and defaulted.

Example:

    void foo(String? s) {
      if (s?.isNotEmpty ?? false) {
-       print(s!.substring(0, 2));
+       print(s.substring(0, 2));
      }
    }

It can already handle longer version well:

    void foo(String? s) {
      if (s != null ? s.isNotEmpty : false) {
       print(s.substring(0, 2));
      }
    }

Not very important, but would be nice.

Dart SDK 2.17.1

@eernstg
Copy link
Member

eernstg commented Jun 13, 2022

I don't think this is a duplicate of #1224, but there is definitely a lot of similarity. I don't know if it would be useful to somehow merge the two.

One thing that makes this issue different (and tricky) is that it depends of knowing that s?.isNotEmpty must have evaluated to true if the whole if-null expression s?.isNotEmpty ?? false yields true, and that's a property of _ ?? false which is not shared by an expression of the form _ ?? b where b is a boolean expression that turns out to yield false at run-time. We must be able to prove that the value is false already at compile time. So would we expect s?.isNotEmpty ?? (v && !v) to promote s when v is a local variable of type bool (because v && !v is false when v is false and also when v is true)? How much magic would we expect to have, in order to determine that the right operand of ?? yields false? It might work just fine to recognize the literal false and only that, but it may then seem silly that s?.isNotEmpty ?? (false) "doesn't work".

@frmrgr
Copy link
Author

frmrgr commented Jun 13, 2022

I think it's a dupe, sorry I hadn't found the earlier one.

Regarding the comment, I would not expect the analyzer to do algebra and it is good that if (s != null ? s.isNotEmpty : (v && !v)) doesn't work. I included if (s != null ? s.isNotEmpty : false) in the OP because I assume that what ?? is a syntactic sugar for. But that all has been addressed in the ticket you linked.

@vsmenon vsmenon transferred this issue from dart-lang/sdk Jun 14, 2022
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Jun 14, 2022
@lrhn lrhn closed this as completed Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants