Skip to content

Commit bcd08c0

Browse files
Merge pull request #73235 from nate-chandler/bitwise-copyable/enable
[BitwiseCopyable] Promote to feature.
2 parents da9922d + b12def9 commit bcd08c0

File tree

55 files changed

+285
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+285
-284
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7740,12 +7740,14 @@ NOTE(note_non_bitwise_copyable_type_indirect_enum_element,none,
77407740
"indirect case is here", ())
77417741
ERROR(non_bitwise_copyable_type_suppressed,none,
77427742
"cannot both conform to and suppress conformance to 'BitwiseCopyable'", ())
7743+
ERROR(non_bitwise_copyable_type_sensitive,none,
7744+
"a @sensitive type cannot conform to 'BitwiseCopyable'", ())
77437745
ERROR(non_bitwise_copyable_type_cxx_nontrivial,none,
77447746
"non-trivial C++ type cannot conform to 'BitwiseCopyable'", ())
77457747
ERROR(non_bitwise_copyable_c_type_nontrivial,none,
77467748
"type with unrepresentable fields cannot derive conformance to 'BitwiseCopyable'", ())
77477749
NOTE(note_non_bitwise_copyable_c_type_add_attr,none,
7748-
"annotate the type __attribute__((__swift_attr__(\"_BitwiseCopyable\")))",())
7750+
"annotate the type __attribute__((__swift_attr__(\"BitwiseCopyable\")))",())
77497751
ERROR(non_bitwise_copyable_type_member,none,
77507752
"%select{stored property %2|associated value %2}1 of "
77517753
"'BitwiseCopyable'-conforming %kind3 has non-bitwise-copyable type %0",

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ PROTOCOL(FloatingPoint)
150150
INVERTIBLE_PROTOCOL_WITH_NAME(Name, #Name)
151151
#include "swift/ABI/InvertibleProtocols.def"
152152

153-
REPRESSIBLE_PROTOCOL_(BitwiseCopyable)
153+
REPRESSIBLE_PROTOCOL(BitwiseCopyable)
154154

155155
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByArrayLiteral, "Array", false)
156156
EXPRESSIBLE_BY_LITERAL_PROTOCOL(ExpressibleByBooleanLiteral, "BooleanLiteralType", true)

include/swift/Basic/Features.def

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +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.
181+
LANGUAGE_FEATURE(BitwiseCopyable, 426, "BitwiseCopyable protocol")
182+
SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferred conformances")
180183

181184
// Swift 6
182185
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -343,12 +346,6 @@ EXPERIMENTAL_FEATURE(StaticExclusiveOnly, true)
343346
/// Enable the @extractConstantsFromMembers attribute.
344347
EXPERIMENTAL_FEATURE(ExtractConstantsFromMembers, false)
345348

346-
/// Enable bitwise-copyable feature.
347-
EXPERIMENTAL_FEATURE(BitwiseCopyable, true)
348-
349-
/// Enable the suppression of inferred, non-invertible, protocols via ~.
350-
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ConformanceSuppression, true)
351-
352349
/// Enables the FixedArray data type.
353350
EXPERIMENTAL_FEATURE(FixedArrays, true)
354351

lib/AST/LifetimeDependence.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ static LifetimeDependenceKind getLifetimeDependenceKindFromDecl(
173173
}
174174

175175
static bool isBitwiseCopyable(Type type, ModuleDecl *mod, ASTContext &ctx) {
176-
if (!ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
177-
return false;
178-
}
179176
auto *bitwiseCopyableProtocol =
180177
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
181178
if (!bitwiseCopyableProtocol) {

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,7 @@ static SmallVector<ProtocolConformance *, 2> findSynthesizedConformances(
12841284
for (auto ip : InvertibleProtocolSet::allKnown())
12851285
trySynthesize(getKnownProtocolKind(ip));
12861286

1287-
if (nominal->getASTContext().LangOpts.hasFeature(
1288-
Feature::BitwiseCopyable)) {
1289-
trySynthesize(KnownProtocolKind::BitwiseCopyable);
1290-
}
1287+
trySynthesize(KnownProtocolKind::BitwiseCopyable);
12911288
}
12921289

12931290
/// Distributed actors can synthesize Encodable/Decodable, so look for those

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8028,9 +8028,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
80288028
continue;
80298029
}
80308030

8031-
if (swiftAttr->getAttribute() == "_BitwiseCopyable") {
8032-
if (!SwiftContext.LangOpts.hasFeature(Feature::BitwiseCopyable))
8033-
continue;
8031+
if (swiftAttr->getAttribute() == "BitwiseCopyable") {
80348032
auto *protocol =
80358033
SwiftContext.getProtocol(KnownProtocolKind::BitwiseCopyable);
80368034
auto *nominal = dyn_cast<NominalTypeDecl>(MappedDecl);

lib/SIL/IR/TypeLowering.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/DiagnosticEngine.h"
2121
#include "swift/AST/DiagnosticsSIL.h"
2222
#include "swift/AST/Expr.h"
23+
#include "swift/AST/FileUnit.h"
2324
#include "swift/AST/GenericEnvironment.h"
2425
#include "swift/AST/LocalArchetypeRequirementCollector.h"
2526
#include "swift/AST/Module.h"
@@ -28,6 +29,7 @@
2829
#include "swift/AST/Pattern.h"
2930
#include "swift/AST/PrettyStackTrace.h"
3031
#include "swift/AST/PropertyWrappers.h"
32+
#include "swift/AST/SourceFile.h"
3133
#include "swift/AST/TypeDifferenceVisitor.h"
3234
#include "swift/AST/Types.h"
3335
#include "swift/ClangImporter/ClangModule.h"
@@ -3041,8 +3043,6 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
30413043
AbstractionPattern origType,
30423044
CanType substType,
30433045
TypeExpansionContext forExpansion) {
3044-
if (!Context.LangOpts.hasFeature(Feature::BitwiseCopyable))
3045-
return;
30463046
auto *bitwiseCopyableProtocol =
30473047
Context.getProtocol(KnownProtocolKind::BitwiseCopyable);
30483048
if (!bitwiseCopyableProtocol)
@@ -3057,10 +3057,20 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
30573057

30583058
if (auto *nominal = substType.getAnyNominal()) {
30593059
auto *module = nominal->getModuleContext();
3060-
if (module && module->isBuiltFromInterface()) {
3061-
// Don't verify for types in modules built from interfaces; the feature
3062-
// may not have been enabled in them.
3063-
return;
3060+
if (module) {
3061+
if (module->isBuiltFromInterface()) {
3062+
// Don't verify for types in modules built from interfaces; the feature
3063+
// may not have been enabled in them.
3064+
return;
3065+
}
3066+
auto *file = dyn_cast_or_null<FileUnit>(module->getModuleScopeContext());
3067+
if (file && file->getKind() == FileUnitKind::Source) {
3068+
auto sourceFile = nominal->getParentSourceFile();
3069+
if (sourceFile && sourceFile->Kind == SourceFileKind::SIL) {
3070+
// Don't verify for types in SIL files.
3071+
return;
3072+
}
3073+
}
30643074
}
30653075
}
30663076

@@ -3086,6 +3096,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
30863096
// unconditionally but does in this case
30873097
// (8) being or containing the error type
30883098
// (9) explicitly suppressing conformance
3099+
// (10) a layout constrained archetype
30893100
bool hasNoNonconformingNode = visitAggregateLeaves(
30903101
origType, substType, forExpansion,
30913102
/*isLeafAggregate=*/
@@ -3155,7 +3166,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
31553166

31563167
// ModuleTypes are trivial but don't warrant being given a
31573168
// conformance to BitwiseCopyable (case (3)).
3158-
if (isa<ModuleType, SILTokenType>(ty)) {
3169+
if (isa<ModuleType>(ty) || isa<SILTokenType>(ty)) {
31593170
// These types should never appear within aggregates.
31603171
assert(isTopLevel && "aggregate containing marker type!?");
31613172
// If they did, though, they would not justify the aggregate's
@@ -3174,13 +3185,21 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
31743185
return !isTopLevel;
31753186
}
31763187

3188+
// Case (10): a layout-constrained archetype.
3189+
if (auto archetype = dyn_cast<ArchetypeType>(ty)) {
3190+
auto constraint = archetype->getLayoutConstraint();
3191+
if (constraint && constraint->isTrivial()) {
3192+
return false;
3193+
}
3194+
}
3195+
31773196
auto *nominal = ty.getAnyNominal();
31783197

31793198
// Non-nominal types (besides case (3) handled above) are trivial iff
31803199
// conforming.
31813200
if (!nominal) {
31823201
llvm::errs()
3183-
<< "Non-nominal type without conformance to _BitwiseCopyable:\n"
3202+
<< "Non-nominal type without conformance to BitwiseCopyable:\n"
31843203
<< ty << "\n"
31853204
<< "within " << substType << "\n"
31863205
<< "of " << origType << "\n";
@@ -3291,7 +3310,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
32913310
return true;
32923311
});
32933312
if (hasNoConformingArchetypeNode) {
3294-
llvm::errs() << "Non-trivial type with _BitwiseCopyable conformance!?:\n"
3313+
llvm::errs() << "Non-trivial type with BitwiseCopyable conformance!?:\n"
32953314
<< substType << "\n";
32963315
conformance.print(llvm::errs());
32973316
llvm::errs() << "\n"

lib/Sema/TypeCheckBitwise.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ static bool checkBitwiseCopyableInstanceStorage(NominalTypeDecl *nominal,
237237
return true;
238238
}
239239

240+
auto &attrs = nominal->getAttrs();
241+
if (attrs.hasAttribute<SensitiveAttr>()) {
242+
if (!isImplicit(check)) {
243+
conformanceDecl->diagnose(diag::non_bitwise_copyable_type_sensitive);
244+
}
245+
return true;
246+
}
247+
240248
if (dc->mapTypeIntoContext(nominal->getDeclaredInterfaceType())
241249
->isNoncopyable()) {
242250
// Already separately diagnosed when explicit.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ ExistentialConformsToSelfRequest::evaluate(Evaluator &evaluator,
682682
ProtocolDecl *decl) const {
683683
// Marker protocols always self-conform.
684684
if (decl->isMarkerProtocol()) {
685-
// Except for BitwiseCopyable an existential of which is non-trivial.
686-
if (decl->getASTContext().LangOpts.hasFeature(Feature::BitwiseCopyable) &&
687-
decl->getKnownProtocolKind() == KnownProtocolKind::BitwiseCopyable) {
685+
// Except for BitwiseCopyable an existential of which is not bitwise
686+
// copyable.
687+
if (decl->getKnownProtocolKind() == KnownProtocolKind::BitwiseCopyable) {
688688
return false;
689689
}
690690
return true;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6117,11 +6117,9 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
61176117
break;
61186118
}
61196119
case KnownProtocolKind::BitwiseCopyable: {
6120-
if (Context.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
6121-
checkBitwiseCopyableConformance(
6122-
conformance, /*isImplicit=*/conformance->getSourceKind() ==
6123-
ConformanceEntryKind::Synthesized);
6124-
}
6120+
checkBitwiseCopyableConformance(
6121+
conformance, /*isImplicit=*/conformance->getSourceKind() ==
6122+
ConformanceEntryKind::Synthesized);
61256123
break;
61266124
}
61276125
default:

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,7 @@ list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-experimental-concis
315315
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
316316
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros")
317317
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Extern")
318-
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BitwiseCopyable")
319318
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BorrowingSwitch")
320-
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "ConformanceSuppression")
321319

322320
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
323321
set(swift_bin_dir "${CMAKE_BINARY_DIR}/bin")

stdlib/public/core/CommandLine.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@ internal func _swift_stdlib_getUnsafeArgvArgc(_: UnsafeMutablePointer<Int32>)
1919
-> UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>
2020

2121
/// Command-line arguments for the current process.
22-
#if $BitwiseCopyable && $ConformanceSuppression
2322
@frozen // namespace
24-
public enum CommandLine : ~_BitwiseCopyable {
23+
public enum CommandLine : ~BitwiseCopyable {
2524
}
26-
#else
27-
@frozen // namespace
28-
public enum CommandLine {
29-
}
30-
@available(*, unavailable)
31-
extension CommandLine : _BitwiseCopyable {}
32-
#endif
3325

3426
extension CommandLine {
3527
/// The backing static variable for argument count may come either from the

stdlib/public/core/KeyPath.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,12 +1855,12 @@ internal struct RawKeyPathComponent {
18551855
}
18561856
}
18571857

1858-
internal func _pop<T : _BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
1858+
internal func _pop<T : BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
18591859
as type: T.Type) -> T {
18601860
let buffer = _pop(from: &from, as: type, count: 1)
18611861
return buffer.baseAddress.unsafelyUnwrapped.pointee
18621862
}
1863-
internal func _pop<T : _BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
1863+
internal func _pop<T : BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
18641864
as: T.Type,
18651865
count: Int) -> UnsafeBufferPointer<T> {
18661866
from = MemoryLayout<T>._roundingUpBaseToAlignment(from)
@@ -3479,7 +3479,7 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor {
34793479
}
34803480
return (baseAddress, misalign)
34813481
}
3482-
mutating func pushDest<T : _BitwiseCopyable>(_ value: T) {
3482+
mutating func pushDest<T : BitwiseCopyable>(_ value: T) {
34833483
let size = MemoryLayout<T>.size
34843484
let (baseAddress, misalign) = adjustDestForAlignment(of: T.self)
34853485
_withUnprotectedUnsafeBytes(of: value) {

stdlib/public/core/MemoryLayout.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,8 @@
3939
/// let pointPointer = UnsafeMutableRawPointer.allocate(
4040
/// byteCount: count * MemoryLayout<Point>.stride,
4141
/// alignment: MemoryLayout<Point>.alignment)
42-
#if $BitwiseCopyable && $ConformanceSuppression
4342
@frozen // namespace
44-
public enum MemoryLayout<T: ~Copyable>: ~_BitwiseCopyable, Copyable {}
45-
#else
46-
@frozen // namespace
47-
public enum MemoryLayout<T: ~Copyable>: Copyable {}
48-
@available(*, unavailable)
49-
extension MemoryLayout: _BitwiseCopyable {}
50-
#endif
43+
public enum MemoryLayout<T: ~Copyable>: ~BitwiseCopyable, Copyable {}
5144

5245
extension MemoryLayout where T: ~Copyable {
5346
/// The contiguous memory footprint of `T`, in bytes.

stdlib/public/core/Misc.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
175175
@_marker public protocol Escapable {}
176176

177177
#if $NoncopyableGenerics && $NonescapableTypes
178-
@_marker public protocol _BitwiseCopyable: ~Escapable { }
178+
@_marker public protocol BitwiseCopyable: ~Escapable { }
179179
#else
180-
@_marker public protocol _BitwiseCopyable { }
180+
@_marker public protocol BitwiseCopyable { }
181181
#endif
182+
183+
@available(*, unavailable)
184+
@_marker public protocol _BitwiseCopyable {}

stdlib/public/core/Optional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extension Optional: Copyable /* where Wrapped: Copyable */ {}
136136

137137
extension Optional: Sendable where Wrapped: ~Copyable & Sendable { }
138138

139-
extension Optional: _BitwiseCopyable where Wrapped: _BitwiseCopyable { }
139+
extension Optional: BitwiseCopyable where Wrapped: BitwiseCopyable { }
140140

141141
@_preInverseGenerics
142142
extension Optional: ExpressibleByNilLiteral where Wrapped: ~Copyable {

stdlib/public/core/Pointer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public protocol _Pointer:
3030
Strideable,
3131
_CustomDebugStringConvertibleOrNone,
3232
_CustomReflectableOrNone,
33-
_BitwiseCopyable
33+
BitwiseCopyable
3434
{
3535
/// A type that represents the distance between two pointers.
3636
typealias Distance = Int

stdlib/public/core/SIMDVector.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ prefix operator .!
3131
/// elementwise accesses. Computational operations are defined on the `SIMD`
3232
/// protocol, which refines this protocol, and on the concrete types that
3333
/// conform to `SIMD`.
34-
public protocol SIMDStorage : _BitwiseCopyable {
34+
public protocol SIMDStorage : BitwiseCopyable {
3535
/// The type of scalars in the vector space.
3636
#if $Embedded
3737
associatedtype Scalar: Hashable
@@ -64,7 +64,7 @@ extension SIMDStorage {
6464
}
6565

6666
/// A type that can be used as an element in a SIMD vector.
67-
public protocol SIMDScalar : _BitwiseCopyable {
67+
public protocol SIMDScalar : BitwiseCopyable {
6868
associatedtype SIMDMaskScalar: SIMDScalar & FixedWidthInteger & SignedInteger
6969
where SIMDMaskScalar.SIMDMaskScalar == SIMDMaskScalar
7070
associatedtype SIMD2Storage: SIMDStorage where SIMD2Storage.Scalar == Self
@@ -81,7 +81,7 @@ public protocol SIMD<Scalar>:
8181
SIMDStorage,
8282
Hashable,
8383
ExpressibleByArrayLiteral,
84-
_BitwiseCopyable
84+
BitwiseCopyable
8585
{
8686
/// The mask type resulting from pointwise comparisons of this vector type.
8787
associatedtype MaskStorage: SIMD
@@ -97,7 +97,7 @@ public protocol SIMD<Scalar>:
9797
Hashable,
9898
CustomStringConvertible,
9999
ExpressibleByArrayLiteral,
100-
_BitwiseCopyable
100+
BitwiseCopyable
101101
{
102102
/// The mask type resulting from pointwise comparisons of this vector type.
103103
associatedtype MaskStorage: SIMD

stdlib/public/core/Unicode.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,5 @@ public func transcode<Input, InputEncoding, OutputEncoding>(
671671
}
672672

673673
/// A namespace for Unicode utilities.
674-
#if $BitwiseCopyable && $ConformanceSuppression
675674
@frozen
676-
public enum Unicode : ~_BitwiseCopyable {}
677-
#else
678-
@frozen
679-
public enum Unicode {}
680-
#endif
681-
675+
public enum Unicode : ~BitwiseCopyable {}

0 commit comments

Comments
 (0)