Skip to content

Commit

Permalink
Merge pull request #72436 from lorentey/noncopyable-primitives-6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lorentey authored Mar 20, 2024
2 parents 729253c + 9507314 commit c4e726a
Show file tree
Hide file tree
Showing 42 changed files with 2,123 additions and 709 deletions.
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

0 comments on commit c4e726a

Please sign in to comment.