Skip to content

[analyzer] Soundness issue with typing of equality expressions #54083

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

Open
eernstg opened this issue Nov 17, 2023 · 3 comments
Open

[analyzer] Soundness issue with typing of equality expressions #54083

eernstg opened this issue Nov 17, 2023 · 3 comments
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. dart-model-analyzer-spec Issues with the analyzer's implementation of the language spec P3 A lower priority bug or feature request soundness type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@eernstg
Copy link
Member

eernstg commented Nov 17, 2023

Consider the following example, based on this comment:

class NeverEqual {
  Never operator ==(Object? o) => throw 0;
}

void main() {
  NeverEqual? neverEqual = NeverEqual() as dynamic, other;
  Object? o = true;

  if (o is! int) neverEqual == other; // Incorrect to assume that this throws.
  print(o.toRadixString(16)); // Unsound.
}

This shows that it is unsound to assume that the type of an <equalityExpression> is Never, even in the case where the corresponding declaration of operator == has return type Never. Equality expressions should always have the type bool.

However, the analyzer (DartPad based on Dart SDK 3.3.0-16.0.dev) does not report any errors on the line marked 'Unsound', which implies that it considers neverEqual == other to be an expression that cannot complete normally.

@lrhn
Copy link
Member

lrhn commented Nov 18, 2023

It's worth mentioning that this cannot affect constant evaluation, since constant == is only allowed with primitive equality, not anything that calls a user written implementation. So it will only be incorrect typing of code that will never be executed at that type. (So technically it's not "unsound", there is no expression which is evaluated to a type which isn't a subtype of its static type. It's just not correct, it doesn't match the compiler behavior.)

@eernstg
Copy link
Member Author

eernstg commented Nov 18, 2023

Well, it's an analyzer issue. If the CFE had had the same static analysis then it would certainly have been a soundness issue: It will attempt to call toRadixString on true.

@srawlins srawlins added the P3 A lower priority bug or feature request label Nov 20, 2023
@eernstg
Copy link
Member Author

eernstg commented Jan 3, 2024

@srawlins, I just tried the example, and it looks like it has been resolved already?

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Mar 28, 2024
@bwilkerson bwilkerson added area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. dart-model-analyzer-spec Issues with the analyzer's implementation of the language spec P3 A lower priority bug or feature request soundness type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants