Skip to content

[Enhanced enums] no error in CFE on class/mixin which has Enum as a superclass and declares values #48388

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
sgrekhov opened this issue Feb 14, 2022 · 1 comment
Assignees
Labels
front-end-missing-error legacy-area-front-end Legacy: Use area-dart-model instead.

Comments

@sgrekhov
Copy link
Contributor

According to the Enhanced Enums specification

It's a compile-time error if a class or mixin declaration has Enum as a superinterface and the interface of the declarations contains an instance member with the name values, whether declared or inherited. If any concrete class implements this interface, it will be an enum declaration class, and then the values member would conflict with the static values constant getter that is automatically added to enum declaration classes. Such an instance values declaration is either useless or wrong, so we disallow it entirely.

Now there is no expected error in CFE in the folllowing cases

mixin M1 on Enum {
  final int values = 42;
//          ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

mixin M2 on Enum {
  final List values = [];
//           ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

abstract class E1 extends Enum {
  int values() => 42;
//    ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

abstract class E2 extends Enum {
  List<E2> values() => [];
//         ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

It is clearly said in the specification '... class or mixin declaration has Enum as a superinterface and the interface of the declarations contains an instance member with the name values ...'. So, I believe, CFE should report a compile-time error like analyzer does.

cc @eernstg to confirm

@sgrekhov sgrekhov added the legacy-area-front-end Legacy: Use area-dart-model instead. label Feb 14, 2022
@sgrekhov sgrekhov changed the title [Enhanced enums] no error in CFE on class/mixin which nas Enum as a superclass and declares values [Enhanced enums] no error in CFE on class/mixin which has Enum as a superclass and declares values Feb 14, 2022
@eernstg
Copy link
Member

eernstg commented Feb 14, 2022

Right, we should have these errors after all. We actually have two different paragraphs about this topic:

It's a compile-time error if the enum declaration contains a static or instance
member declaration with the name values, or if the superclass or
any superinterface of the enum declaration has an interface member named
values.

which occurs just before section 'Semantics', and the paragraph that you mentioned:

It's a compile-time error if a class or mixin declaration has Enum as a superinterface
and the interface of the declarations contains an instance member with the name
values, whether declared or inherited.

I saw the first one, but overlooked the second one.

The first one is concerned with errors that are reported during the static analysis of an enum declaration, and it catches both instance and static declarations in the enum declaration, and instance declarations in any superinterface. The second one applies to any class/mixin declaration that has Enum as a superinterface, and it only catches instance declarations.

So we can have a static values in any superinterface of an enum (including the mixins mentioned in this issue) without errors. An instance member named values can occur in a mixin/class A without error as long as A does not have Enum as a superinterface, but if A is used as a superinterface of an enum then that enum gets an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
front-end-missing-error legacy-area-front-end Legacy: Use area-dart-model instead.
Projects
None yet
Development

No branches or pull requests

3 participants