Skip to content

Commit 11b5d27

Browse files
committed
[CoroutineAccessors] Add flag to observe errors.
Temporarily allow the legacy behavior of allowing caller coroutine accessors to observe errors (i.e. by executing no code after the yield if the caller threw an error) behind the CoroutineAccessorsUnwindOnCallerError flag.
1 parent 8972d62 commit 11b5d27

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

include/swift/AST/Types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4825,7 +4825,8 @@ enum class SILCoroutineKind : uint8_t {
48254825

48264826
/// This function is a yield-once coroutine (used by read and modify
48274827
/// accessors). It has the following differences from YieldOnce:
4828-
/// - it does not observe errors thrown by its caller
4828+
/// - it does not observe errors thrown by its caller (unless the feature
4829+
/// CoroutineAccessorsUnwindOnCallerError is enabled)
48294830
/// - it uses the callee-allocated ABI
48304831
YieldOnce2,
48314832

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ EXPERIMENTAL_FEATURE(UnspecifiedMeansMainActorIsolated, false)
427427
/// modify/read single-yield coroutines
428428
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CoroutineAccessors, true)
429429

430+
/// modify/read single-yield coroutines always execute code post-yield code
431+
EXPERIMENTAL_FEATURE(CoroutineAccessorsUnwindOnCallerError, false)
432+
430433
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
431434
#undef EXPERIMENTAL_FEATURE
432435
#undef UPCOMING_FEATURE

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ static bool usesFeatureAllowUnsafeAttribute(Decl *decl) {
296296
UNINTERESTING_FEATURE(WarnUnsafe)
297297
UNINTERESTING_FEATURE(SafeInterop)
298298
UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
299+
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)
299300

300301
bool swift::usesFeatureIsolatedDeinit(const Decl *decl) {
301302
if (auto cd = dyn_cast<ClassDecl>(decl)) {

lib/SILGen/SILGenApply.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4992,7 +4992,10 @@ class EndCoroutineApply : public Cleanup {
49924992
auto *beginApply =
49934993
cast<BeginApplyInst>(ApplyToken->getDefiningInstruction());
49944994
auto isCalleeAllocated = beginApply->isCalleeAllocated();
4995-
auto unwindOnCallerError = !isCalleeAllocated;
4995+
auto unwindOnCallerError =
4996+
!isCalleeAllocated ||
4997+
SGF.SGM.getASTContext().LangOpts.hasFeature(
4998+
Feature::CoroutineAccessorsUnwindOnCallerError);
49964999
if (forUnwind && unwindOnCallerError) {
49975000
SGF.B.createAbortApply(l, ApplyToken);
49985001
} else {

test/SILGen/coroutine_accessors.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// RUN: %target-swift-emit-silgen \
22
// RUN: %s \
33
// RUN: -enable-experimental-feature CoroutineAccessors \
4-
// RUN: | %FileCheck %s
4+
// RUN: | %FileCheck %s --check-prefixes=CHECK,CHECK-NOUNWIND
5+
6+
// RUN: %target-swift-emit-silgen \
7+
// RUN: %s \
8+
// RUN: -enable-experimental-feature CoroutineAccessors \
9+
// RUN: -enable-experimental-feature CoroutineAccessorsUnwindOnCallerError \
10+
// RUN: | %FileCheck %s --check-prefixes=CHECK,CHECK-UNWIND
511

612
// REQUIRES: asserts
713

@@ -115,7 +121,8 @@ public var irm: Int {
115121
// CHECK: dealloc_stack [[OLD_VALUE_ADDR]]
116122
// CHECK: return [[OLD_VALUE]]
117123
// CHECK: bb2([[ERROR:%[^,]+]] : @owned $any Error):
118-
// CHECK: end_apply [[TOKEN]]
124+
// CHECK-NOUNWIND: end_apply [[TOKEN]]
125+
// CHECK-UNWIND: abort_apply [[TOKEN]]
119126
// CHECK: end_access [[SELF_ACCESS]]
120127
// CHECK: dealloc_stack [[NEW_VALUE_ADDR]]
121128
// CHECK: dealloc_stack [[OLD_VALUE_ADDR]]

0 commit comments

Comments
 (0)