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

Erroneous RangeError when compiling to WASM #55817

Closed
Rexios80 opened this issue May 22, 2024 · 5 comments
Closed

Erroneous RangeError when compiling to WASM #55817

Rexios80 opened this issue May 22, 2024 · 5 comments
Assignees
Labels
area-dart2wasm Issues for the dart2wasm compiler. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@Rexios80
Copy link
Contributor

Rexios80 commented May 22, 2024

flyerhq/flutter_chat_ui#596

Unhandled exception:
RangeError (index): Invalid value: Valid value range is empty: 1
#0      List.[] (dart:core-patch/growable_array.dart:264)
#1      Intrinsifier.generateInstanceIntrinsic (package:dart2wasm/intrinsics.dart:337)
#2      CodeGenerator.visitInstanceInvocation (package:dart2wasm/code_generator.dart:1817)
#3      InstanceInvocation.accept1 (package:kernel/ast.dart:5803)
String _multiUserTextBuilder(List<types.User> author) {
  if (author.isEmpty) {
    return '';
  } else if (author.length == 1) {
    return '${author.first.firstName} is typing';
  } else if (author.length == 2) {
    return '${author.first.firstName} and ${author[1].firstName}'; // This line is the issue
  } else {
    return '${author.first.firstName} and ${author.length - 1} others';
  }
}

As far as I can tell, this is perfectly valid Dart code failing to compile to WASM

See also: flutter/flutter#148617

@osa1 osa1 added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-dart2wasm Issues for the dart2wasm compiler. labels May 23, 2024
@osa1 osa1 self-assigned this May 23, 2024
@kevmoo
Copy link
Member

kevmoo commented May 23, 2024

@Rexios80 – confirm 3.5.0 (build 3.5.0-167.0.dev) – right?

@osa1
Copy link
Member

osa1 commented May 23, 2024

I've started debugging this, but the package cannot be built right now without making some changes in the dependencies, see flyerhq/flutter_chat_ui#596 (comment).

@Rexios80
Copy link
Contributor Author

@kevmoo

% flutter --version
Flutter 3.22.0-43.0.pre.18 • channel [user-branch] • unknown source
Framework • revision 7b51c0c336 (10 minutes ago) • 2024-05-23 08:55:23 -0400
Engine • revision 8b094fbb94
Tools • Dart 3.5.0 (build 3.5.0-178.0.dev) • DevTools 2.36.0-dev.10

@mkustermann
Copy link
Member

I think this is an incorrect optimization: We see that the List<types.User> author list is actually always a const <types.User>[]. Then our compiler wants to optimize the code by performing the list index at compile-time, but it doesn't check whether the index is in-bounds.

=> cl/368302 should fix this.

@mkustermann
Copy link
Member

Filed cherry-pick request for stable: #55853

copybara-service bot pushed a commit that referenced this issue May 29, 2024
…t if index is in-bounds

We have an optimization that will do list lookups at compile time when
the receiver is a constant list and the index is a constant integer.

=> We should only perform this optimization if index is in-bounds.
=> If it's out-of-bounds it should be a [RangeError] thrown at runtime
   (if that code is ever executed)

Bug: #55817

Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/368302
Cherry-pick-request: #55853
Change-Id: I6a8b8ebd4ec0917d963e425fb0202aaef8d19bbd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368304
Reviewed-by: Slava Egorov <[email protected]>
Reviewed-by: Ömer Ağacan <[email protected]>
Commit-Queue: Martin Kustermann <[email protected]>
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. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants