Skip to content

Commit d1ac903

Browse files
committed
Introduce a suppressible feature for availability on associated types
This allows us to emit Swift interfaces that can be handled by older Swift compilers.
1 parent 0652bb7 commit d1ac903

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ 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")
112113
LANGUAGE_FEATURE(BuiltinIntLiteralAccessors, 368, "Builtin.IntLiteral accessors")
113114
LANGUAGE_FEATURE(Macros, 0, "Macros")
114115
LANGUAGE_FEATURE(FreestandingExpressionMacros, 382, "Expression macros")

lib/AST/ASTPrinter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,6 +3305,20 @@ static bool usesFeatureNoAsyncAvailability(Decl *decl) {
33053305
return decl->getAttrs().getNoAsync(decl->getASTContext()) != nullptr;
33063306
}
33073307

3308+
static bool usesFeatureAssociatedTypeAvailability(Decl *decl) {
3309+
return isa<AssociatedTypeDecl>(decl) &&
3310+
decl->getAttrs().hasAttribute<AvailableAttr>();
3311+
}
3312+
3313+
static void
3314+
suppressingFeatureAssociatedTypeAvailability(
3315+
PrintOptions &options, llvm::function_ref<void()> action) {
3316+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3317+
options.ExcludeAttrList.push_back(DeclAttrKind::Available);
3318+
action();
3319+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
3320+
}
3321+
33083322
static bool usesFeatureBuiltinIntLiteralAccessors(Decl *decl) {
33093323
return false;
33103324
}

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+
}

0 commit comments

Comments
 (0)