Skip to content
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

[ddc] switch case doesn't match enum when enum comes from a different library and has a final field that's not initialized via constructor #59939

Open
denniskaselow opened this issue Jan 18, 2025 · 0 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler

Comments

@denniskaselow
Copy link

denniskaselow commented Jan 18, 2025

This is a very niche and unusual case with an easy workaround, but I still wanted to report it.

Running the following code prints this should NOT happen when compiled with ddc:

// main.dart
import 'package:switch_case_enum_bug/my_enum.dart';

void main() {
  final value = MyEnum.a;
  print(switch (value) {
    MyEnum.a => 'this should happen',
    _ => 'this should NOT happen',
  });
}

// my_enum.dart
enum MyEnum {
  a;
  final someText = 'Hello World';
}

It only happens if the enum is defined in a separate library (with main.dart in the web directory and my_enum.dart in the lib directory). The generated code looks like this:

  dart.defineLazy(CT, {
    get C0() {
      return C[0] = dart.const(Object.setPrototypeOf({
        [_Enum__name]: "a",
        [_Enum_index]: 0,
        someText: null
      }, my_enum.MyEnum.prototype));
    }
  });

someText: null seems to be the problem. It's correctly initialized to Hello World if the enum is in the same library or if it's initialized via constructor.

A simple workaround is to initialize the field in the constructor or turning it into a getter. (I've only run into this issue because my actual enum implements an interface that has a field that I'm not yet using)

tested with:
Dart SDK version: 3.7.0-309.0.dev (dev) (Tue Jan 7 20:06:23 2025 -0800) on "windows_x64"
Dart SDK version: 3.7.0-323.0.dev (dev) (Mon Jan 13 04:02:13 2025 -0800) on "windows_x64"

@denniskaselow denniskaselow added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jan 18, 2025
@lrhn lrhn added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler labels Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler
Projects
None yet
Development

No branches or pull requests

2 participants