Skip to content

Bug: Type checking is broken for enums #52269

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

Closed
MichaelTamm opened this issue May 4, 2023 · 2 comments
Closed

Bug: Type checking is broken for enums #52269

MichaelTamm opened this issue May 4, 2023 · 2 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@MichaelTamm
Copy link

I think, the following code should not compile:

enum A { foo, bar }

enum B { foo, bar, baz }

void main() {
  final x = A.foo;
  print(x == B.foo ? '$x == B.foo' : '$x != B.foo');
}

... because it makes no sense to compare x (an instance of enum A) with an instance of enum B.

Link to the example in DartPad

  • Dart SDK 2.19.6
@lrhn
Copy link
Member

lrhn commented May 4, 2023

It is indeed statically detectable that the an instance of type A will not be equal to an instance of type B.
Because both are enum classes, which means nothing else can implement the types, and enums cannot override ==.

It's not something the language tries to prevent you from doing. After all, it works (it returns false as it should) and the == operator has parameter type Object, so it's perfectly fine to ask.
It's a little weird to make this example code not compile, but allow it if you wrote Object x = A.foo;.

The analyzer could choose to tell you that the comparison is useless. And it does, you just have to ask for it by enabling the
unrelated_type_equality_checks lint.

@lrhn lrhn added the closed-as-intended Closed as the reported issue is expected behavior label May 5, 2023
@lrhn lrhn closed this as completed May 5, 2023
@MichaelTamm
Copy link
Author

Just for reference: The Linter unrelated_type_equality_checks does not work for this example: #58653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants