Skip to content

Commit de6e8be

Browse files
committed
Add more lifetime_dependence test for stored properties.
1 parent 7b9da75 commit de6e8be

File tree

2 files changed

+124
-5
lines changed

2 files changed

+124
-5
lines changed

test/SILOptimizer/lifetime_dependence_inherit.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,26 @@ struct BV : ~Escapable {
2626
}
2727
}
2828

29-
struct NE : ~Escapable {
29+
// Nonescapable wrapper.
30+
struct NEBV : ~Escapable {
3031
var bv: BV
3132

3233
// Test lifetime inheritance through initialization.
33-
init(_ bv: consuming BV) -> dependsOn(bv) Self {
34+
init(_ bv: consuming BV) {
3435
self.bv = bv
35-
return self
36+
}
37+
38+
var view: BV {
39+
_read {
40+
yield bv
41+
}
42+
_modify {
43+
yield &bv
44+
}
45+
}
46+
47+
func borrowedView() -> dependsOn(scoped self) BV {
48+
bv
3649
}
3750
}
3851

@@ -42,6 +55,26 @@ func bv_derive(bv: consuming BV) -> dependsOn(bv) BV {
4255
}
4356

4457
// Test lifetime inheritance through stored properties.
45-
func ne_extract_member(ne: consuming NE) -> dependsOn(ne) BV {
46-
return ne.bv
58+
func ne_extract_member(nebv: consuming NEBV) -> dependsOn(nebv) BV {
59+
return nebv.bv
60+
}
61+
62+
func ne_yield_member(nebv: consuming NEBV) -> dependsOn(nebv) BV {
63+
return nebv.view
64+
}
65+
66+
func bv_consume(_ x: consuming BV) {}
67+
68+
// It is ok to consume the aggregate before the property.
69+
// The property's lifetime must exceed the aggregate's.
70+
func nebv_consume_member(nebv: consuming NEBV) {
71+
let bv = nebv.bv
72+
_ = consume nebv
73+
bv_consume(bv)
74+
}
75+
76+
func nebv_consume_after_yield(nebv: consuming NEBV) {
77+
let view = nebv.view
78+
_ = consume nebv
79+
bv_consume(view)
4780
}

test/SILOptimizer/lifetime_dependence_todo.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,65 @@
1111
// Future tests for LifetimeDependenceDiagnostics.
1212
// REQUIRES: disabled
1313

14+
struct BV : ~Escapable {
15+
let p: UnsafeRawPointer
16+
let i: Int
17+
18+
@_unsafeNonescapableResult
19+
init(_ p: UnsafeRawPointer, _ i: Int) {
20+
self.p = p
21+
self.i = i
22+
}
23+
}
24+
25+
// Nonescapable wrapper.
26+
struct NEBV : ~Escapable {
27+
var bv: BV
28+
29+
// Test lifetime inheritance through initialization.
30+
init(_ bv: consuming BV) {
31+
self.bv = bv
32+
}
33+
34+
var view: BV {
35+
_read {
36+
yield bv
37+
}
38+
_modify {
39+
yield &bv
40+
}
41+
}
42+
43+
func borrowedView() -> dependsOn(scoped self) BV {
44+
bv
45+
}
46+
}
47+
48+
// Noncopyable, Nonescapable wrapper.
49+
struct NCNEBV : ~Copyable, ~Escapable {
50+
var bv: BV
51+
52+
// Test lifetime inheritance through initialization.
53+
init(_ bv: consuming BV) {
54+
self.bv = bv
55+
}
56+
57+
var view: BV {
58+
_read {
59+
yield bv
60+
}
61+
_modify {
62+
yield &bv
63+
}
64+
}
65+
66+
func borrowedView() -> dependsOn(scoped self) BV {
67+
bv
68+
}
69+
}
70+
71+
func ncnebv_consume(_: consuming NCNEBV) {}
72+
func bv_consume(_: consuming BV) {}
1473

1574
// =============================================================================
1675
// Diagnostics that should fail.
@@ -42,3 +101,30 @@ func bvcons_capture_escapelet(bv: consuming BV) -> ()->Int {
42101
// expected-note @-1 {{this use causes the lifetime-dependent value to escape}}
43102
return closure
44103
}
104+
105+
// =============================================================================
106+
// Can't write lit tests because move-only diagnostics are broken.
107+
108+
func nebv_consume(_: consuming NEBV) {}
109+
110+
// FIXME: consume operator diagnostics does not report a line number for "used here"
111+
func nebv_consume_borrow(nebv: NEBV) {
112+
let bv = nebv.borrowedView() // expected-note {{conflicting access is here}}
113+
nebv_consume(nebv) // expected-error {{overlapping accesses to 'nebv', but deinitialization requires exclusive access}}
114+
bv_consume(bv)
115+
}
116+
117+
// FIXME: consume operator diagnostics does not report a line number for "used here"
118+
func nebv_consume_borrow(nebv: consuming NEBV) { // expected-error {{'nebv' used after consume}}
119+
let bv = nebv.borrowedView() // expected-note {{conflicting access is here}}
120+
_ = consume nebv // expected-error {{overlapping accesses to 'nebv', but modification requires exclusive access}}
121+
// expected-note @-1{{consumed here}}
122+
bv_consume(bv)
123+
}
124+
125+
// FIXME: consume operator diagnostics does not report a line number for "used here"
126+
func ncnebv_consume_borrow(ncnebv: NCNEBV) {
127+
let bv = ncnebv.borrowedView() // expected-note {{conflicting access is here}}
128+
ncnebv_consume(ncnebv) // expected-error {{overlapping accesses to 'nebv', but deinitialization requires exclusive access}}
129+
bv_consume(bv)
130+
}

0 commit comments

Comments
 (0)