Skip to content

Commit eca4afa

Browse files
committed
[BitwiseCopyable] Avoid a condfail.
The standard library defines ``` protocol BitwiseCopyable {} typealias _BitwiseCopyable = BitwiseCopyable ``` For current compilers, `BitwiseCopyable` is a "known protocol". For older compilers, it is not; instead `_BitwiseCopyable` is. So print the following into the swiftinterface for those older compilers: ``` protocol _BitwiseCopyable {} typealias BitwiseCopyable = _BitwiseCopyable ``` rdar://127755503
1 parent e441d7e commit eca4afa

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ struct PrintOptions {
391391
/// Suppress printing of '~Proto' for suppressible, non-invertible protocols.
392392
bool SuppressConformanceSuppression = false;
393393

394+
/// Replace BitwiseCopyable with _BitwiseCopyable.
395+
bool SuppressBitwiseCopyable = false;
396+
394397
/// List of attribute kinds that should not be printed.
395398
std::vector<AnyAttrKind> ExcludeAttrList = {
396399
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ LANGUAGE_FEATURE(BuiltinStoreRaw, 0, "Builtin.storeRaw")
176176
LANGUAGE_FEATURE(BuiltinCreateTask, 0, "Builtin.createTask and Builtin.createDiscardingTask")
177177
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeImplements, 0, "@_implements on associated types")
178178
LANGUAGE_FEATURE(MoveOnlyPartialConsumption, 429, "Partial consumption of noncopyable values")
179-
/// Enable bitwise-copyable feature.
180179
LANGUAGE_FEATURE(BitwiseCopyable, 426, "BitwiseCopyable protocol")
181180
SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferred conformances")
181+
SUPPRESSIBLE_LANGUAGE_FEATURE(BitwiseCopyable2, 426, "BitwiseCopyable feature")
182182

183183
// Swift 6
184184
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)

lib/AST/ASTPrinter.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,15 @@ suppressingFeatureConformanceSuppression(PrintOptions &options,
31533153
options.ExcludeAttrList.resize(originalExcludeAttrCount);
31543154
}
31553155

3156+
static void
3157+
suppressingFeatureBitwiseCopyable2(PrintOptions &options,
3158+
llvm::function_ref<void()> action) {
3159+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3160+
llvm::SaveAndRestore<bool> scope(options.SuppressBitwiseCopyable, true);
3161+
action();
3162+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
3163+
}
3164+
31563165
/// Suppress the printing of a particular feature.
31573166
static void suppressingFeature(PrintOptions &options, Feature feature,
31583167
llvm::function_ref<void()> action) {
@@ -3435,17 +3444,29 @@ void PrintAST::visitOpaqueTypeDecl(OpaqueTypeDecl *decl) {
34353444
}
34363445

34373446
void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
3447+
auto name = decl->getName();
3448+
bool suppressingBitwiseCopyable =
3449+
Options.SuppressBitwiseCopyable &&
3450+
decl->getModuleContext()->isStdlibModule() &&
3451+
(decl->getNameStr() == "_BitwiseCopyable");
3452+
if (suppressingBitwiseCopyable) {
3453+
name = decl->getASTContext().getIdentifier("BitwiseCopyable");
3454+
}
34383455
printDocumentationComment(decl);
34393456
printAttributes(decl);
34403457
printAccess(decl);
34413458
Printer.printIntroducerKeyword("typealias", Options, " ");
34423459
printContextIfNeeded(decl);
34433460
recordDeclLoc(decl,
34443461
[&]{
3445-
Printer.printName(decl->getName(), getTypeMemberPrintNameContext(decl));
3462+
Printer.printName(name, getTypeMemberPrintNameContext(decl));
34463463
}, [&]{ // Signature
34473464
printGenericDeclGenericParams(decl);
34483465
});
3466+
if (suppressingBitwiseCopyable) {
3467+
Printer << " = Swift._BitwiseCopyable";
3468+
return;
3469+
}
34493470
bool ShouldPrint = true;
34503471
Type Ty = decl->getUnderlyingType();
34513472

@@ -3626,6 +3647,12 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
36263647
}
36273648

36283649
void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
3650+
auto name = decl->getName();
3651+
if (Options.SuppressBitwiseCopyable &&
3652+
decl->getModuleContext()->isStdlibModule() &&
3653+
(decl->getNameStr() == "BitwiseCopyable")) {
3654+
name = decl->getASTContext().getIdentifier("_BitwiseCopyable");
3655+
}
36293656
printDocumentationComment(decl);
36303657
printAttributes(decl);
36313658
printAccess(decl);
@@ -3639,7 +3666,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
36393666
printContextIfNeeded(decl);
36403667
recordDeclLoc(decl,
36413668
[&]{
3642-
Printer.printName(decl->getName());
3669+
Printer.printName(name);
36433670
});
36443671

36453672
if (Options.PrintPrimaryAssociatedTypes) {

lib/AST/FeatureSet.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,19 @@ static bool usesFeatureConformanceSuppression(Decl *decl) {
677677
return false;
678678
}
679679

680+
static bool usesFeatureBitwiseCopyable2(Decl *decl) {
681+
if (!decl->getModuleContext()->isStdlibModule()) {
682+
return false;
683+
}
684+
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
685+
return proto->getNameStr() == "BitwiseCopyable";
686+
}
687+
if (auto *typealias = dyn_cast<TypeAliasDecl>(decl)) {
688+
return typealias->getNameStr() == "_BitwiseCopyable";
689+
}
690+
return false;
691+
}
692+
680693
static bool usesFeatureIsolatedAny(Decl *decl) {
681694
return usesTypeMatching(decl, [](Type type) {
682695
if (auto fnType = type->getAs<AnyFunctionType>()) {

stdlib/public/core/Misc.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
174174
@_documentation(visibility: internal)
175175
@_marker public protocol Escapable {}
176176

177+
#if $BitwiseCopyable2
177178
#if $NoncopyableGenerics && $NonescapableTypes
178179
@_marker public protocol BitwiseCopyable: ~Escapable { }
179180
#else
@@ -182,3 +183,11 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
182183

183184
@available(*, deprecated, message: "Use BitwiseCopyable")
184185
public typealias _BitwiseCopyable = BitwiseCopyable
186+
#else
187+
#if $NoncopyableGenerics && $NonescapableTypes
188+
@_marker public protocol _BitwiseCopyable: ~Escapable { }
189+
#else
190+
@_marker public protocol _BitwiseCopyable { }
191+
#endif
192+
public typealias BitwiseCopyable = _BitwiseCopyable
193+
#endif

test/ModuleInterface/bitwise_copyable.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@
99
public struct S_Implicit_Noncopyable {}
1010

1111
// CHECK-NOT: extension Test.S_Implicit_Noncopyable : Swift.BitwiseCopyable {}
12+
13+
// CHECK: public protocol BitwiseCopyable {
14+
// CHECK-NEXT: }
15+
// CHECK-NEXT: public typealias _BitwiseCopyable = Test.BitwiseCopyable
16+
public protocol BitwiseCopyable {}
17+
public typealias _BitwiseCopyable = BitwiseCopyable
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -parse-stdlib -module-name Swift
3+
// RUN: %FileCheck %s < %t.swiftinterface
4+
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -parse-stdlib -module-name Swift
5+
6+
// CHECK: #if compiler(>=5.3) && $BitwiseCopyable2
7+
// CHECK-NEXT: public protocol BitwiseCopyable {
8+
// CHECK-NEXT: }
9+
// CHECK-NEXT: #else
10+
// CHECK-NEXT: public protocol _BitwiseCopyable {
11+
// CHECK-NEXT: }
12+
// CHECK-NEXT: #endif
13+
14+
// CHECK: #if compiler(>=5.3) && $BitwiseCopyable2
15+
// CHECK-NEXT: public typealias _BitwiseCopyable = Swift.BitwiseCopyable
16+
// CHECK-NEXT: #else
17+
// CHECK-NEXT: public typealias BitwiseCopyable = Swift._BitwiseCopyable
18+
// CHECK-NEXT: #endif
19+
public protocol BitwiseCopyable {}
20+
public typealias _BitwiseCopyable = BitwiseCopyable
21+

0 commit comments

Comments
 (0)