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

Analyzer extension method resolution is incorrect for nullable types #40931

Closed leafpetersen closed 4 years ago

leafpetersen commented 4 years ago

The following program resolves an extension on T? to an int? receiver, which should result in T being resolved to int, and hence the return type of foo being int:

extension Test<T> on T? {
  T get foo => this!;
}

void main() {
  int? a;
  a.foo.isEven;
}

The analyzer currently emits an incorrect error on this indicating that the return type of foo is nullable.

leafp-macbookpro:tmp leafp$ /Users/leafp/src/dart-repo/sdk/xcodebuild/ReleaseX64NNBD/dart-sdk/bin/dartanalyzer --enable-experiment=non-nullable ~/tmp/test4.dart
Analyzing /Users/leafp/tmp/test4.dart...
  error • The expression is nullable and must be null-checked before it can be used. • test4.dart:26:3 • unchecked_use_of_nullable_value

cc @scheglov

scheglov commented 4 years ago

https://dart-review.googlesource.com/c/sdk/+/138790

Well, method body inference is not specified, so I don't know for sure if I'm doing it right. But this CL fixes the test case.