Skip to content

Commit 6de879e

Browse files
osa1Commit Queue
authored and
Commit Queue
committed
[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 <[email protected]> Reviewed-by: Jackson Gardner <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent 4fd5d78 commit 6de879e

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

pkg/dart2wasm/lib/async.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,11 @@ class AsyncCodeGenerator extends CodeGenerator {
11231123

11241124
for (int catchIdx = 0; catchIdx < node.catches.length; catchIdx += 1) {
11251125
final Catch catch_ = node.catches[catchIdx];
1126-
final Catch? nextCatch =
1127-
node.catches.length < catchIdx ? node.catches[catchIdx + 1] : null;
1126+
1127+
final nextCatchIdx = catchIdx + 1;
1128+
final Catch? nextCatch = nextCatchIdx < node.catches.length
1129+
? node.catches[nextCatchIdx]
1130+
: null;
11281131

11291132
_emitTargetLabel(innerTargets[catch_]!);
11301133

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests try-catch in `async`, with multiple `catch`/`on` blocks, where the
6+
// first type test in the `catch`/`on` blocks fail and the subsequent test
7+
// passes.
8+
//
9+
// This is a regression test for issue #55347.
10+
11+
import 'package:expect/expect.dart';
12+
13+
class MyException implements Exception {
14+
MyException([this.message]);
15+
16+
final String? message;
17+
18+
@override
19+
String toString() => 'MyException($message)';
20+
}
21+
22+
class MyOtherException implements Exception {
23+
MyOtherException([this.message]);
24+
25+
final String? message;
26+
27+
@override
28+
String toString() => 'MyOtherException($message)';
29+
}
30+
31+
Future<String> asynchronouslyThrowException() async {
32+
throw MyException('Throwing an error!');
33+
}
34+
35+
Future<String?> test() async {
36+
try {
37+
await asynchronouslyThrowException();
38+
Expect.fail('Exception is not thrown');
39+
} on MyOtherException {
40+
Expect.fail('Wrong exception caught');
41+
} on MyException {
42+
return 'Success';
43+
} catch (error) {
44+
Expect.fail('Wrong exception caught');
45+
}
46+
Expect.fail('No exception caught');
47+
return null;
48+
}
49+
50+
void main() async {
51+
Expect.equals(await test(), 'Success');
52+
}

0 commit comments

Comments
 (0)