Skip to content

Commit 59a401d

Browse files
committed
Update tests
1 parent a932576 commit 59a401d

10 files changed

+83
-70
lines changed

test/Parse/explicit_lifetime_dependence_specifiers.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -disable-experimental-parser-round-trip -enable-experimental-feature NoncopyableGenerics -enable-builtin-module
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -disable-experimental-parser-round-trip -enable-experimental-feature NoncopyableGenerics -enable-builtin-module -enable-experimental-lifetime-dependence-inference
2+
// REQUIRES: asserts
23
// REQUIRES: noncopyable_generics
34

45
import Builtin
56

67
struct BufferView : ~Escapable {
78
let ptr: UnsafeRawBufferPointer
9+
@_unsafeNonescapableResult
810
init(_ ptr: UnsafeRawBufferPointer) {
911
self.ptr = ptr
1012
}
13+
@_unsafeNonescapableResult
1114
init?(_ ptr: UnsafeRawBufferPointer, _ i: Int) {
1215
if (i % 2 == 0) {
1316
return nil
@@ -18,15 +21,15 @@ struct BufferView : ~Escapable {
1821
self.ptr = ptr
1922
return self
2023
}
21-
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>) -> _consume(a) Self {
24+
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper) -> _consume(a) Self {
2225
self.ptr = ptr
2326
return self
2427
}
25-
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>, b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
28+
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper, b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
2629
self.ptr = ptr
2730
return self
2831
}
29-
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>, b: borrowing Array<Int>, c: Double) -> _consume(a) _borrow(b) Int { // expected-error{{expected Self return type for initializers with lifetime dependence specifiers}}
32+
init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Double>, b: borrowing Array<Int>, c: Double) -> _consume(a) _borrow(b) Int { // expected-error{{expected Self return type for initializers with lifetime dependence specifiers}}
3033
self.ptr = ptr
3134
return 0
3235
}
@@ -43,6 +46,7 @@ struct BufferView : ~Escapable {
4346

4447
struct MutableBufferView : ~Escapable, ~Copyable {
4548
let ptr: UnsafeMutableRawBufferPointer
49+
@_unsafeNonescapableResult
4650
init(_ ptr: UnsafeMutableRawBufferPointer) {
4751
self.ptr = ptr
4852
}
@@ -131,6 +135,10 @@ func invalidSpecifier4(_ x: borrowing BufferView) -> _borrow(0) BufferView { //
131135

132136
struct Wrapper : ~Escapable {
133137
let view: BufferView
138+
init(_ view: consuming BufferView) -> _consume(view) Self {
139+
self.view = view
140+
return self
141+
}
134142
borrowing func getView1() -> _borrow(self) BufferView {
135143
return view
136144
}

test/SIL/explicit_lifetime_dependence_specifiers.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ struct BufferView : ~Escapable {
2727
self.ptr = ptr
2828
return self
2929
}
30-
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_SaySdGYlitcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Array<Double>, @thin BufferView.Type) -> _inherit(2) @owned BufferView {
31-
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>) -> _consume(a) Self {
30+
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVYlitcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @thin BufferView.Type) -> _inherit(2) @owned BufferView {
31+
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper) -> _consume(a) Self {
3232
self.ptr = ptr
3333
return self
3434
}
35-
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_SaySdGYliSaySiGhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Array<Double>, @guaranteed Array<Int>, @thin BufferView.Type) -> _inherit(2)_scope(3) @owned BufferView {
36-
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>, _ b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
35+
// CHECK: sil hidden @$s39explicit_lifetime_dependence_specifiers10BufferViewVyACSW_AA7WrapperVYliSaySiGhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Wrapper, @guaranteed Array<Int>, @thin BufferView.Type) -> _inherit(2)_scope(3) @owned BufferView {
36+
init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Wrapper, _ b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
3737
self.ptr = ptr
3838
return self
3939
}
@@ -102,19 +102,20 @@ struct Wrapper : ~Escapable {
102102
}
103103
}
104104

105-
struct Container {
105+
struct Container : ~Escapable {
106106
let ptr: UnsafeRawBufferPointer
107+
@_unsafeNonescapableResult
107108
init(_ ptr: UnsafeRawBufferPointer) {
108109
self.ptr = ptr
109110
}
110111
}
111112

112-
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getConsumingViewyAA06BufferG0VAA9ContainerVnYliF : $@convention(thin) (Container) -> _inherit(1) @owned BufferView {
113+
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getConsumingViewyAA06BufferG0VAA9ContainerVnYliF : $@convention(thin) (@owned Container) -> _inherit(1) @owned BufferView {
113114
func getConsumingView(_ x: consuming Container) -> _consume(x) BufferView {
114115
return BufferView(x.ptr)
115116
}
116117

117-
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getBorrowingViewyAA06BufferG0VAA9ContainerVYlsF : $@convention(thin) (Container) -> _scope(1) @owned BufferView {
118+
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers16getBorrowingViewyAA06BufferG0VAA9ContainerVYlsF : $@convention(thin) (@guaranteed Container) -> _scope(1) @owned BufferView {
118119
func getBorrowingView(_ x: borrowing Container) -> _borrow(x) BufferView {
119120
return BufferView(x.ptr)
120121
}

test/SIL/implicit_lifetime_dependence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct BufferView : ~Escapable {
2424
init(_ otherBV: consuming BufferView) {
2525
self.ptr = otherBV.ptr
2626
}
27-
// CHECK: sil hidden @$s28implicit_lifetime_dependence10BufferViewVyACSW_SaySiGhtcfC : $@convention(method) (UnsafeRawBufferPointer, @guaranteed Array<Int>, @thin BufferView.Type) -> _scope(1) @owned BufferView {
27+
// CHECK: sil hidden @$s28implicit_lifetime_dependence10BufferViewVyACSWYls_SaySiGhtcfC : $@convention(method) (UnsafeRawBufferPointer, @guaranteed Array<Int>, @thin BufferView.Type) -> _scope(1) @owned BufferView {
2828
init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) {
2929
self.ptr = ptr
3030
}

test/SIL/lifetime_dependence_generics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct PView: P {
2525
borrowing func getE() -> _borrow(self) View { return View() }
2626
}
2727

28-
public func test(pview: consuming PView) -> _consume(pview) View {
28+
public func test(pview: borrowing PView) -> _borrow(pview) View {
2929
return pview.getDefault()
3030
}
3131

@@ -35,4 +35,4 @@ public func test(pview: consuming PView) -> _consume(pview) View {
3535

3636
// CHECK: sil private [transparent] [thunk] @$s28lifetime_dependence_generics5PViewVAA1PA2aDP4getE1EQzyYLsFTW : $@convention(witness_method: P) (@in_guaranteed PView) -> _scope(0) @out View {
3737

38-
// CHECK: sil @$s28lifetime_dependence_generics4test5pviewAA4ViewVAA5PViewVnYli_tF : $@convention(thin) (PView) -> _inherit(1) @owned View {
38+
// CHECK: sil @$s28lifetime_dependence_generics4test5pviewAA4ViewVAA5PViewVYls_tF : $@convention(thin) (PView) -> _scope(1) @owned View {

test/SILOptimizer/lifetime_dependence_borrow.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
// RUN: -enable-experimental-feature NonescapableTypes \
88
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics
99

10+
// REQUIRES: asserts
11+
// REQUIRES: noncopyable_generics
1012
// REQUIRES: swift_in_compiler
1113

12-
@_nonescapable
13-
struct BV {
14+
struct BV : ~Escapable {
1415
let p: UnsafeRawPointer
1516
let c: Int
1617

@@ -23,7 +24,7 @@ struct BV {
2324
}
2425
}
2526

26-
struct NC : ~Copyable {
27+
struct NCNE : ~Copyable, ~Escapable {
2728
let p: UnsafeRawPointer
2829
let c: Int
2930

@@ -34,16 +35,16 @@ struct NC : ~Copyable {
3435
}
3536

3637
// Propagate a borrow.
37-
func bv_get_borrow(container: borrowing NC) -> _borrow(container) BV {
38+
func bv_get_borrow(container: borrowing NCNE) -> _borrow(container) BV {
3839
container.getBV()
3940
}
4041

4142
// Copy a borrow.
42-
func bv_get_copy(container: borrowing NC) -> _copy(container) BV {
43+
func bv_get_copy(container: borrowing NCNE) -> _copy(container) BV {
4344
return container.getBV()
4445
}
4546

4647
// Recognize nested accesses as part of the same dependence scope.
47-
func bv_get_mutate(container: inout NC) -> _mutate(container) BV {
48+
func bv_get_mutate(container: inout NCNE) -> _mutate(container) BV {
4849
container.getBV()
4950
}

test/SILOptimizer/lifetime_dependence_borrow_fail.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics \
99
// RUN: -enable-experimental-lifetime-dependence-inference
1010

11+
// REQUIRES: asserts
12+
// REQUIRES: noncopyable_generics
1113
// REQUIRES: swift_in_compiler
1214

13-
@_nonescapable
14-
struct BV {
15+
struct BV : ~Escapable {
1516
let p: UnsafeRawPointer
1617
let i: Int
1718

@@ -26,13 +27,33 @@ struct NC : ~Copyable {
2627
let p: UnsafeRawPointer
2728
let i: Int
2829

30+
@_unsafeNonescapableResult
31+
init(_ p: UnsafeRawPointer, _ i: Int) {
32+
self.p = p
33+
self.i = i
34+
}
2935
borrowing func getBV() -> _borrow(self) BV {
3036
BV(p, i)
3137
}
3238
}
3339

34-
func bv_get_consume(container: consuming NC) -> BV {
40+
struct NE : ~Escapable {
41+
let p: UnsafeRawPointer
42+
let i: Int
43+
44+
@_unsafeNonescapableResult
45+
init(_ p: UnsafeRawPointer, _ i: Int) {
46+
self.p = p
47+
self.i = i
48+
}
49+
borrowing func getBV() -> _borrow(self) BV {
50+
BV(p, i)
51+
}
52+
}
53+
54+
func bv_get_consume(container: consuming NE) -> BV {
3555
return container.getBV() // expected-error {{lifetime-dependent value escapes its scope}}
3656
// expected-note @-1{{it depends on this scoped access to variable 'container'}}
3757
// expected-note @-2{{this use causes the lifetime-dependent value to escape}}
3858
}
59+

test/SILOptimizer/lifetime_dependence_generic_fail.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/SILOptimizer/lifetime_dependence_scope_fixup.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ struct MutableView : ~Copyable, ~Escapable {
5050
init(_ otherBV: borrowing View) {
5151
self.ptr = otherBV.ptr
5252
}
53-
init(_ k: consuming NCContainer) {
53+
init(_ k: borrowing NCContainer) {
54+
self.ptr = k.ptr
55+
}
56+
init(_ k: consuming NEContainer) {
5457
self.ptr = k.ptr
5558
}
5659
}
@@ -114,6 +117,9 @@ func test3(_ a: Array<Int>) {
114117
}
115118
}
116119

120+
/*
121+
// Currently fails because the lifetime dependence util isn't analyzing a
122+
// def-use chain involving a stack temporary
117123
func test4(_ a: Array<Int>) {
118124
a.withUnsafeBytes {
119125
var x = NCContainer($0)
@@ -123,6 +129,7 @@ func test4(_ a: Array<Int>) {
123129
consume(view)
124130
}
125131
}
132+
*/
126133

127134
func test5(_ a: Array<Int>) {
128135
a.withUnsafeBytes {
@@ -152,3 +159,13 @@ func test7(_ a: UnsafeRawBufferPointer) {
152159
}
153160
mutate(&x)
154161
}
162+
163+
func test8(_ a: Array<Int>) {
164+
a.withUnsafeBytes {
165+
var x = NEContainer($0)
166+
mutate(&x)
167+
let view = MutableView(x)
168+
use(view)
169+
consume(view)
170+
}
171+
}

test/Serialization/Inputs/def_explicit_lifetime_dependence.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
public struct AnotherView : ~Escapable {
2+
let ptr: UnsafeRawBufferPointer
3+
@_unsafeNonescapableResult
4+
public init(_ ptr: UnsafeRawBufferPointer) {
5+
self.ptr = ptr
6+
}
7+
}
8+
19
public struct BufferView : ~Escapable {
210
let ptr: UnsafeRawBufferPointer
311
@_unsafeNonescapableResult
@@ -8,11 +16,11 @@ public struct BufferView : ~Escapable {
816
self.ptr = ptr
917
return self
1018
}
11-
public init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Double>) -> _consume(a) Self {
19+
public init(_ ptr: UnsafeRawBufferPointer, _ a: consuming AnotherView) -> _consume(a) Self {
1220
self.ptr = ptr
1321
return self
1422
}
15-
public init(_ ptr: UnsafeRawBufferPointer, _ a: consuming Array<Int>, _ b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
23+
public init(_ ptr: UnsafeRawBufferPointer, _ a: consuming AnotherView, _ b: borrowing Array<Int>) -> _consume(a) _borrow(b) Self {
1624
self.ptr = ptr
1725
return self
1826
}

test/Serialization/explicit_lifetime_dependence.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,14 @@ func testInitializers() {
3434
let b = Array(0..<capacity)
3535
a.withUnsafeBytes {
3636
let view1 = BufferView($0, a)
37-
let view2 = BufferView($0, a, b)
37+
let view2 = BufferView($0, b)
3838
let mysteryView = deriveThisOrThat(view1, view2)
3939
use(mysteryView)
4040
}
4141
}
42-
4342
// CHECK: sil @$s32def_explicit_lifetime_dependence6deriveyAA10BufferViewVADYlsF : $@convention(thin) (@guaranteed BufferView) -> _scope(1) @owned BufferView
44-
4543
// CHECK: sil @$s32def_explicit_lifetime_dependence16consumeAndCreateyAA10BufferViewVADnYliF : $@convention(thin) (@owned BufferView) -> _inherit(1) @owned BufferView
46-
4744
// CHECK: sil @$s32def_explicit_lifetime_dependence15borrowAndCreateyAA10BufferViewVADYlsF : $@convention(thin) (@guaranteed BufferView) -> _scope(1) @owned BufferView
48-
4945
// CHECK: sil @$s32def_explicit_lifetime_dependence16deriveThisOrThatyAA10BufferViewVADYls_ADYlstF : $@convention(thin) (@guaranteed BufferView, @guaranteed BufferView) -> _scope(1, 2) @owned BufferView
5046

5147
// CHECK: sil @$s32def_explicit_lifetime_dependence10BufferViewVyACSW_SaySiGhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @guaranteed Array<Int>, @thin BufferView.Type) -> _scope(2) @owned BufferView
52-
53-
// CHECK: sil @$s32def_explicit_lifetime_dependence10BufferViewVyACSW_SaySiGYliADhYlstcfC : $@convention(method) (UnsafeRawBufferPointer, @owned Array<Int>, @guaranteed Array<Int>, @thin BufferView.Type) -> _inherit(2)_scope(3) @owned BufferView

0 commit comments

Comments
 (0)