Skip to content

Commit 387e8d3

Browse files
Merge pull request #73516 from nate-chandler/rdar127755503
[BitwiseCopyable] Avoid a condfail.
2 parents 38371cd + 2671652 commit 387e8d3

File tree

8 files changed

+91
-5
lines changed

8 files changed

+91
-5
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
@@ -176,9 +176,9 @@ LANGUAGE_FEATURE(BuiltinCreateTask, 0, "Builtin.createTask and Builtin.createDis
176176
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeImplements, 0, "@_implements on associated types")
177177
LANGUAGE_FEATURE(BuiltinAddressOfRawLayout, 0, "Builtin.addressOfRawLayout")
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
SUPPRESSIBLE_LANGUAGE_FEATURE(NoncopyableGenerics, 427, "Noncopyable generics")
183183

184184
// Swift 6

lib/AST/ASTPrinter.cpp

Lines changed: 29 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,29 @@ void PrintAST::visitOpaqueTypeDecl(OpaqueTypeDecl *decl) {
34363445
}
34373446

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

@@ -3627,6 +3648,12 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
36273648
}
36283649

36293650
void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
3651+
auto name = decl->getName();
3652+
if (Options.SuppressBitwiseCopyable &&
3653+
decl->getModuleContext()->isStdlibModule() &&
3654+
(decl->getNameStr() == "BitwiseCopyable")) {
3655+
name = decl->getASTContext().getIdentifier("_BitwiseCopyable");
3656+
}
36303657
printDocumentationComment(decl);
36313658
printAttributes(decl);
36323659
printAccess(decl);
@@ -3640,7 +3667,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
36403667
printContextIfNeeded(decl);
36413668
recordDeclLoc(decl,
36423669
[&]{
3643-
Printer.printName(decl->getName());
3670+
Printer.printName(name);
36443671
});
36453672

36463673
if (Options.PrintPrimaryAssociatedTypes) {

lib/AST/FeatureSet.cpp

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

727+
static bool usesFeatureBitwiseCopyable2(Decl *decl) {
728+
if (!decl->getModuleContext()->isStdlibModule()) {
729+
return false;
730+
}
731+
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
732+
return proto->getNameStr() == "BitwiseCopyable";
733+
}
734+
if (auto *typealias = dyn_cast<TypeAliasDecl>(decl)) {
735+
return typealias->getNameStr() == "_BitwiseCopyable";
736+
}
737+
return false;
738+
}
739+
727740
static bool usesFeatureIsolatedAny(Decl *decl) {
728741
return usesTypeMatching(decl, [](Type type) {
729742
if (auto fnType = type->getAs<AnyFunctionType>()) {

stdlib/public/core/Misc.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,20 @@ 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
180181
@_marker public protocol BitwiseCopyable { }
181182
#endif
182183

183-
@available(*, unavailable)
184-
@_marker public protocol _BitwiseCopyable {}
184+
@available(*, deprecated, message: "Use BitwiseCopyable")
185+
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+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift \
2+
// RUN: -disable-availability-checking \
3+
// RUN: -debug-diagnostic-names
4+
5+
struct S : _BitwiseCopyable {} // expected-warning {{'_BitwiseCopyable' is deprecated: Use BitwiseCopyable}}
6+
7+
func f<T : _BitwiseCopyable>(_ t: T) {} // expected-warning {{'_BitwiseCopyable' is deprecated: Use BitwiseCopyable}}

0 commit comments

Comments
 (0)