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

[dart2wasm] Getting illegal cast, only when compiled with -O0 #55341

Closed
eyebrowsoffire opened this issue Mar 29, 2024 · 5 comments
Closed

[dart2wasm] Getting illegal cast, only when compiled with -O0 #55341

eyebrowsoffire opened this issue Mar 29, 2024 · 5 comments
Labels
area-dart2wasm Issues for the dart2wasm compiler. cfe-dysfunctionalities Issues for the CFE not behaving as intended cfe-encodings Encoding related CFE issues.

Comments

@eyebrowsoffire
Copy link
Contributor

This is a minimum reproducible case, distilled from some code in the flutter framework that was causing this problem.

Steps to reproduce:

(1) Compile the following code with dart compile wasm -O0 --extra-compiler-option=--enable-asserts:

void main() {
  final Map<String, Map<String, String>> nestedMap = <String, Map<String, String>>{};
  nestedMap?['hello'] ??= <String, String>{};
  nestedMap?['hello']!['world'] = 'foo';

  print('map: $nestedMap');
}

bool get isDebug => !bool.fromEnvironment('dart.vm.product') && !bool.fromEnvironment('dart.vm.profile');

Map<String, Map<String, String>>? getNestedMap() {
  return isDebug ? <String, Map<String, String>>{} : null;
}

(2) Instantiate and run the output in a browser.

Results:
This crashes, with the following appearing in console:

assert_text.wasm:0xbb2e Uncaught RuntimeError: illegal cast
    at main (assert_text.wasm:0xbb2e)
    at main tear-off trampoline (assert_text.wasm:0xbb87)
    at _invokeMain (assert_text.wasm:0xbd73)
    at invoke (assert_text.mjs:227:28)
    at (index):7:9

Note: This seems to not occur when running at -O1 or higher, so something in the wasm-opt transformations somehow smooths out whatever issue is occurring here.

@lrhn lrhn added the area-dart2wasm Issues for the dart2wasm compiler. label Mar 30, 2024
@osa1
Copy link
Member

osa1 commented Apr 3, 2024

Slightly smaller repro:

void main() {
  final Map<String, Map<String, String>> nestedMap = <String, Map<String, String>>{};
  nestedMap?['hello'] ??= <String, String>{};
  print('map: $nestedMap');
}

Kernel: (simplified)

static method main() → void {
  final Map<String, Map<String, String>> nestedMap = <String, Map<String, String>>{};

  let final Map<String, Map<String, String>> #t1 = nestedMap in
    #t1 == null
      ?{Map<String, String>?} null
      : let final String #t2 = "hello" in
          #t1.{Map::[]}(#t2){(Object?) → Map<String, String>?} == null
            ?{Map<String, String>} #t1.{Map::[]=}(#t2, <String, String>{}){(String, Map<String, String>) → void}
            : null;

  print("map: ${nestedMap}");
}

When compiling conditional with the Map.[] call we try to cast the return type to ref Object because that's the expectedType argument passed to the compiler (I don't know when compiling which library function), but the else branch returns null.

@johnniwinther
Copy link
Member

I think we set the static type of the last conditional expression wrongly. From the language perspective it is correct that the whole expression is known to return Map<String, String> but since we see that the value is not used we use null as the false case of the conditional expression. We should either not optimize for this case (and use the value of nestedMap['hello'] as the result) or update the static type of the ConditionalExpression to be nullable.

@chloestefantsova What do you think is the best approach?

@johnniwinther johnniwinther added cfe-dysfunctionalities Issues for the CFE not behaving as intended cfe-encodings Encoding related CFE issues. labels Apr 3, 2024
@chloestefantsova
Copy link
Contributor

@chloestefantsova What do you think is the best approach?

Intuitively it feels that adjusting the type of the conditional expression should be an easier route. Let me look into it a bit more close.

@chloestefantsova
Copy link
Contributor

I'm experimenting with a potential solution at https://dart-review.googlesource.com/c/sdk/+/360865.

@akalankavinda
Copy link

akalankavinda commented May 11, 2024

Hello.
I am simply trying to pass a string to dart function.

@pragma("wasm:export")
void onKeyup(String elemntId) {
print(elemntId);
}

but it fails with the following error on the browser.
image

can you suggest me a fix for this please ?

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. cfe-dysfunctionalities Issues for the CFE not behaving as intended cfe-encodings Encoding related CFE issues.
Projects
None yet
Development

No branches or pull requests

6 participants