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

NNBD. Specify error message for 'throw e' if e is not assignable to 'Object' #39631

Closed sgrekhov closed 4 years ago

sgrekhov commented 4 years ago

According to the NNBD specification

It is an error if the static type of e in the expression throw e is not assignable to Object.

There is an error message, but not specific. For example

class A {
}
void test1(Null x) {
  throw x; //   error - This expression is invalid as it will always be null. - static_errors_A15_t01.dart:69:9 - invalid_use_of_null_value
}
void test2(A? x) {
  throw x; //   error - The expression is nullable and must be null-checked before it can be used. - static_errors_A15_t01.dart:73:9 - unchecked_use_of_nullable_value
}
void test3<T extends Null>(T x) {
  throw x; //   error - The expression is nullable and must be null-checked before it can be used. - static_errors_A15_t01.dart:77:9 - unchecked_use_of_nullable_value
}
void test4<T extends Object?>(T x) {
  throw x; //   error - The expression is nullable and must be null-checked before it can be used. - static_errors_A15_t01.dart:81:9 - unchecked_use_of_nullable_value
}

main() {
}

I'm not 100% sure, but analyzer error messages could be more specific in this case

dartanalyzer version 2.7.0-dev.2.1

scheglov commented 4 years ago

Hm... The message could be different, e.g. saying that the expression is potentially nullable, but this does not change much.

"assignable to Object" means "non-nullable", so "not assignable to Object" means "not non-nullable", which is the definition of "potentially nullable".

So, I'm not sure we can do much better here, so no action for now.