Files
swift-mirror/test/ModuleInterface/Inputs/lifetime_dependence.swift
Andrew Trick b5b0c75ccd Remove diagnostic: lifetime_dependence_on_bitwise_copyable
Allow lifetime depenendence on types that are BitwiseCopyable & Escapable.

This is unsafe in the sense that the compiler will not diagnose any use of the
dependent value outside of the lexcial scope of the source value. But, in
practice, dependence on an UnsafePointer is often needed. In that case, the
programmer should have already taken responsibility for ensuring the lifetime of the
pointer over all dependent uses. Typically, an unsafe pointer is valid for the
duration of a closure. Lifetime dependence prevents the dependent value from
being returned by the closure, so common usage is safe by default.

Typical example:

func decode(_ bufferRef: Span<Int>) { /*...*/ }

extension UnsafeBufferPointer {
  // The client must ensure the lifetime of the buffer across the invocation of `body`.
  // The client must ensure that no code modifies the buffer during the invocation of `body`.
  func withUnsafeSpan<Result>(_ body: (Span<Element>) throws -> Result) rethrows -> Result {
    // Construct Span using its internal, unsafe API.
    try body(Span(unsafePointer: baseAddress!, count: count))
  }
}

func decodeArrayAsUBP(array: [Int]) {
  array.withUnsafeBufferPointer { buffer in
    buffer.withUnsafeSpan {
      decode($0)
    }
  }
}

In the future, we may add SILGen support for tracking the lexical scope of
BitwiseCopyable values. That would allow them to have the same dependence
behavior as other source values.
2024-05-22 17:10:56 -07:00

52 lines
1.4 KiB
Swift

public struct AnotherView : ~Escapable {
@usableFromInline let _ptr: UnsafeRawBufferPointer
@usableFromInline let _count: Int
internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) -> dependsOn(ptr) Self {
self._ptr = ptr
self._count = count
}
}
public struct BufferView : ~Escapable {
@usableFromInline let _ptr: UnsafeRawBufferPointer
@usableFromInline let _count: Int
@usableFromInline
internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) -> dependsOn(ptr) Self {
self._ptr = ptr
self._count = count
}
@inlinable
internal init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) -> dependsOn(a) Self {
self.init(ptr, a.count)
return self
}
@inlinable
internal init(_ ptr: UnsafeRawBufferPointer, _ a: consuming AnotherView) -> dependsOn(a) Self {
self.init(ptr, a._count)
return self
}
}
@inlinable
public func derive(_ x: consuming BufferView) -> dependsOn(x) BufferView {
return BufferView(x._ptr, x._count)
}
@inlinable
public func use(_ x: consuming BufferView) {}
@inlinable
public func consumeAndCreate(_ view: consuming BufferView) -> dependsOn(view) BufferView {
return BufferView(view._ptr, view._count)
}
@inlinable
public func deriveThisOrThat(_ this: consuming BufferView, _ that: consuming BufferView) -> dependsOn(this, that) BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(this._ptr, this._count)
}
return BufferView(that._ptr, that._count)
}