mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This attribute can be attached to a noncopyable struct to specify that its storage is raw, meaning the type definition is (with some limitations) able to do as it pleases with the storage. This provides a basis for implementing types for things like atomics, locks, and data structures that use inline storage to store conditionally-initialized values. The example in `test/Prototypes/UnfairLock.swift` demonstrates the use of a raw layout type to wrap Darwin's `os_unfair_lock` APIs, allowing a lock value to be stored inside of classes or other types without needing a separate allocation, and using the borrow model to enforce safe access to lock-guarded storage.
35 lines
1.0 KiB
Swift
35 lines
1.0 KiB
Swift
// RUN: %target-swift-frontend -enable-experimental-feature StrictConcurrency -enable-experimental-feature RawLayout -typecheck -verify %s
|
|
|
|
func checkSendable(_: @Sendable () -> ()) {}
|
|
|
|
@_rawLayout(size: 4, alignment: 4)
|
|
struct NotAutomaticallySendableAndNotUsedAsSendable: ~Copyable {}
|
|
|
|
@_rawLayout(size: 4, alignment: 4)
|
|
struct NotAutomaticallySendable: ~Copyable {} // expected-note{{}}
|
|
|
|
func testNotAutomaticallySendable() {
|
|
let s = NotAutomaticallySendable()
|
|
|
|
checkSendable { _ = s } // expected-warning{{capture of 's' with non-sendable type 'NotAutomaticallySendable'}}
|
|
}
|
|
|
|
@_rawLayout(size: 4, alignment: 4)
|
|
struct UnuncheckedSendable: ~Copyable, Sendable {} // expected-warning{{@_rawLayout does not conform to the 'Sendable' protocol}}
|
|
|
|
func testUnuncheckedSendable() {
|
|
let s = UnuncheckedSendable()
|
|
|
|
checkSendable { _ = s }
|
|
}
|
|
|
|
@_rawLayout(size: 4, alignment: 4)
|
|
struct UncheckedSendable: ~Copyable, @unchecked Sendable {}
|
|
|
|
func testUncheckedSendable() {
|
|
let s = UncheckedSendable()
|
|
|
|
checkSendable { _ = s }
|
|
}
|
|
|