dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.57k forks source link

[enhanced-enums] A value declaration is a constant context #48232

Open eernstg opened 2 years ago

eernstg commented 2 years ago

Consider the following program:

enum E {
    v([]); // No error.
  const E(_);
}

The program uses --enable-experiment=enhanced-enums. It does not have any errors, in particular, [] can be used as an actual argument because the spec has:

where args are considered as occurring in a const context, ...

However, the CFE reports an error at [] (two, actually):

n017.dart:2:7: Error: Constant expression expected.
Try inserting 'const'.
    v([]);
      ^
n017.dart:2:7: Error: Non-constant list literal is not a constant expression.
    v([]);
      ^

The analyzer rejects the program as follows:

ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER|/usr/local/google/home/eernst/lang/dart/scratch/202201/n017.dart|3|9|1|A constant constructor can't call a non-constant super constructor of 'Enum'.

which means that the analyzer may or may not have the same fault, but we don't get to see it because we encounter an error with the Enum constructor (which could be a fault in the analyzer, or in the declaration of Enum, or both).

So we have at least one platform where there is a need to make this change: An enum value declaration is a constant context.


Subtasks:

eernstg commented 2 years ago

@scheglov, do you agree that the CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER issue that seems to affect the analysis of every enhanced enum declaration could mask errors on the enum members? Is it expected to be resolved soon?

scheglov commented 2 years ago

Yes, I agree. https://dart-review.googlesource.com/c/sdk/+/230321 should fix this.

eernstg commented 2 years ago

Thanks! I added https://github.com/dart-lang/sdk/issues/48237 just in case there is a need to change the analyzer as well.