1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// RUN: %target-swift-frontend %s \
// RUN: -emit-sil \
// RUN: -enable-builtin-module \
// RUN: -enable-experimental-feature NonescapableTypes | %FileCheck %s
import Builtin
struct BufferView : ~Escapable {
let ptr: UnsafeRawBufferPointer
@_unsafeNonescapableResult
init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
@_unsafeNonescapableResult
init?(_ ptr: UnsafeRawBufferPointer, _ i: Int) {
if (i % 2 == 0) {
return nil
}
self.ptr = ptr
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_SaySiGhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @guaranteed Array<Int>, @thin BufferView.Type) -> _scope(1) @owned BufferView {
init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) -> dependsOn(a) Self {
self.ptr = ptr
return self
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVYlitcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @thin BufferView.Type) -> _inherit(1) @owned BufferView {
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper) -> dependsOn(a) Self {
self.ptr = ptr
return self
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVYliSaySiGhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @guaranteed Array<Int>, @thin BufferView.Type) -> _inherit(1) _scope(2) @owned BufferView {
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper, _ b: borrowing Array<Int>) -> dependsOn(a) dependsOn(b) Self {
self.ptr = ptr
return self
}
}
struct MutableBufferView : ~Escapable, ~Copyable {
let ptr: UnsafeMutableRawBufferPointer
@_unsafeNonescapableResult
init(_ ptr: UnsafeMutableRawBufferPointer) {
self.ptr = ptr
}
}
func testBasic() {
let capacity = 4
let a = Array(0..<capacity)
a.withUnsafeBytes {
let view = BufferView($0)
let derivedView = derive(view)
let newView = consumeAndCreate(derivedView)
use(newView)
}
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers6deriveyAA10BufferViewVADYlsF : $@convention(thin) (@guaranteed BufferView) -> _scope(0) @owned BufferView {
func derive(_ x: borrowing BufferView) -> dependsOn(scoped x) BufferView {
return BufferView(x.ptr)
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers16consumeAndCreateyAA10BufferViewVADnYliF : $@convention(thin) (@owned BufferView) -> _inherit(0) @owned BufferView {
func consumeAndCreate(_ x: consuming BufferView) -> dependsOn(x) BufferView {
return BufferView(x.ptr)
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers17deriveThisOrThat1yAA10BufferViewVADYls_ADYlstF : $@convention(thin) (@guaranteed BufferView, @guaranteed BufferView) -> _scope(0, 1) @owned BufferView {
func deriveThisOrThat1(_ this: borrowing BufferView, _ that: borrowing BufferView) -> dependsOn(scoped this, that) BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(this.ptr)
}
return BufferView(that.ptr)
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers17deriveThisOrThat2yAA10BufferViewVADYls_ADnYlitF : $@convention(thin) (@guaranteed BufferView, @owned BufferView) -> _inherit(1) _scope(0) @owned BufferView {
func deriveThisOrThat2(_ this: borrowing BufferView, _ that: consuming BufferView) -> dependsOn(scoped this) dependsOn(that) BufferView {
if (Int.random(in: 1..<100) == 0) {
return BufferView(this.ptr)
}
return BufferView(that.ptr)
}
func use(_ x: borrowing BufferView) {}
struct Wrapper : ~Escapable {
let view: BufferView
init(_ view: consuming BufferView) {
self.view = view
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers7WrapperV8getView1AA10BufferViewVyYLsF : $@convention(method) (@guaranteed Wrapper) -> _scope(0) @owned BufferView {
borrowing func getView1() -> dependsOn(scoped self) BufferView {
return view
}
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers7WrapperV8getView2AA10BufferViewVyYLiF : $@convention(method) (@owned Wrapper) -> _inherit(0) @owned BufferView {
consuming func getView2() -> dependsOn(self) BufferView {
return view
}
}
struct Container : ~Escapable {
let ptr: UnsafeRawBufferPointer
@_unsafeNonescapableResult
init(_ ptr: UnsafeRawBufferPointer) {
self.ptr = ptr
}
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getConsumingViewyAA06BufferG0VAA9ContainerVnYliF : $@convention(thin) (@owned Container) -> _inherit(0) @owned BufferView {
func getConsumingView(_ x: consuming Container) -> dependsOn(x) BufferView {
return BufferView(x.ptr)
}
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getBorrowingViewyAA06BufferG0VAA9ContainerVYlsF : $@convention(thin) (@guaranteed Container) -> _scope(0) @owned BufferView {
func getBorrowingView(_ x: borrowing Container) -> dependsOn(scoped x) BufferView {
return BufferView(x.ptr)
}
|