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

Can an NNBD library catch dynamic? #916

Closed
munificent opened this issue Apr 7, 2020 · 2 comments
Closed

Can an NNBD library catch dynamic? #916

munificent opened this issue Apr 7, 2020 · 2 comments
Labels
nnbd NNBD related issues question Further information is requested

Comments

@munificent
Copy link
Member

munificent commented Apr 7, 2020

As I was migrating language_2/exception/try_catch_optimized5_test.dart, I ran into an interesting failure. Given:

main() {
  try {
    throw "ok";
  } on dynamic catch (error) {
    print(error);
  }
}

It runs fine in the VM:

$ dart --null-safety --enable-experiment=non-nullable temp.dart
ok

But analyzer does not like it:

$ dartanalyzer --enable-experiment=non-nullable temp.dart
Analyzing temp.dart...
  error • A nullable type can't be used in an 'on' clause because it isn't valid to throw 'null'. • temp.dart:4:8 • nullable_type_in_catch_clause
1 error found.

I don't see anything in the NNBD spec related to catching non-nullable types so I don't know which tool is behavior correctly. My hunch is that we probably do want to treat dynamic specially and allow that in a catch clause even though it's technically nullable?

A related question is around type parameters:

main<T>() {
  try {
    throw "ok";
  } on T catch (error) {
    print(error);
  }
}

Analyzer currently considers that an error, but does allow:

main<T extends Object>() {
  try {
    throw "ok";
  } on T catch (error) {
    print(error);
  }
}

cc @stereotype441 @scheglov @johnniwinther

@munificent munificent added question Further information is requested nnbd NNBD related issues labels Apr 7, 2020
@leafpetersen
Copy link
Member

The type of the on clause may be nullable. We discussed this here with the decision to allow it. The spec was landed here in that state, but an interim draft specified an error for potentially nullable on clauses, which is perhaps where the analyzer error originates.

@munificent
Copy link
Member Author

OK, thanks! Filed dart-lang/sdk#41378 to track fixing analyzer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nnbd NNBD related issues question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants