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

Analyzer const cast fails for Never?. #54820

Open
lrhn opened this issue Feb 4, 2024 · 5 comments
Open

Analyzer const cast fails for Never?. #54820

lrhn opened this issue Feb 4, 2024 · 5 comments
Labels
analyzer-constants area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@lrhn
Copy link
Member

lrhn commented Feb 4, 2024

Example:

  const v = null as Never?;

The analyzer reports a compile-time error for this:

line 3 • Evaluation of this constant expression throws an exception.

Doesn't say which exception, and it shouldn't since null is a valid value of any nullable type.
(Doesn't check type for nullability?)

@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-constants type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Feb 4, 2024
@bwilkerson
Copy link
Member

I'm guessing that Never? means that it will either return null or never return. There are likely other places in the analyzer / linter where Never? isn't being handled correctly. For example, the code

Never? f() => null;

results in the lint Unnecessary use of the type 'Null'. Try using 'void' instead.. This seems like a poor message, at the very least.

@scheglov @pq

@bwilkerson bwilkerson added the P2 A bug or feature request we're likely to work on label Feb 5, 2024
@pq
Copy link
Member

pq commented Feb 5, 2024

I'm inclined to agree that at least prefer_void_to_null is overreporting.

Issue filed: #59388

@eernstg
Copy link
Member

eernstg commented Feb 5, 2024

We've got this one as well: #59309.

@pq
Copy link
Member

pq commented Feb 5, 2024

Yes, thanks @eernstg!

@lrhn
Copy link
Member Author

lrhn commented Feb 5, 2024

I'm guessing that Never? means that it will either return null or never return.

Or throw, just like Null.
The type Never? is NORM-equivalent to Null, which means that it should behave exactly the same in most contexts (likely all contexts for this type, a few other types have a few exceptions where the pre-normalization type matters).

It's definitely true that Never? <: Null and Null <: Never?, and their Type objects are equal, and identical if const:

typedef NeverQ = Never?;
void main() { 
  assert(Null == NeverQ);
  const t1 = Null; 
  const t2 = NeverQ;
  assert(identical(t1, t2));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-constants area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants