[dart2wasm] Fix exception handling in async functions

Fixes #55347.

Change-Id: I9ced2a4c06e6cb9714dc47e3661f5582c70cdeb0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/361063
Reviewed-by: Erik Ernst <eernst@google.com>
Reviewed-by: Jackson Gardner <jacksongardner@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
This commit is contained in:
Ömer Sinan Ağacan 2024-04-05 14:23:00 +00:00 committed by Commit Queue
parent 4fd5d783c1
commit 6de879e0f5
2 changed files with 57 additions and 2 deletions

View file

@ -1123,8 +1123,11 @@ class AsyncCodeGenerator extends CodeGenerator {
for (int catchIdx = 0; catchIdx < node.catches.length; catchIdx += 1) {
final Catch catch_ = node.catches[catchIdx];
final Catch? nextCatch =
node.catches.length < catchIdx ? node.catches[catchIdx + 1] : null;
final nextCatchIdx = catchIdx + 1;
final Catch? nextCatch = nextCatchIdx < node.catches.length
? node.catches[nextCatchIdx]
: null;
_emitTargetLabel(innerTargets[catch_]!);

View file

@ -0,0 +1,52 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests try-catch in `async`, with multiple `catch`/`on` blocks, where the
// first type test in the `catch`/`on` blocks fail and the subsequent test
// passes.
//
// This is a regression test for issue #55347.
import 'package:expect/expect.dart';
class MyException implements Exception {
MyException([this.message]);
final String? message;
@override
String toString() => 'MyException($message)';
}
class MyOtherException implements Exception {
MyOtherException([this.message]);
final String? message;
@override
String toString() => 'MyOtherException($message)';
}
Future<String> asynchronouslyThrowException() async {
throw MyException('Throwing an error!');
}
Future<String?> test() async {
try {
await asynchronouslyThrowException();
Expect.fail('Exception is not thrown');
} on MyOtherException {
Expect.fail('Wrong exception caught');
} on MyException {
return 'Success';
} catch (error) {
Expect.fail('Wrong exception caught');
}
Expect.fail('No exception caught');
return null;
}
void main() async {
Expect.equals(await test(), 'Success');
}