mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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.
38 lines
1.5 KiB
Swift
38 lines
1.5 KiB
Swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes
|
|
// REQUIRES: asserts
|
|
|
|
struct BufferView : ~Escapable, ~Copyable {
|
|
let ptr: UnsafeRawBufferPointer?
|
|
let c: Int
|
|
init(_ ptr: UnsafeRawBufferPointer?, _ c: Int) -> dependsOn(ptr) Self {
|
|
self.ptr = ptr
|
|
self.c = c
|
|
}
|
|
}
|
|
|
|
struct ImplicitInit1 : ~Escapable { // expected-error{{cannot infer lifetime dependence on implicit initializer, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
|
|
let ptr: UnsafeRawBufferPointer
|
|
}
|
|
|
|
struct ImplicitInit2 : ~Escapable, ~Copyable {
|
|
let mbv: BufferView
|
|
}
|
|
|
|
struct ImplicitInit3 : ~Escapable, ~Copyable { // expected-error{{cannot infer lifetime dependence on implicit initializer, multiple parameters qualifiy as a candidate}}
|
|
let mbv1: BufferView
|
|
let mbv2: BufferView
|
|
}
|
|
|
|
func foo1() -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
|
|
return BufferView(nil, 0)
|
|
}
|
|
|
|
func foo2(_ i: borrowing Int) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
|
|
return BufferView(nil, 0)
|
|
}
|
|
|
|
func foo3<T: BitwiseCopyable>(arg: borrowing T) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
|
|
return BufferView(nil, 0)
|
|
}
|
|
|