Skip to content

Commit 13bebd2

Browse files
authored
Merge pull request swiftlang#71990 from DougGregor/new-concurrency-lib-old-compiler-hacks
Introduce suppressible features so that the newer _Concurrency interface file can be handled by older compilers
2 parents 7944945 + c96a89b commit 13bebd2

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(UnsafeInheritExecutor, 0, "@_unsafeInheritExecutor
109109
SUPPRESSIBLE_LANGUAGE_FEATURE(PrimaryAssociatedTypes2, 346, "Primary associated types")
110110
SUPPRESSIBLE_LANGUAGE_FEATURE(UnavailableFromAsync, 0, "@_unavailableFromAsync")
111111
SUPPRESSIBLE_LANGUAGE_FEATURE(NoAsyncAvailability, 340, "@available(*, noasync)")
112+
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeAvailability, 0, "Availability on associated types")
113+
SUPPRESSIBLE_LANGUAGE_FEATURE(AsyncSequenceFailure, 0, "Failure associated type on AsyncSequence and AsyncIteratorProtocol")
112114
LANGUAGE_FEATURE(BuiltinIntLiteralAccessors, 368, "Builtin.IntLiteral accessors")
113115
LANGUAGE_FEATURE(Macros, 0, "Macros")
114116
LANGUAGE_FEATURE(FreestandingExpressionMacros, 382, "Expression macros")

lib/AST/ASTPrinter.cpp

Lines changed: 45 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

@@ -3305,6 +3321,35 @@ static bool usesFeatureNoAsyncAvailability(Decl *decl) {
33053321
return decl->getAttrs().getNoAsync(decl->getASTContext()) != nullptr;
33063322
}
33073323

3324+
static bool usesFeatureAssociatedTypeAvailability(Decl *decl) {
3325+
return isa<AssociatedTypeDecl>(decl) &&
3326+
decl->getAttrs().hasAttribute<AvailableAttr>();
3327+
}
3328+
3329+
static void
3330+
suppressingFeatureAssociatedTypeAvailability(
3331+
PrintOptions &options, llvm::function_ref<void()> action) {
3332+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3333+
options.ExcludeAttrList.push_back(DeclAttrKind::Available);
3334+
action();
3335+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
3336+
}
3337+
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+
33083353
static bool usesFeatureBuiltinIntLiteralAccessors(Decl *decl) {
33093354
return false;
33103355
}

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ extension ASTGenError {
2020
MessageID(domain: "ASTGen", id: "\(Self.self)")
2121
}
2222

23-
var severity: DiagnosticSeverity {
24-
.error
25-
}
23+
var severity: DiagnosticSeverity { .error }
2624
}
2725

2826
/// An error emitted when a token is of an unexpected kind.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name assoc
2+
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name assoc
3+
// RUN: %FileCheck %s < %t.swiftinterface
4+
5+
// REQUIRES: concurrency, objc_interop
6+
7+
// CHECK: public protocol P
8+
public protocol P {
9+
// CHECK: #if compiler(>=5.3) && $AssociatedTypeAvailability
10+
// CHECK-NEXT: @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
11+
// CHECK-NEXT: associatedtype AT = Self
12+
// CHECK-NEXT: #else
13+
// CHECK-NEXT: associatedtype AT = Self
14+
// CHECK-NEXT: #endif
15+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
16+
associatedtype AT = Self
17+
}
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)