dart-sdk/tests/language/sync_star/sync_star_exception_nested_test.dart
Clement Skau c486a07b02 [VM] Fixes yield* exception handling in sync*.
This adds a mechanism similar to that used in async functions where
exceptions are caught in the synthetic code and passed into the
generated body to be rethrow'n.
This ensures the exception is throw'n from the same place as the
original yield*, as per the spec.

Bug: https://github.com/dart-lang/sdk/issues/42466
Change-Id: I054b9db568a49b046b6bb49f3e775bf093f83950
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/160221
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
2020-10-27 06:46:27 +00:00

56 lines
973 B
Dart

// Copyright (c) 2020, 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.
// See: https://github.com/dart-lang/sdk/issues/42466
import 'dart:collection';
import 'package:expect/expect.dart';
String? caughtString;
a() sync* {
yield 3;
throw 'Throw from a()';
yield 4;
}
b() sync* {
yield 2;
yield* a();
yield 5;
}
c() sync* {
try {
yield 1;
yield* b();
yield 6;
} catch (e, st) {
caughtString = 'Caught in c()';
}
}
d() sync* {
try {
yield 0;
yield* c();
yield 7;
} catch (e, st) {
caughtString = 'Caught in d()';
}
}
main() {
List yields = [];
try {
for (final e in d()) {
yields.add(e);
}
} catch (e, st) {
caughtString = 'Caught in main()';
}
Expect.equals('Caught in c()', caughtString);
Expect.listEquals([0, 1, 2, 3, 7], yields);
}