Skip to content

Commit 4199d9f

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 68e23b7 commit 4199d9f

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

include/swift/AST/PrintOptions.h

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

397+
/// Replace BitwiseCopyable with _BitwiseCopyable.
398+
bool SuppressBitwiseCopyable = false;
399+
397400
/// List of attribute kinds that should not be printed.
398401
std::vector<AnyAttrKind> ExcludeAttrList = {
399402
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ LANGUAGE_FEATURE(BuiltinCreateTask, 0, "Builtin.createTask and Builtin.createDis
177177
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeImplements, 0, "@_implements on associated types")
178178
LANGUAGE_FEATURE(BuiltinAddressOfRawLayout, 0, "Builtin.addressOfRawLayout")
179179
LANGUAGE_FEATURE(MoveOnlyPartialConsumption, 429, "Partial consumption of noncopyable values")
180-
/// Enable bitwise-copyable feature.
181180
LANGUAGE_FEATURE(BitwiseCopyable, 426, "BitwiseCopyable protocol")
182181
SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferred conformances")
182+
SUPPRESSIBLE_LANGUAGE_FEATURE(BitwiseCopyable2, 426, "BitwiseCopyable feature")
183183

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

lib/AST/ASTPrinter.cpp

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

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

34383447
void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
3448+
auto name = decl->getName();
3449+
bool suppressingBitwiseCopyable = Options.SuppressBitwiseCopyable &&
3450+
(decl->getNameStr() == "_BitwiseCopyable");
3451+
if (suppressingBitwiseCopyable) {
3452+
name = decl->getASTContext().getIdentifier("BitwiseCopyable");
3453+
}
34393454
printDocumentationComment(decl);
34403455
printAttributes(decl);
34413456
printAccess(decl);
34423457
Printer.printIntroducerKeyword("typealias", Options, " ");
34433458
printContextIfNeeded(decl);
34443459
recordDeclLoc(decl,
34453460
[&]{
3446-
Printer.printName(decl->getName(), getTypeMemberPrintNameContext(decl));
3461+
Printer.printName(name, getTypeMemberPrintNameContext(decl));
34473462
}, [&]{ // Signature
34483463
printGenericDeclGenericParams(decl);
34493464
});
3465+
if (suppressingBitwiseCopyable) {
3466+
Printer << " = Swift._BitwiseCopyable";
3467+
return;
3468+
}
34503469
bool ShouldPrint = true;
34513470
Type Ty = decl->getUnderlyingType();
34523471

@@ -3627,6 +3646,11 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
36273646
}
36283647

36293648
void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
3649+
auto name = decl->getName();
3650+
if (Options.SuppressBitwiseCopyable &&
3651+
(decl->getNameStr() == "BitwiseCopyable")) {
3652+
name = decl->getASTContext().getIdentifier("_BitwiseCopyable");
3653+
}
36303654
printDocumentationComment(decl);
36313655
printAttributes(decl);
36323656
printAccess(decl);
@@ -3640,7 +3664,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
36403664
printContextIfNeeded(decl);
36413665
recordDeclLoc(decl,
36423666
[&]{
3643-
Printer.printName(decl->getName());
3667+
Printer.printName(name);
36443668
});
36453669

36463670
if (Options.PrintPrimaryAssociatedTypes) {

lib/AST/FeatureSet.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,16 @@ static bool usesFeatureConformanceSuppression(Decl *decl) {
693693
return false;
694694
}
695695

696+
static bool usesFeatureBitwiseCopyable2(Decl *decl) {
697+
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
698+
return proto->getNameStr() == "BitwiseCopyable";
699+
}
700+
if (auto *typealias = dyn_cast<TypeAliasDecl>(decl)) {
701+
return typealias->getNameStr() == "_BitwiseCopyable";
702+
}
703+
return false;
704+
}
705+
696706
static bool usesFeatureIsolatedAny(Decl *decl) {
697707
return usesTypeMatching(decl, [](Type type) {
698708
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

0 commit comments

Comments
 (0)