dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

"? ... : null" is not inferred as a nullable type #39437

Closed munificent closed 4 years ago

munificent commented 4 years ago

Take this program:

bool b = false;
int i = b ? 123 : null;

int test() {
  return b ? 123 : null;
}

And analyze it with:

dartanalyzer --enable-experiment=non-nullable temp.dart

I would expect two errors, because the result of ?: is nullable and it is being used in non-nullable contexts. Instead, I get:

No issues found!
stereotype441 commented 4 years ago

It looks like the analyzer's current implementation of LUB thinks that the LUB of types int and Null is int (which was correct pre-NNBD but is wrong under the new NNBD rules).

CC @scheglov who is currently working on improvements to LUB

stereotype441 commented 4 years ago

@scheglov confirmed that under the new rules he's implementing, LUB(int, Null) will be int?, so this should work fine once that work is done.

scheglov commented 4 years ago

Fixed in 6444fba9c71d5125799afe5798afb6f26a110b43. image

munificent commented 4 years ago

Thanks!