Skip to content
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

Null safety feedback: Null check should allow type promotion but doesn't #44327

Closed
suragch opened this issue Nov 27, 2020 · 4 comments
Closed
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@suragch
Copy link
Contributor

suragch commented Nov 27, 2020

I'm upgrading a personal package that is based on the Flutter framework. I noticed here in the Flutter Text widget source code that there is a null check:

if (textSpan != null) {
  properties.add(textSpan!.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
}

However, textSpan! is still using the ! operator. Shouldn't textSpan be promoted to a non-nullable type without having to use the ! operator? However, trying to remove the operator gives the following error:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.

@suragch
Copy link
Contributor Author

suragch commented Nov 27, 2020

Here is another example in my own code:

@override
RotatableString get current {
  if (_currentTextRun == null) {
    throw StateError(
        'Current is undefined before moveNext is called or after last element.');
  }
  return _currentTextRun!;
}

The ! operator is required even though _currentTextRun can't possibly be null.

@eernstg
Copy link
Member

eernstg commented Nov 27, 2020

This is working as intended. Type promotion is only applicable to local variables. _currentTextRun is not local and textSpan may well be non-local as well (the example doesn't show that). Promotion of an instance variable is not sound, because it could be overridden by a getter that runs a computation and returns a different object each time it is invoked. Cf. dart-lang/language#1188 for discussions about a mechanism which is similar to type promotion but based on dynamic checks, with some links to related discussions.

@eernstg eernstg added the closed-as-intended Closed as the reported issue is expected behavior label Nov 27, 2020
@eernstg eernstg closed this as completed Nov 27, 2020
@suragch
Copy link
Contributor Author

suragch commented Nov 27, 2020

@eernstg Thank you for that explanation. I had misunderstood how it worked. That makes sense now.

@PlugFox
Copy link

PlugFox commented May 9, 2021

Example.

https://dartpad.dev/8e1bb623664177623aae58df3c90f046?null_safety=true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants