Files
swift-mirror/test/SILOptimizer/lifetime_dependence_inherit.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

85 lines
1.8 KiB
Swift

// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -o /dev/null \
// RUN: -verify \
// RUN: -sil-verify-all \
// RUN: -module-name test \
// RUN: -enable-experimental-feature NonescapableTypes
// REQUIRES: asserts
// REQUIRES: swift_in_compiler
struct BV : ~Escapable {
let p: UnsafeRawPointer
let i: Int
init(_ p: UnsafeRawPointer, _ i: Int) -> dependsOn(p) Self {
self.p = p
self.i = i
}
@_unsafeNonescapableResult
init(independent p: UnsafeRawPointer, _ i: Int) {
self.p = p
self.i = i
}
consuming func derive() -> dependsOn(self) BV {
// Technically, this "new" view does not depend on the 'view' argument.
// This unsafely creates a new view with no dependence.
return BV(independent: self.p, self.i)
}
}
// Nonescapable wrapper.
struct NEBV : ~Escapable {
var bv: BV
// Test lifetime inheritance through initialization.
init(_ bv: consuming BV) {
self.bv = bv
}
var view: BV {
_read {
yield bv
}
_modify {
yield &bv
}
}
func borrowedView() -> dependsOn(scoped self) BV {
bv
}
}
// Test lifetime inheritance through chained consumes.
func bv_derive(bv: consuming BV) -> dependsOn(bv) BV {
bv.derive()
}
// Test lifetime inheritance through stored properties.
func ne_extract_member(nebv: consuming NEBV) -> dependsOn(nebv) BV {
return nebv.bv
}
func ne_yield_member(nebv: consuming NEBV) -> dependsOn(nebv) BV {
return nebv.view
}
func bv_consume(_ x: consuming BV) {}
// It is ok to consume the aggregate before the property.
// The property's lifetime must exceed the aggregate's.
func nebv_consume_member(nebv: consuming NEBV) {
let bv = nebv.bv
_ = consume nebv
bv_consume(bv)
}
func nebv_consume_after_yield(nebv: consuming NEBV) {
let view = nebv.view
_ = consume nebv
bv_consume(view)
}