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

Illegal cast in switch statement for dynamic values on WASM #59782

Open
thomasostfeld opened this issue Dec 20, 2024 · 1 comment · May be fixed by simpleclub/math_keyboard#73
Open

Illegal cast in switch statement for dynamic values on WASM #59782

thomasostfeld opened this issue Dec 20, 2024 · 1 comment · May be fixed by simpleclub/math_keyboard#73
Labels
area-dart2wasm Issues for the dart2wasm compiler. 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)

Comments

@thomasostfeld
Copy link

A switch statement on a dynamic value tries to cast the value to a type, when all (non-default) cases are matching that type.

Reproducable Code:

void main() {
  dynamic number = 42;
  switch (number) {
    case 'a':
      print('a');
    case 'b':
      print('b');
    default:
      print('default');
  }
}

This code works on web with JS, but fails when running with --wasm.

Expected behaviour:

prints out default.

Actual behaviour:

Uncaught RuntimeError: illegal cast

When adding a case of any other type than String it works.

I thought this would be fixed by #56321, but this is still happening.

dart --version output:

Dart SDK version: 3.6.0 (stable) (Thu Dec 5 07:46:24 2024 -0800) on "macos_arm64"
@thomasostfeld thomasostfeld added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Dec 20, 2024
@lrhn
Copy link
Member

lrhn commented Dec 20, 2024

For refutable patterns, the static type of the matched value shouldn't matter, so something is being cast unnecessarily here. (And for these patterns, the type shouldn't matter at all, they're equivalent to 'a' == $v which works for any type of object.

The only case where the static matched value type being dynamic should mater for a refutable pattern is when you use the relational operators, like case == expr or case < expr, where the operator invocation is a dynamic invocation and its result is subject to implicit downcast to bool (which doesn't really matter for ==).
And bound variables like var x could get the type dynamic, but that doesn't matter inside the pattern.

@lrhn lrhn added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-dart2wasm Issues for the dart2wasm compiler. labels Dec 20, 2024
@thomasostfeld thomasostfeld linked a pull request Dec 22, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart2wasm Issues for the dart2wasm compiler. 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)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants