Skip to content

Commit 52228af

Browse files
committed
Update tests for stricter diagnostics on borrowed argument lifetime
(cherry picked from commit 4ef3b4f)
1 parent f71b836 commit 52228af

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

test/SIL/explicit_lifetime_dependence_specifiers.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ func deriveThisOrThat1(_ this: borrowing BufferView, _ that: borrowing BufferVie
105105
if (Int.random(in: 1..<100) == 0) {
106106
return BufferView(independent: this.ptr)
107107
}
108-
return BufferView(independent: that.ptr)
108+
let newThat = BufferView(independent: that.ptr)
109+
return _overrideLifetime(newThat, copying: that)
109110
}
110111

111112
// CHECK-LABEL: sil hidden @$s39explicit_lifetime_dependence_specifiers17deriveThisOrThat2yAA10BufferViewVAD_ADntF : $@convention(thin) (@guaranteed BufferView, @owned BufferView) -> @lifetime(copy 1, borrow 0) @owned BufferView {

test/SIL/implicit_lifetime_dependence.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ func testBasic() {
8585
// CHECK-LABEL: sil hidden @$s28implicit_lifetime_dependence6deriveyAA10BufferViewVADF : $@convention(thin) (@guaranteed BufferView) -> @lifetime(copy 0) @owned BufferView {
8686
@lifetime(copy x)
8787
func derive(_ x: borrowing BufferView) -> BufferView {
88-
return BufferView(x.ptr, x.c)
88+
let newBV = BufferView(x.ptr, x.c)
89+
return _overrideLifetime(newBV, copying: x)
8990
}
9091

9192
@lifetime(copy x)
9293
func derive(_ unused: Int, _ x: borrowing BufferView) -> BufferView {
93-
return BufferView(independent: x.ptr, x.c)
94+
let newBV = BufferView(independent: x.ptr, x.c)
95+
return _overrideLifetime(newBV, copying: x)
9496
}
9597

9698
// CHECK-LABEL: sil hidden @$s28implicit_lifetime_dependence16consumeAndCreateyAA10BufferViewVADnF : $@convention(thin) (@owned BufferView) -> @lifetime(copy 0) @owned BufferView {
@@ -212,7 +214,9 @@ struct GenericBufferView<Element> : ~Escapable {
212214
// CHECK-LABEL: sil hidden @$s28implicit_lifetime_dependence23tupleLifetimeDependenceyAA10BufferViewV_ADtADF : $@convention(thin) (@guaranteed BufferView) -> @lifetime(copy 0) (@owned BufferView, @owned BufferView) {
213215
@lifetime(copy x)
214216
func tupleLifetimeDependence(_ x: borrowing BufferView) -> (BufferView, BufferView) {
215-
return (BufferView(x.ptr, x.c), BufferView(x.ptr, x.c))
217+
let newX1 = BufferView(x.ptr, x.c)
218+
let newX2 = BufferView(x.ptr, x.c)
219+
return (_overrideLifetime(newX1, copying: x), _overrideLifetime(newX2, copying: x))
216220
}
217221

218222
public struct OuterNE: ~Escapable {

test/SILOptimizer/lifetime_dependence/lifetime_dependence_borrow.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
// REQUIRES: swift_in_compiler
99
// REQUIRES: swift_feature_LifetimeDependence
1010

11+
@_unsafeNonescapableResult
12+
@lifetime(copy source)
13+
internal func _overrideLifetime<
14+
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
15+
>(
16+
_ dependent: consuming T, copying source: borrowing U
17+
) -> T {
18+
dependent
19+
}
20+
1121
// Some container-ish thing.
1222
struct CN: ~Copyable {
1323
let p: UnsafeRawPointer
@@ -47,7 +57,8 @@ struct MBV : ~Escapable, ~Copyable {
4757
// Requires a borrow.
4858
@lifetime(copy self)
4959
borrowing func getBV() -> BV {
50-
BV(p, i)
60+
let bv = BV(p, i)
61+
return _overrideLifetime(bv, copying: self)
5162
}
5263
}
5364

test/SILOptimizer/lifetime_dependence/lifetime_dependence_mutate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ struct MutableSpan : ~Escapable, ~Copyable {
9393

9494
var iterator: Iter {
9595
@lifetime(copy self)
96-
get { Iter(base: base, count: count) }
96+
get {
97+
let newIter = Iter(base: base, count: count)
98+
return _overrideLifetime(newIter, copying: self)
99+
}
97100
}
98101
}
99102

test/SILOptimizer/lifetime_dependence/lifetime_dependence_scope_fixup.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
// REQUIRES: swift_in_compiler
66
// REQUIRES: swift_feature_LifetimeDependence
77

8+
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
9+
/// a value identical to `dependent` that inherits all lifetime dependencies from
10+
/// the `source` argument.
11+
@_unsafeNonescapableResult
12+
@_transparent
13+
@lifetime(copy source)
14+
internal func _overrideLifetime<
15+
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
16+
>(
17+
_ dependent: consuming T, copying source: borrowing U
18+
) -> T {
19+
dependent
20+
}
21+
822
struct NCContainer : ~Copyable {
923
let ptr: UnsafeRawBufferPointer
1024
let c: Int
@@ -233,7 +247,9 @@ func test9() {
233247
234248
@lifetime(copy x)
235249
func getViewTuple(_ x: borrowing View) -> (View, View) {
236-
return (View(x.ptr, x.c), View(x.ptr, x.c))
250+
let x1 = View(x.ptr, x.c)
251+
let x2 = View(x.ptr, x.c)
252+
return (_overrideLifetime(x1, copying: x), _overrideLifetime(x2, copying: x))
237253
}
238254

239255
public func test10() {

0 commit comments

Comments
 (0)