mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Update availability for CxxSpan<->Span, fix lifetimebound on parameters with reference type Because swift-ide-test doesn't care about typechecking, std-span-interface.swift passed despite containing 2 separate errors. This updates the test file to properly exercise the entire compilation pipeline for the macro expansions, by running swift-frontend -emit-module and calling each macro expansion. The first issue was that CxxSpan initializers taking [Mutable]Span still had their availability set to Swift 6.2+, even after back-deploying caused [Mutable]Span to have availability back to Swift 5.0. Since _SwiftifyImport expansions copy the availbility of Span, this resulted in the macro expansions calling unavailable initializers. Interestingly enough, this manifested itself in the form of a tripped assert in SIL verification, because although we do now typecheck the expansions from _SwiftifyImport, the compilation can still keep going after `shouldEmitFunctionBody` returns false: the macro expansion declaration is still there, but is now missing its definition, despite not being external. The second issue was when parameters with C++ reference types were annotated with `[[clang::lifetimebound]]`. For parameters with a type that is `Escapable`, this is normally done using `@lifetime(borrow foo)`. However C++ reference parameters are imported as `inout`, which requires the `@lifetime(&foo)` syntax. rdar://151493400 rdar://151678415
164 lines
5.7 KiB
Swift
164 lines
5.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2023 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
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Builtin
|
|
|
|
@usableFromInline
|
|
internal func unsafeBitCast<T: ~Escapable & ~Copyable, U>(
|
|
_ x: consuming T, to type: U.Type
|
|
) -> U {
|
|
Builtin.reinterpretCast(x)
|
|
}
|
|
|
|
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
|
|
/// a value identical to `dependent` with a lifetime dependency on the caller's
|
|
/// borrow scope of the `source` argument.
|
|
///
|
|
/// This mimics the stdlib definition. It is public for use with import macros.
|
|
@unsafe
|
|
@_unsafeNonescapableResult
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
@lifetime(borrow source)
|
|
public func _cxxOverrideLifetime<
|
|
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
|
|
>(
|
|
_ dependent: consuming T, borrowing source: borrowing U
|
|
) -> T {
|
|
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
|
|
// should be expressed by a builtin that is hidden within the function body.
|
|
dependent
|
|
}
|
|
|
|
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
|
|
/// a value identical to `dependent` that inherits all lifetime dependencies from
|
|
/// the `source` argument.
|
|
///
|
|
/// This mimics the stdlib definition. It is public for use with import macros.
|
|
@unsafe
|
|
@_unsafeNonescapableResult
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
@lifetime(copy source)
|
|
public func _cxxOverrideLifetime<
|
|
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
|
|
>(
|
|
_ dependent: consuming T, copying source: borrowing U
|
|
) -> T {
|
|
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
|
|
// should be expressed by a builtin that is hidden within the function body.
|
|
dependent
|
|
}
|
|
|
|
/// A C++ type that is an object that can refer to a contiguous sequence of objects.
|
|
///
|
|
/// C++ standard library type `std::span` conforms to this protocol.
|
|
public protocol CxxSpan<Element> {
|
|
associatedtype Element
|
|
associatedtype Size: BinaryInteger
|
|
|
|
init()
|
|
init(_ unsafePointer : UnsafePointer<Element>, _ count: Size)
|
|
|
|
func size() -> Size
|
|
func __dataUnsafe() -> UnsafePointer<Element>?
|
|
}
|
|
|
|
extension CxxSpan {
|
|
/// Creates a C++ span from a Swift UnsafeBufferPointer
|
|
@inlinable
|
|
public init(_ unsafeBufferPointer: UnsafeBufferPointer<Element>) {
|
|
unsafe precondition(unsafeBufferPointer.baseAddress != nil,
|
|
"UnsafeBufferPointer should not point to nil")
|
|
unsafe self.init(unsafeBufferPointer.baseAddress!, Size(unsafeBufferPointer.count))
|
|
}
|
|
|
|
@inlinable
|
|
public init(_ unsafeMutableBufferPointer: UnsafeMutableBufferPointer<Element>) {
|
|
unsafe precondition(unsafeMutableBufferPointer.baseAddress != nil,
|
|
"UnsafeMutableBufferPointer should not point to nil")
|
|
unsafe self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
|
|
}
|
|
|
|
@available(SwiftCompatibilitySpan 5.0, *)
|
|
@inlinable
|
|
@unsafe
|
|
public init(_ span: Span<Element>) {
|
|
let (p, c) = unsafe unsafeBitCast(span, to: (UnsafeRawPointer?, Int).self)
|
|
unsafe precondition(p != nil, "Span should not point to nil")
|
|
let binding = unsafe p!.bindMemory(to: Element.self, capacity: c)
|
|
unsafe self.init(binding, Size(c))
|
|
}
|
|
}
|
|
|
|
@available(SwiftCompatibilitySpan 5.0, *)
|
|
extension Span {
|
|
@_alwaysEmitIntoClient
|
|
@unsafe
|
|
@_unsafeNonescapableResult
|
|
@lifetime(borrow span)
|
|
public init<T: CxxSpan<Element>>(
|
|
_unsafeCxxSpan span: borrowing T,
|
|
) {
|
|
let buffer = unsafe UnsafeBufferPointer(start: span.__dataUnsafe(), count: Int(span.size()))
|
|
let newSpan = unsafe Span(_unsafeElements: buffer)
|
|
// 'self' is limited to the caller's scope of the variable passed to the 'span' argument.
|
|
self = unsafe _cxxOverrideLifetime(newSpan, borrowing: span)
|
|
}
|
|
}
|
|
|
|
@available(SwiftCompatibilitySpan 5.0, *)
|
|
extension MutableSpan {
|
|
@_alwaysEmitIntoClient
|
|
@unsafe
|
|
@_unsafeNonescapableResult
|
|
@lifetime(borrow span)
|
|
public init<T: CxxMutableSpan<Element>>(
|
|
_unsafeCxxSpan span: borrowing T,
|
|
) {
|
|
let buffer = unsafe UnsafeMutableBufferPointer(start: span.__dataUnsafe(), count: Int(span.size()))
|
|
let newSpan = unsafe MutableSpan(_unsafeElements: buffer)
|
|
// 'self' is limited to the caller's scope of the variable passed to the 'span' argument.
|
|
self = unsafe _cxxOverrideLifetime(newSpan, borrowing: span)
|
|
}
|
|
}
|
|
|
|
public protocol CxxMutableSpan<Element> {
|
|
associatedtype Element
|
|
associatedtype Size: BinaryInteger
|
|
|
|
init()
|
|
init(_ unsafeMutablePointer : UnsafeMutablePointer<Element>, _ count: Size)
|
|
|
|
func size() -> Size
|
|
func __dataUnsafe() -> UnsafeMutablePointer<Element>?
|
|
}
|
|
|
|
extension CxxMutableSpan {
|
|
/// Creates a C++ span from a Swift UnsafeMutableBufferPointer
|
|
@inlinable
|
|
public init(_ unsafeMutableBufferPointer: UnsafeMutableBufferPointer<Element>) {
|
|
unsafe precondition(unsafeMutableBufferPointer.baseAddress != nil,
|
|
"UnsafeMutableBufferPointer should not point to nil")
|
|
unsafe self.init(unsafeMutableBufferPointer.baseAddress!, Size(unsafeMutableBufferPointer.count))
|
|
}
|
|
|
|
@available(SwiftCompatibilitySpan 5.0, *)
|
|
@inlinable
|
|
@unsafe
|
|
public init(_ span: consuming MutableSpan<Element>) {
|
|
let (p, c) = unsafe unsafeBitCast(span, to: (UnsafeMutableRawPointer?, Int).self)
|
|
unsafe precondition(p != nil, "Span should not point to nil")
|
|
let binding = unsafe p!.bindMemory(to: Element.self, capacity: c)
|
|
unsafe self.init(binding, Size(c))
|
|
}
|
|
}
|