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

[extension types/analyzer] The receiver can't be null false negative on extension type typed values? #56759

Open
modulovalue opened this issue Sep 20, 2024 · 2 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@modulovalue
Copy link
Contributor

Consider:

void main() {
  const Foo a = Foo(0);
  const int b = 0;
 
  a?.toString();
// The receiver can't be null, so the null-aware operator '?.' is unnecessary.
// v
  b?.toString();
}

extension type const Foo(int i) {}

b?.toString(); causes the analyzer to emit a The receiver can't be null, so the null-aware operator '?.' is unnecessary. diagnostic, but it looks to me like a?.toString(); could also be annotated with a similar diagnostic.

@dart-github-bot
Copy link
Collaborator

Summary: The analyzer incorrectly fails to detect that a null-aware operator is unnecessary for an extension type (Foo) when it should, as it does for a regular type (int). This suggests a potential false negative in the analyzer's null-aware operator analysis for extension types.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Sep 20, 2024
@lrhn
Copy link
Member

lrhn commented Sep 21, 2024

It's reasonable. The type of b is int which is a subtype of Object. That guarantees that the value cannot be null.

The type of a is Foo, which is not a subtype of Object, and also not a supertype of Null.

The current representation type of Foo is int, which means that the actual value won't be null, but depending on design, it may be reasonable and non-breaking for the author to change the representation type in a future version, maybe to int?.
The user of the type Foo is not allowed to assume that the value cannot be null, because that would make it breaking for the author to change that.

@lrhn lrhn removed type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

3 participants