Skip to content

Enhanced enums: dart throws unexpected compile error for the declaration of enum with factory. #48181

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
iarkh opened this issue Jan 20, 2022 · 4 comments
Assignees
Labels
legacy-area-front-end Legacy: Use area-dart-model instead.

Comments

@iarkh
Copy link
Contributor

iarkh commented Jan 20, 2022

Dart SDK version: 2.17.0-28.0.dev (dev) (Tue Jan 18 12:05:00 2022 -0800) on "windows_x64"

Dart produces a compile time error for the following source code example:

// SharedOptions=--enable-experiment=enhanced-enums
enum E {
  e1,
  e2;
  factory E.f(int i) => E.values[i];
}

main() {}

Seems like there should not be an error here.

Sample output is:

$> dart --enable-experiment=enhanced-enums test.dart
test.dart:2:6: Error: Constant evaluation error:
enum E {
     ^
test.dart:2:6: Context: Expected constant 'null' to be of type 'E', but was of type 'Null'.
 - 'E' is from 'test.dart'.
enum E {
     ^
test.dart:2:6: Context: While analyzing:
enum E {
     ^
@mraleph mraleph added the legacy-area-front-end Legacy: Use area-dart-model instead. label Jan 24, 2022
@mraleph
Copy link
Member

mraleph commented Jan 24, 2022

/cc @chloestefantsova

@eernstg
Copy link
Member

eernstg commented Jan 24, 2022

@lrhn, is it intended that a constructor const E(); will be generated in an enum declaration E in the case where E contains a factory constructor (redirecting or not)? This seems inconsistent with the treatment of a similar situation for classes:

class C {
  factory C.fac() = D;
}

class D implements C {
  D();
}

void main() {
  C(); // Error, there is no constructor named `C`.
}

So I would think that there should be an error in the original example, because it needs to invoke a constructor named E, and there is no such thing, but the spec seems to disagree.

@chloestefantsova
Copy link
Contributor

I'm working on a fix (https://dart-review.googlesource.com/c/sdk/+/229540) according to the current spec.

@lrhn
Copy link
Member

lrhn commented Jan 25, 2022

The design document does indeed say:

If no generative constructors were declared, and no unnamed factory constructor was added, a default generative constructor const Name._$(this.index, this._$name); is added.

So, yes, it's intended.

There might be reason for not wanting any generative constructors on normal classes. There is never a reason to not have a generative constructor for an enum class, because it must have at least one enum element that must be constructable.

Adding a factory constructor does not change that, so unless it actually uses the unnamed constructor name, we'll always add the default constructor.

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

No branches or pull requests

5 participants