Skip to content

Commit 144c927

Browse files
committed
[BitwiseCopyable] Don't infer for exported types.
Types exported from modules built with library evolution may be trivial during one build and subsequently cease to be trivial. That means the compiler must not infer the conformance for types exported from resilient modules. To avoid dialecticization based on whether a module is built in that way, require the annotation for all exported types.
1 parent 2db89d8 commit 144c927

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

proposals/nnnn-bitwise-copyable.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ We propose a new marker protocol `BitwiseCopyable` that can be conformed to by t
1919
When compiling generic code with such constraints, the compiler can emit these efficient operations directly, only requiring minimal overhead to look up the size of the value at runtime.
2020
Alternatively, developers can use this constraint to selectively provide high-performance variations of specific operations, such as bulk copying of a container.
2121

22-
[^1]: The term "trivial" is used in [SE-138](0138-unsaferawbufferpointer.md) and [SE-0370](0370-pointer-family-initialization-improvements.md) to refer to types with the property above. The discussion below will explain why certain generic or resilient types that are trivial will not in fact be `BitwiseCopyable`.
22+
[^1]: The term "trivial" is used in [SE-138](0138-unsaferawbufferpointer.md) and [SE-0370](0370-pointer-family-initialization-improvements.md) to refer to types with the property above. The discussion below will explain why certain generic or exported types that are trivial will not in fact be `BitwiseCopyable`.
2323

2424
## Motivation
2525

@@ -58,7 +58,7 @@ Many basic types in the standard library will conformed to this protocol.
5858
Developer's own types may be conformed to the protocol, as well.
5959
The compiler will check any such conformance and emit a diagnostic if the type contains elements that are not `BitwiseCopyable`.
6060

61-
Furthermore, when building a module, the compiler will infer conformance to `BitwiseCopyable` for any non-resilient struct or enum defined within the module whose stored members are all `BitwiseCopyable`.
61+
Furthermore, when building a module, the compiler will infer conformance to `BitwiseCopyable` for any non-exported struct or enum defined within the module whose stored members are all `BitwiseCopyable`.
6262

6363
Developers cannot conform types defined in other modules to the protocol.
6464

@@ -146,14 +146,16 @@ For generic types, a conformance will only be inferred if its fields uncondition
146146
In the `RegularBox` example above, a conditional conformance will not be inferred.
147147
If this is desired, the developer can explicitly write the conditional conformance.
148148

149-
### Inference for types in evolving libraries
149+
### Inference for exported types
150150

151-
This does not apply to public (or `@usableFromInline`) types defined within a module built with library evolution.
152-
While all the type's fields may be `BitwiseCopyable` at the moment, the compiler can't predict that they will always be.
151+
This does not apply to exported (`public`, `package`, or `@usableFromInline`) types.
152+
In the case of a library built with library evolution, while all the type's fields may be `BitwiseCopyable` at the moment, the compiler can't predict that they will always be.
153153
If this is the developer's intent, they can explicitly conform the type.
154+
To avoid having semantics that vary based on library evolution, the same applies to all exported (`public`, `package`, or `@usableFromInline`) types.
155+
156+
For `@frozen` types, however, `BitwiseCopyable` conformance will be inferred.
157+
That's allowed, even in the case of a library built with library evolution, because the compiler can see that the type's fields are all `BitwiseCopyable` and knows that they will remain that way.
154158

155-
For `@frozen` types, however, `BitwiseCopyable` conformance will be inferred even when in a module built with library evolution.
156-
That's because the compiler can see that the type's fields are all `BitwiseCopyable` and knows that they will remain that way.
157159
For example, the compiler will infer a conformance of the following struct
158160
```swift
159161
@frozen

0 commit comments

Comments
 (0)