Skip to content

Commit c96a89b

Browse files
committed
Introduce a suppressible feature for async sequences Failure type
Suppressing this feature doesn't disable the use of new syntax in the normal way. Instead, it introduces `@rethrows` on the AsyncIteratorProtocol and AsyncSequence protocols, so that older compilers can still use the async sequences generated by newer compilers and standard libraries. Fixes the rest of rdar://123782658
1 parent d1ac903 commit c96a89b

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ struct PrintOptions {
372372
/// Whether to suppress printing of custom attributes that are expanded macros.
373373
bool SuppressExpandedMacros = true;
374374

375+
/// Whether we're supposed to slap a @rethrows on `AsyncSequence` /
376+
/// `AsyncIteratorProtocol` for backward-compatibility reasons.
377+
bool AsyncSequenceRethrows = false;
378+
375379
/// List of attribute kinds that should not be printed.
376380
std::vector<AnyAttrKind> ExcludeAttrList = {
377381
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(PrimaryAssociatedTypes2, 346, "Primary associated
110110
SUPPRESSIBLE_LANGUAGE_FEATURE(UnavailableFromAsync, 0, "@_unavailableFromAsync")
111111
SUPPRESSIBLE_LANGUAGE_FEATURE(NoAsyncAvailability, 340, "@available(*, noasync)")
112112
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeAvailability, 0, "Availability on associated types")
113+
SUPPRESSIBLE_LANGUAGE_FEATURE(AsyncSequenceFailure, 0, "Failure associated type on AsyncSequence and AsyncIteratorProtocol")
113114
LANGUAGE_FEATURE(BuiltinIntLiteralAccessors, 368, "Builtin.IntLiteral accessors")
114115
LANGUAGE_FEATURE(Macros, 0, "Macros")
115116
LANGUAGE_FEATURE(FreestandingExpressionMacros, 382, "Expression macros")

lib/AST/ASTPrinter.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,11 @@ static bool hasLessAccessibleSetter(const AbstractStorageDecl *ASD) {
12051205
return ASD->getSetterFormalAccess() < ASD->getFormalAccess();
12061206
}
12071207

1208+
static bool isImplicitRethrowsProtocol(const ProtocolDecl *proto) {
1209+
return proto->isSpecificProtocol(KnownProtocolKind::AsyncSequence) ||
1210+
proto->isSpecificProtocol(KnownProtocolKind::AsyncIteratorProtocol);
1211+
}
1212+
12081213
void PrintAST::printAttributes(const Decl *D) {
12091214
if (Options.SkipAttributes)
12101215
return;
@@ -1215,6 +1220,17 @@ void PrintAST::printAttributes(const Decl *D) {
12151220

12161221
auto attrs = D->getAttrs();
12171222

1223+
// When printing a Swift interface, make sure that older compilers see
1224+
// @rethrows on the AsyncSequence and AsyncIteratorProtocol.
1225+
if (Options.AsyncSequenceRethrows && Options.IsForSwiftInterface) {
1226+
if (auto proto = dyn_cast<ProtocolDecl>(D)) {
1227+
if (isImplicitRethrowsProtocol(proto)) {
1228+
Printer << "@rethrows";
1229+
Printer.printNewline();
1230+
}
1231+
}
1232+
}
1233+
12181234
// Save the current number of exclude attrs to restore once we're done.
12191235
unsigned originalExcludeAttrCount = Options.ExcludeAttrList.size();
12201236

@@ -3319,6 +3335,21 @@ suppressingFeatureAssociatedTypeAvailability(
33193335
options.ExcludeAttrList.resize(originalExcludeAttrCount);
33203336
}
33213337

3338+
static bool usesFeatureAsyncSequenceFailure(Decl *decl) {
3339+
if (auto proto = dyn_cast<ProtocolDecl>(decl)) {
3340+
return isImplicitRethrowsProtocol(proto);
3341+
}
3342+
3343+
return false;
3344+
}
3345+
3346+
static void
3347+
suppressingFeatureAsyncSequenceFailure(
3348+
PrintOptions &options, llvm::function_ref<void()> action) {
3349+
llvm::SaveAndRestore<bool> saved(options.AsyncSequenceRethrows, true);
3350+
action();
3351+
}
3352+
33223353
static bool usesFeatureBuiltinIntLiteralAccessors(Decl *decl) {
33233354
return false;
33243355
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name _Concurrency -disable-implicit-concurrency-module-import
2+
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name _Concurrency -disable-implicit-concurrency-module-import
3+
// RUN: %FileCheck %s < %t.swiftinterface
4+
5+
// REQUIRES: concurrency
6+
7+
// CHECK: @rethrows
8+
// CHECK-NEXT: public protocol AsyncIteratorProtocol
9+
public protocol AsyncIteratorProtocol {
10+
}

0 commit comments

Comments
 (0)