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

Downward inference issue with nullability in analyzer #40022

Closed vsmenon closed 4 years ago

vsmenon commented 4 years ago

The following:

T JS<T extends Object?>(dynamic x) {
  return x as T;
}

main() {
  dynamic x = 42;
  Object y = JS(x);
  print(y);
}

triggers (on Object y = JS(x)):

Couldn't infer type parameter 'T'.  Tried to infer 'Object' for 'T' which doesn't work: Return type declared as 'T' used where 'Object' is required.  Consider passing explicit type arguments to the generic.

and

A value of type 'Object?' can't be assigned to a variable of type 'Object'.

T should be inferred as Object here.

Note, this is coming up in DDC internal dart:* code. Using JS<Object>(...) works around the problem for now.

Hitting this on both http://nullsafety.dartpad.dev/ and an SDK built this week.

bwilkerson commented 4 years ago

@scheglov

scheglov commented 4 years ago

Cannot reproduce locally. I see this issue in DartPad.

scheglov@scheglov-macbookpro2:~/Source/Dart/sdk.git/sdk (master)$ dartanalyzer --enable-experiment=non-nullable /Users/scheglov/dart/test/bin/test.dart
Analyzing /Users/scheglov/dart/test/bin/test.dart...
  lint • Name non-constant identifiers using lowerCamelCase. • /Users/scheglov/dart/test/bin/test.dart:1:3 • non_constant_identifier_names
1 lint found.
scheglov@scheglov-macbookpro2:~/Source/Dart/sdk.git/sdk (master)$ dartanalyzer --version
dartanalyzer version 2.8.0-edge.e2e290b4cfde36c69459f2974d4856dc84e649bc
vsmenon commented 4 years ago

Let me try re-syncing and re-building.

vsmenon commented 4 years ago

This is fixed at head. Sorry for the noise.