Skip to content

[6.0][stdlib] Start adopting noncopyable generics in the stdlib #72436

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 55 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
364107b
[stdlib] MemoryLayout: Add support for non-copyable type arguments
lorentey Feb 16, 2024
11101dc
[stdlib] Unsafe[Mutable]Pointer: Add support for non-copyable Pointee…
lorentey Feb 16, 2024
f99e884
[stdlib] Optional: Initial support for noncopyable payloads
lorentey Feb 29, 2024
5d5db49
[stdlib] UnsafeRawPointer: new interactions with noncopyable types
lorentey Mar 5, 2024
63d4c37
[stdlib] OpaquePointer: new interactions with noncopyable types
lorentey Mar 5, 2024
b2224fe
[stdlib] MemoryLayout: Adopt @_preInverseGenerics
lorentey Mar 6, 2024
71bb2bd
[stdlib] UnsafePointer: Adopt @_preInverseGenerics
lorentey Mar 6, 2024
53bfa07
[stdlib] Optional: Adopt @_preInverseGenerics
lorentey Mar 6, 2024
4f57f83
[stdlib] UnsafeRawPointer: Adopt @_preInverseGenerics
lorentey Mar 6, 2024
2b6e773
[stdlib] OpaquePointer: Adopt @_preInverseGenerics
lorentey Mar 6, 2024
703c1e5
[stdlib] Generalize swap(_:_:) for noncopyable types
lorentey Mar 6, 2024
9445691
[stdlib] UnsafePointer: Add @_preInverseGenerics to stored properties
lorentey Mar 7, 2024
7aa4d9b
[stdlib] UnsafePointer: Add @_preInverseGenerics to _Pointer conformance
lorentey Mar 7, 2024
c405af7
[stdlib] Optional: Add @_preInverseGenerics to ExpressibleByNilLitera…
lorentey Mar 7, 2024
418c039
[stdlib] UnsafePointer: Don’t let the compiler synthesize `hashValue`
lorentey Mar 7, 2024
4598a09
[stdlib] UnsafePointer: Resolve most of the symbol mismatches
lorentey Mar 7, 2024
83f6e18
[Synchronization] Update symbol expectations
lorentey Mar 7, 2024
1380dc0
[stdlib] Unsafe[Mutable]BufferPointer: Add support for non-copyable P…
lorentey Mar 2, 2024
8277663
[stdlib] UnsafeRawBufferPointer: new interactions with noncopyable types
lorentey Mar 5, 2024
8cd54d6
[stdlib] Add support for noncopyables to swap(_:_:)
lorentey Mar 6, 2024
56f8e81
[stdlib] Result: Initial support for noncopyable payloads
lorentey Mar 5, 2024
9e45cac
[stdlib] Add support for noncopyables to withExtendedLifetime(of:) fa…
lorentey Mar 6, 2024
449129a
[stdlib] Add support for noncopyables to withUnsafePointer(to:) family
lorentey Mar 6, 2024
eed8b30
[stdlib] Temporarily disable round trip validation for debug types
lorentey Mar 12, 2024
4347ae2
[stdlib][abi] Add expectations for new property descriptors for U[M]B…
lorentey Mar 13, 2024
e58140c
[stdlib] Add partial ~Copyable support to withUnsafeTemporaryAllocati…
lorentey Mar 14, 2024
ca5b422
[stdlib] ManagedBuffer: Support for noncopyable Element
lorentey Mar 14, 2024
97aa6a0
[stdlib] Add workaround for unexpected ManagedBuffer ABI mismatch
lorentey Mar 14, 2024
d36d81e
[IRGen] Temporarily disable round-tripping debug types
lorentey Mar 14, 2024
8f0e1c2
[build] Enable noncopyable generics; disable anything that prevents t…
lorentey Mar 14, 2024
d256193
[stdlib] Update SwiftStdlibLegacyABI convention to use `@available(sw…
lorentey Mar 15, 2024
6f68ac1
[stdlib] withUnsafePointer: Spell out Error conformance on E
lorentey Mar 15, 2024
f915f2c
[stdlib] Make MemoryLayout techically copyable
lorentey Mar 15, 2024
4eac03d
[stdlib] Apply minor review notes
lorentey Mar 15, 2024
e0967b6
[stdlib] {consuming,borrowing}{Flat,}Map: adopt typed throws
lorentey Mar 15, 2024
7e43a51
[stdlib] Use hasFeature(Foo) instead of opaque $Foo syntax
lorentey Mar 15, 2024
6906d99
[stdlib] Apply @Azoy’s remaining review notes (thanks!)
lorentey Mar 15, 2024
864888c
[stdlib] Remove all new public API for now
lorentey Mar 15, 2024
340e7d3
[Synchronization] Generalize U[M]BP’s AtomicRepresentable conformance
lorentey Mar 15, 2024
30e4678
[util] swift-abi-symbol-checker: Don’t exit early for missing additions
lorentey Mar 15, 2024
7c4e38f
Revert "[build] Enable noncopyable generics; disable anything that pr…
lorentey Mar 15, 2024
92339b9
Revert "[IRGen] Temporarily disable round-tripping debug types"
lorentey Mar 15, 2024
db17bc5
[stdlib] Delete leftover workaround
lorentey Mar 15, 2024
1e003d2
[test] Update tests for new stdlib
lorentey Mar 15, 2024
c80862f
[stdlib] ManagedBuffer: Add @_preInverseGenerics for $Embedded
lorentey Mar 15, 2024
00948f4
[test] XFAIL two new nontrivial test failures
lorentey Mar 15, 2024
392fe28
[gardening] update copyright notice
glessard Mar 15, 2024
587b459
[abi] Update false positives list for api-digester based ABI/source s…
lorentey Mar 15, 2024
10da5ce
[test][IDE] Update code completion expectation
lorentey Mar 15, 2024
1d96db8
[test] Temporarily XFAIL tests that expect swiftinterface files to wo…
lorentey Mar 15, 2024
e561d13
[stdlib] Apply review notes from @glessard (thanks!)
lorentey Mar 15, 2024
b4afcd2
[test][abi] Remove expectations that got resolved by #72348
lorentey Mar 15, 2024
b9b552d
[test] Un-xfail tests that are passing now that NoncopyableGenerics i…
lorentey Mar 18, 2024
71178fe
[test] More test updates
lorentey Mar 18, 2024
9507314
[test] Fix mismerge in old ABI checker’s expectation list
lorentey Mar 18, 2024
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
35 changes: 23 additions & 12 deletions stdlib/public/Cxx/UnsafeCxxIterators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -18,12 +18,13 @@
///
/// - SeeAlso: https://en.cppreference.com/w/cpp/named_req/InputIterator
public protocol UnsafeCxxInputIterator: Equatable {
associatedtype Pointee
associatedtype Pointee: ~Copyable

/// Returns the unwrapped result of C++ `operator*()`.
///
/// Generally, Swift creates this property automatically for C++ types that
/// define `operator*()`.
@_borrowed
var pointee: Pointee { get }

/// Returns an iterator pointing to the next item in the sequence.
Expand All @@ -33,19 +34,23 @@ public protocol UnsafeCxxInputIterator: Equatable {
func successor() -> Self
}

extension UnsafePointer: UnsafeCxxInputIterator {}
extension UnsafePointer: UnsafeCxxInputIterator
where Pointee: ~Copyable {}

extension UnsafeMutablePointer: UnsafeCxxInputIterator {}
extension UnsafeMutablePointer: UnsafeCxxInputIterator
where Pointee: ~Copyable {}

extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator {
public typealias Pointee = Wrapped.Pointee

@inlinable
public var pointee: Pointee {
if let value = self {
return value.pointee
_read {
guard let value = self else {
fatalError("Could not dereference nullptr")
}
yield value.pointee
}
fatalError("Could not dereference nullptr")
}

@inlinable
Expand All @@ -58,10 +63,12 @@ extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator
}

public protocol UnsafeCxxMutableInputIterator: UnsafeCxxInputIterator {
@_borrowed
override var pointee: Pointee { get set }
}

extension UnsafeMutablePointer: UnsafeCxxMutableInputIterator {}
extension UnsafeMutablePointer: UnsafeCxxMutableInputIterator
where Pointee: ~Copyable {}

/// Bridged C++ iterator that allows computing the distance between two of its
/// instances, and advancing an instance by a given number of elements.
Expand All @@ -77,10 +84,14 @@ public protocol UnsafeCxxRandomAccessIterator: UnsafeCxxInputIterator {
static func +=(lhs: inout Self, rhs: Distance)
}

extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
extension UnsafePointer: UnsafeCxxRandomAccessIterator
where Pointee: ~Copyable {}

extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator
where Pointee: ~Copyable {}

public protocol UnsafeCxxMutableRandomAccessIterator: UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}
public protocol UnsafeCxxMutableRandomAccessIterator:
UnsafeCxxRandomAccessIterator, UnsafeCxxMutableInputIterator {}

extension UnsafeMutablePointer: UnsafeCxxMutableRandomAccessIterator {}
extension UnsafeMutablePointer: UnsafeCxxMutableRandomAccessIterator
where Pointee: ~Copyable {}
17 changes: 10 additions & 7 deletions stdlib/public/Synchronization/AtomicPointers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -15,7 +15,7 @@
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension UnsafePointer: AtomicRepresentable {
extension UnsafePointer: AtomicRepresentable where Pointee: ~Copyable {
/// The storage representation type that `Self` encodes to and decodes from
/// which is a suitable type when used in atomic operations.
@available(SwiftStdlib 6.0, *)
Expand Down Expand Up @@ -65,7 +65,7 @@ extension UnsafePointer: AtomicRepresentable {
}

@available(SwiftStdlib 6.0, *)
extension UnsafePointer: AtomicOptionalRepresentable {
extension UnsafePointer: AtomicOptionalRepresentable where Pointee: ~Copyable {
/// The storage representation type that encodes to and decodes from
/// `Optional<Self>` which is a suitable type when used in atomic operations
/// on `Optional`.
Expand Down Expand Up @@ -121,7 +121,7 @@ extension UnsafePointer: AtomicOptionalRepresentable {
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 6.0, *)
extension UnsafeMutablePointer: AtomicRepresentable {
extension UnsafeMutablePointer: AtomicRepresentable where Pointee: ~Copyable {
/// The storage representation type that `Self` encodes to and decodes from
/// which is a suitable type when used in atomic operations.
@available(SwiftStdlib 6.0, *)
Expand Down Expand Up @@ -171,7 +171,8 @@ extension UnsafeMutablePointer: AtomicRepresentable {
}

@available(SwiftStdlib 6.0, *)
extension UnsafeMutablePointer: AtomicOptionalRepresentable {
extension UnsafeMutablePointer: AtomicOptionalRepresentable
where Pointee: ~Copyable {
/// The storage representation type that encodes to and decodes from
/// `Optional<Self>` which is a suitable type when used in atomic operations
/// on `Optional`.
Expand Down Expand Up @@ -773,7 +774,7 @@ extension ObjectIdentifier: AtomicOptionalRepresentable {
#if (_pointerBitWidth(_32) && _hasAtomicBitWidth(_64)) || (_pointerBitWidth(_64) && _hasAtomicBitWidth(_128))

@available(SwiftStdlib 6.0, *)
extension UnsafeBufferPointer: AtomicRepresentable {
extension UnsafeBufferPointer: AtomicRepresentable where Element: ~Copyable {
/// The storage representation type that `Self` encodes to and decodes from
/// which is a suitable type when used in atomic operations.
@available(SwiftStdlib 6.0, *)
Expand Down Expand Up @@ -839,7 +840,9 @@ extension UnsafeBufferPointer: AtomicRepresentable {
#if (_pointerBitWidth(_32) && _hasAtomicBitWidth(_64)) || (_pointerBitWidth(_64) && _hasAtomicBitWidth(_128))

@available(SwiftStdlib 6.0, *)
extension UnsafeMutableBufferPointer: AtomicRepresentable {
extension UnsafeMutableBufferPointer: AtomicRepresentable
where Element: ~Copyable
{
/// The storage representation type that `Self` encodes to and decodes from
/// which is a suitable type when used in atomic operations.
@available(SwiftStdlib 6.0, *)
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros")
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Extern")
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BitwiseCopyable")
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BorrowingSwitch")

if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
set(swift_bin_dir "${CMAKE_BINARY_DIR}/bin")
Expand Down
34 changes: 26 additions & 8 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -150,38 +150,58 @@ public struct OpaquePointer {
internal init(_ v: Builtin.RawPointer) {
self._rawValue = v
}
}

@available(*, unavailable)
extension OpaquePointer: Sendable {}

/// Creates an `OpaquePointer` from a given address in memory.
extension OpaquePointer {
/// Creates a new `OpaquePointer` from the given address, specified as a bit
/// pattern.
///
/// - Parameter bitPattern: A bit pattern to use for the address of the new
/// pointer. If `bitPattern` is zero, the result is `nil`.
@_transparent
public init?(bitPattern: Int) {
if bitPattern == 0 { return nil }
self._rawValue = Builtin.inttoptr_Word(bitPattern._builtinWordValue)
}

/// Creates an `OpaquePointer` from a given address in memory.
/// Creates a new `OpaquePointer` from the given address, specified as a bit
/// pattern.
///
/// - Parameter bitPattern: A bit pattern to use for the address of the new
/// pointer. If `bitPattern` is zero, the result is `nil`.
@_transparent
public init?(bitPattern: UInt) {
if bitPattern == 0 { return nil }
self._rawValue = Builtin.inttoptr_Word(bitPattern._builtinWordValue)
}
}

extension OpaquePointer {
/// Converts a typed `UnsafePointer` to an opaque C pointer.
@_transparent
public init<T>(@_nonEphemeral _ from: UnsafePointer<T>) {
@_preInverseGenerics
public init<T: ~Copyable>(@_nonEphemeral _ from: UnsafePointer<T>) {
self._rawValue = from._rawValue
}

/// Converts a typed `UnsafePointer` to an opaque C pointer.
///
/// The result is `nil` if `from` is `nil`.
@_transparent
public init?<T>(@_nonEphemeral _ from: UnsafePointer<T>?) {
@_preInverseGenerics
public init?<T: ~Copyable>(@_nonEphemeral _ from: UnsafePointer<T>?) {
guard let unwrapped = from else { return nil }
self.init(unwrapped)
}
}

extension OpaquePointer {
/// Converts a typed `UnsafeMutablePointer` to an opaque C pointer.
@_transparent
@_preInverseGenerics
public init<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>) {
self._rawValue = from._rawValue
}
Expand All @@ -190,6 +210,7 @@ public struct OpaquePointer {
///
/// The result is `nil` if `from` is `nil`.
@_transparent
@_preInverseGenerics
public init?<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>?) {
guard let unwrapped = from else { return nil }
self.init(unwrapped)
Expand All @@ -215,9 +236,6 @@ extension OpaquePointer: Hashable {
}
}

@available(*, unavailable)
extension OpaquePointer : Sendable { }

@_unavailableInEmbedded
extension OpaquePointer: CustomDebugStringConvertible {
/// A textual representation of the pointer, suitable for debugging.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/CompilerProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -266,7 +266,7 @@ public protocol CaseIterable {
/// `Optional` type conforms to `ExpressibleByNilLiteral`.
/// `ExpressibleByNilLiteral` conformance for types that use `nil` for other
/// purposes is discouraged.
public protocol ExpressibleByNilLiteral {
public protocol ExpressibleByNilLiteral: ~Copyable {
/// Creates an instance initialized with `nil`.
init(nilLiteral: ())
}
Expand Down
Loading