Skip to content

[BitwiseCopyable] Avoid a condfail. #73516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ struct PrintOptions {
/// Suppress printing of '~Proto' for suppressible, non-invertible protocols.
bool SuppressConformanceSuppression = false;

/// Replace BitwiseCopyable with _BitwiseCopyable.
bool SuppressBitwiseCopyable = false;

/// List of attribute kinds that should not be printed.
std::vector<AnyAttrKind> ExcludeAttrList = {
DeclAttrKind::Transparent, DeclAttrKind::Effects,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ LANGUAGE_FEATURE(BuiltinCreateTask, 0, "Builtin.createTask and Builtin.createDis
SUPPRESSIBLE_LANGUAGE_FEATURE(AssociatedTypeImplements, 0, "@_implements on associated types")
LANGUAGE_FEATURE(BuiltinAddressOfRawLayout, 0, "Builtin.addressOfRawLayout")
LANGUAGE_FEATURE(MoveOnlyPartialConsumption, 429, "Partial consumption of noncopyable values")
/// Enable bitwise-copyable feature.
LANGUAGE_FEATURE(BitwiseCopyable, 426, "BitwiseCopyable protocol")
SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferred conformances")
SUPPRESSIBLE_LANGUAGE_FEATURE(BitwiseCopyable2, 426, "BitwiseCopyable feature")
SUPPRESSIBLE_LANGUAGE_FEATURE(NoncopyableGenerics, 427, "Noncopyable generics")

// Swift 6
Expand Down
31 changes: 29 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,15 @@ suppressingFeatureConformanceSuppression(PrintOptions &options,
options.ExcludeAttrList.resize(originalExcludeAttrCount);
}

static void
suppressingFeatureBitwiseCopyable2(PrintOptions &options,
llvm::function_ref<void()> action) {
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
llvm::SaveAndRestore<bool> scope(options.SuppressBitwiseCopyable, true);
action();
options.ExcludeAttrList.resize(originalExcludeAttrCount);
}

/// Suppress the printing of a particular feature.
static void suppressingFeature(PrintOptions &options, Feature feature,
llvm::function_ref<void()> action) {
Expand Down Expand Up @@ -3436,17 +3445,29 @@ void PrintAST::visitOpaqueTypeDecl(OpaqueTypeDecl *decl) {
}

void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
auto name = decl->getName();
bool suppressingBitwiseCopyable =
Options.SuppressBitwiseCopyable &&
decl->getModuleContext()->isStdlibModule() &&
(decl->getNameStr() == "_BitwiseCopyable");
if (suppressingBitwiseCopyable) {
name = decl->getASTContext().getIdentifier("BitwiseCopyable");
}
printDocumentationComment(decl);
printAttributes(decl);
printAccess(decl);
Printer.printIntroducerKeyword("typealias", Options, " ");
printContextIfNeeded(decl);
recordDeclLoc(decl,
[&]{
Printer.printName(decl->getName(), getTypeMemberPrintNameContext(decl));
Printer.printName(name, getTypeMemberPrintNameContext(decl));
}, [&]{ // Signature
printGenericDeclGenericParams(decl);
});
if (suppressingBitwiseCopyable) {
Printer << " = Swift._BitwiseCopyable";
return;
}
bool ShouldPrint = true;
Type Ty = decl->getUnderlyingType();

Expand Down Expand Up @@ -3627,6 +3648,12 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
}

void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
auto name = decl->getName();
if (Options.SuppressBitwiseCopyable &&
decl->getModuleContext()->isStdlibModule() &&
(decl->getNameStr() == "BitwiseCopyable")) {
name = decl->getASTContext().getIdentifier("_BitwiseCopyable");
}
printDocumentationComment(decl);
printAttributes(decl);
printAccess(decl);
Expand All @@ -3640,7 +3667,7 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
printContextIfNeeded(decl);
recordDeclLoc(decl,
[&]{
Printer.printName(decl->getName());
Printer.printName(name);
});

if (Options.PrintPrimaryAssociatedTypes) {
Expand Down
13 changes: 13 additions & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,19 @@ static bool usesFeatureConformanceSuppression(Decl *decl) {
return false;
}

static bool usesFeatureBitwiseCopyable2(Decl *decl) {
if (!decl->getModuleContext()->isStdlibModule()) {
return false;
}
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
return proto->getNameStr() == "BitwiseCopyable";
}
if (auto *typealias = dyn_cast<TypeAliasDecl>(decl)) {
return typealias->getNameStr() == "_BitwiseCopyable";
}
return false;
}

static bool usesFeatureIsolatedAny(Decl *decl) {
return usesTypeMatching(decl, [](Type type) {
if (auto fnType = type->getAs<AnyFunctionType>()) {
Expand Down
13 changes: 11 additions & 2 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,20 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
@_documentation(visibility: internal)
@_marker public protocol Escapable {}

#if $BitwiseCopyable2
#if $NoncopyableGenerics && $NonescapableTypes
@_marker public protocol BitwiseCopyable: ~Escapable { }
#else
@_marker public protocol BitwiseCopyable { }
#endif

@available(*, unavailable)
@_marker public protocol _BitwiseCopyable {}
@available(*, deprecated, message: "Use BitwiseCopyable")
public typealias _BitwiseCopyable = BitwiseCopyable
#else
#if $NoncopyableGenerics && $NonescapableTypes
@_marker public protocol _BitwiseCopyable: ~Escapable { }
#else
@_marker public protocol _BitwiseCopyable { }
#endif
public typealias BitwiseCopyable = _BitwiseCopyable
#endif
6 changes: 6 additions & 0 deletions test/ModuleInterface/bitwise_copyable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
public struct S_Implicit_Noncopyable {}

// CHECK-NOT: extension Test.S_Implicit_Noncopyable : Swift.BitwiseCopyable {}

// CHECK: public protocol BitwiseCopyable {
// CHECK-NEXT: }
// CHECK-NEXT: public typealias _BitwiseCopyable = Test.BitwiseCopyable
public protocol BitwiseCopyable {}
public typealias _BitwiseCopyable = BitwiseCopyable
21 changes: 21 additions & 0 deletions test/ModuleInterface/bitwise_copyable_stdlib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -parse-stdlib -module-name Swift
// RUN: %FileCheck %s < %t.swiftinterface
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -parse-stdlib -module-name Swift

// CHECK: #if compiler(>=5.3) && $BitwiseCopyable2
// CHECK-NEXT: public protocol BitwiseCopyable {
// CHECK-NEXT: }
// CHECK-NEXT: #else
// CHECK-NEXT: public protocol _BitwiseCopyable {
// CHECK-NEXT: }
// CHECK-NEXT: #endif

// CHECK: #if compiler(>=5.3) && $BitwiseCopyable2
// CHECK-NEXT: public typealias _BitwiseCopyable = Swift.BitwiseCopyable
// CHECK-NEXT: #else
// CHECK-NEXT: public typealias BitwiseCopyable = Swift._BitwiseCopyable
// CHECK-NEXT: #endif
public protocol BitwiseCopyable {}
public typealias _BitwiseCopyable = BitwiseCopyable

7 changes: 7 additions & 0 deletions test/Sema/bitwse_copyable_underscore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift \
// RUN: -disable-availability-checking \
// RUN: -debug-diagnostic-names

struct S : _BitwiseCopyable {} // expected-warning {{'_BitwiseCopyable' is deprecated: Use BitwiseCopyable}}

func f<T : _BitwiseCopyable>(_ t: T) {} // expected-warning {{'_BitwiseCopyable' is deprecated: Use BitwiseCopyable}}