Skip to content

Commit 2c35728

Browse files
committed
Add unit tests for lifetime dependence property access semantics.
1 parent a9f22a4 commit 2c35728

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

test/SILOptimizer/lifetime_dependence/semantics.swift

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ extension Array {
7474
}
7575
}
7676

77-
struct Inner {
77+
struct InnerTrivial {
78+
var p: UnsafePointer<Int>
79+
80+
@lifetime(borrow self)
81+
borrowing func span() -> Span<Int> {
82+
Span(base: p, count: 1)
83+
}
84+
}
85+
86+
struct InnerObject {
87+
let object: AnyObject
7888
var p: UnsafePointer<Int>
7989

8090
@lifetime(borrow self)
@@ -84,14 +94,31 @@ struct Inner {
8494
}
8595

8696
struct Outer {
87-
let inner: Inner
88-
let fakePointer: UnsafePointer<Inner>
97+
var _innerTrivial: InnerTrivial
98+
var _innerObject: InnerObject
99+
let trivialPointer: UnsafePointer<InnerTrivial>
100+
let objectPointer: UnsafePointer<InnerObject>
89101

90-
var innerAddress: Inner {
102+
var innerTrivialAddress: InnerTrivial {
91103
unsafeAddress {
92-
fakePointer
104+
trivialPointer
93105
}
94106
}
107+
108+
var innerObjectAddress: InnerObject {
109+
unsafeAddress {
110+
objectPointer
111+
}
112+
}
113+
114+
var innerTrivialTemp: InnerTrivial {
115+
get { _innerTrivial }
116+
}
117+
118+
var innerObjectTemp: InnerObject {
119+
get { _innerObject }
120+
}
121+
95122
/* TODO: rdar://137608270 Add Builtin.addressof() support for @addressable arguments
96123
@addressableSelf
97124
var innerAddress: Inner {
@@ -206,11 +233,63 @@ func testTrivialScope<T>(a: Array<T>) -> Span<T> {
206233
// =============================================================================
207234

208235
@lifetime(borrow outer)
209-
func testBorrowComponent(outer: Outer) -> Span<Int> {
210-
outer.inner.span()
236+
func testBorrowStoredTrivial(outer: Outer) -> Span<Int> {
237+
outer._innerTrivial.span()
238+
}
239+
240+
@lifetime(borrow outer)
241+
func testBorrowStoredObject(outer: Outer) -> Span<Int> {
242+
outer._innerObject.span()
211243
}
212244

213245
@lifetime(borrow outer)
214-
func testBorrowAddressComponent(outer: Outer) -> Span<Int> {
215-
outer.innerAddress.span()
246+
func testBorrowTrivialAddressProjection(outer: Outer) -> Span<Int> {
247+
outer.innerTrivialAddress.span()
216248
}
249+
250+
@lifetime(borrow outer)
251+
func testBorrowObjectAddressProjection(outer: Outer) -> Span<Int> {
252+
outer.innerObjectAddress.span()
253+
}
254+
255+
func testExprExtendTrivialTemp(outer: Outer) {
256+
parse(outer.innerTrivialTemp.span())
257+
// expected-error @-1{{lifetime-dependent value escapes its scope}}
258+
// expected-note @-2{{it depends on the lifetime of this parent value}}
259+
// expected-note @-3{{this use of the lifetime-dependent value is out of scope}}
260+
}
261+
262+
func testExprExtendObjectTemp(outer: Outer) {
263+
parse(outer.innerObjectTemp.span())
264+
// expected-error @-1{{lifetime-dependent value escapes its scope}}
265+
// expected-note @-2{{it depends on the lifetime of this parent value}}
266+
// expected-note @-3{{this use of the lifetime-dependent value is out of scope}}
267+
}
268+
269+
func testLocalExtendTrivialTemp(outer: Outer) {
270+
let span = outer.innerTrivialTemp.span()
271+
// expected-error @-1{{lifetime-dependent variable 'span' escapes its scope}}
272+
// expected-note @-2{{it depends on the lifetime of this parent value}}
273+
parse(span) // expected-note {{this use of the lifetime-dependent value is out of scope}}
274+
}
275+
276+
func testLocalExtendObjectTemp(outer: Outer) {
277+
let span = outer.innerObjectTemp.span()
278+
// expected-error @-1{{lifetime-dependent variable 'span' escapes its scope}}
279+
// expected-note @-2{{it depends on the lifetime of this parent value}}
280+
parse(span) // expected-note {{this use of the lifetime-dependent value is out of scope}}
281+
}
282+
283+
@lifetime(borrow outer)
284+
func testReturnTrivialTemp(outer: Outer) -> Span<Int> {
285+
outer.innerTrivialTemp.span()
286+
// expected-error @-1{{lifetime-dependent value escapes its scope}}
287+
// expected-note @-2{{it depends on the lifetime of this parent value}}
288+
} // expected-note {{this use causes the lifetime-dependent value to escape}}
289+
290+
@lifetime(borrow outer)
291+
func testReturnObjectTemp(outer: Outer) -> Span<Int> {
292+
outer.innerObjectTemp.span()
293+
// expected-error @-1{{lifetime-dependent value escapes its scope}}
294+
// expected-note @-2{{it depends on the lifetime of this parent value}}
295+
} // expected-note {{this use causes the lifetime-dependent value to escape}}

0 commit comments

Comments
 (0)