Skip to content

Commit 2e622cd

Browse files
committed
Lifetime dependence unit tests.
1 parent 8c09291 commit 2e622cd

14 files changed

+385
-92
lines changed

test/SILOptimizer/argument_conventions.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// RUN: -enable-experimental-feature NonescapableTypes \
44
// RUN: 2>&1 | %FileCheck %s
55

6+
// REQUIRES: swift_in_compiler
7+
68
import Builtin
79

810
class C {}

test/SILOptimizer/argument_conventions.swift

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics
9+
10+
// REQUIRES: swift_in_compiler
11+
12+
@_nonescapable
13+
struct BV {
14+
let p: UnsafeRawPointer
15+
let c: Int
16+
17+
public var isEmpty: Bool { c == 0 }
18+
19+
@_unsafeNonescapableResult
20+
init(_ p: UnsafeRawPointer, _ c: Int) {
21+
self.p = p
22+
self.c = c
23+
}
24+
}
25+
26+
struct NC : ~Copyable {
27+
let p: UnsafeRawPointer
28+
let c: Int
29+
30+
// Requires a borrow.
31+
borrowing func getBV() -> _borrow(self) BV {
32+
BV(p, c)
33+
}
34+
}
35+
36+
// Propagate a borrow.
37+
func bv_get_borrow(container: borrowing NC) -> _borrow(container) BV {
38+
container.getBV()
39+
}
40+
41+
// Copy a borrow.
42+
func bv_get_copy(container: borrowing NC) -> _copy(container) BV {
43+
return container.getBV()
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics
9+
10+
// REQUIRES: swift_in_compiler
11+
12+
@_nonescapable
13+
struct BV {
14+
let p: UnsafeRawPointer
15+
let i: Int
16+
17+
@_unsafeNonescapableResult
18+
init(_ p: UnsafeRawPointer, _ i: Int) {
19+
self.p = p
20+
self.i = i
21+
}
22+
}
23+
24+
struct NC : ~Copyable {
25+
let p: UnsafeRawPointer
26+
let i: Int
27+
28+
borrowing func getBV() -> _borrow(self) BV {
29+
BV(p, i)
30+
}
31+
}
32+
33+
func bv_get_consume(container: consuming NC) -> BV {
34+
return container.getBV() // expected-error {{lifetime-dependent value escapes its scope}}
35+
// expected-note @-1{{it depends on this scoped access to variable 'container'}}
36+
// expected-note @-2{{this use causes the lifetime-dependent value to escape}}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -sil-verify-all \
3+
// RUN: -module-name test \
4+
// RUN: -disable-experimental-parser-round-trip \
5+
// RUN: -enable-experimental-feature NonescapableTypes \
6+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics \
7+
// RUN: 2>&1 | %FileCheck %s
8+
9+
// REQUIRES: swift_in_compiler
10+
11+
@_nonescapable
12+
struct BV {
13+
let p: UnsafeRawPointer
14+
let c: Int
15+
}
16+
17+
func bv_copy(_ bv: borrowing BV) -> _copy(bv) BV {
18+
copy bv
19+
}
20+
21+
// Diagnostics resolves mark_dependence [nonescaping].
22+
//
23+
// CHECK-LABEL: sil hidden @$s4test14bv_borrow_copy0B0AA2BVVAE_tF : $@convention(thin) (@guaranteed BV) -> _inherit(1) @owned BV {
24+
// CHECK: bb0(%0 : @noImplicitCopy $BV):
25+
// CHECK: [[R:%.*]] = apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _inherit(1) @owned BV
26+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[R]] : $BV on %0 : $BV
27+
// CHECK: return [[MD]] : $BV
28+
// CHECK-LABEL: } // end sil function '$s4test14bv_borrow_copy0B0AA2BVVAE_tF'
29+
func bv_borrow_copy(bv: borrowing BV) -> _borrow(bv) BV {
30+
bv_copy(bv)
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -enable-experimental-feature NoncopyableGenerics \
9+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics \
10+
// RUN: -parse-stdlib -module-name Swift
11+
12+
// REQUIRES: swift_in_compiler
13+
14+
@_marker public protocol Escapable {}
15+
16+
@_silgen_name("imagineInt64")
17+
func imagineInt64() -> Builtin.Int64
18+
19+
protocol P {
20+
associatedtype E: ~Escapable
21+
borrowing func getE() -> _borrow(self) E
22+
}
23+
24+
extension P {
25+
borrowing func getDefault() -> _borrow(self) E {
26+
return getE()
27+
}
28+
}
29+
30+
public struct Bits {
31+
var i = imagineInt64()
32+
}
33+
34+
struct PBits: P {
35+
func getE() -> Bits { return Bits() }
36+
}
37+
38+
public func pbits_ret_concerete() -> Bits {
39+
let pbits = PBits()
40+
return pbits.getDefault()
41+
}
42+
43+
public func consume_indirect<NE: ~Escapable>(ne: consuming NE) -> _consume(ne) NE {
44+
return ne
45+
}
46+
47+
public func copy_indirect<NE: ~Escapable>(ne: borrowing NE) -> _copy(ne) NE {
48+
return copy ne
49+
}
50+
51+
public func copy_inout<NE: ~Escapable>(ne: inout NE) -> _copy(ne) NE {
52+
return copy ne
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -enable-experimental-feature NoncopyableGenerics \
9+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics \
10+
// RUN: -parse-stdlib -module-name Swift
11+
12+
// REQUIRES: swift_in_compiler
13+
14+
@_marker public protocol Escapable {}
15+
16+
protocol P {
17+
associatedtype E: ~Escapable
18+
borrowing func getE() -> _borrow(self) E
19+
}
20+
21+
extension P {
22+
borrowing func getDefault() -> _borrow(self) E {
23+
return getE()
24+
}
25+
}
26+
27+
public struct View: ~Escapable {}
28+
29+
public struct PView: P {
30+
borrowing func getE() -> _borrow(self) View { return View() }
31+
}
32+
33+
public func pview_ret_concrete(pview: consuming PView) -> _consume(pview) View {
34+
return pview.getDefault() // expected-error {{lifetime-dependent value escapes its scope}}
35+
// expected-note @-1 {{it depends on this scoped access to variable 'pview'}}
36+
// expected-note @-2 {{this use of the lifetime-dependent value is out of scope}}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics
9+
10+
// REQUIRES: swift_in_compiler
11+
12+
@_nonescapable
13+
struct BV {
14+
let p: UnsafeRawPointer
15+
let i: Int
16+
17+
@_unsafeNonescapableResult
18+
init(_ p: UnsafeRawPointer, _ i: Int) {
19+
self.p = p
20+
self.i = i
21+
}
22+
23+
consuming func derive() -> _consume(self) BV {
24+
// Technically, this "new" view does not depend on the 'view' argument.
25+
// This unsafely creates a new view with no dependence.
26+
return BV(self.p, self.i)
27+
}
28+
}
29+
30+
@_nonescapable
31+
struct NE {
32+
var bv: BV
33+
34+
// Test lifetime inheritance through initialization.
35+
init(_ bv: consuming BV) -> _consume(bv) Self {
36+
self.bv = bv
37+
return self
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-experimental-parser-round-trip \
7+
// RUN: -enable-experimental-feature NonescapableTypes \
8+
// RUN: -Xllvm -enable-lifetime-dependence-diagnostics
9+
10+
// REQUIRES: swift_in_compiler
11+
12+
@_nonescapable
13+
struct BV {
14+
let p: UnsafeRawPointer
15+
let i: Int
16+
17+
@_unsafeNonescapableResult
18+
init(_ p: UnsafeRawPointer, _ i: Int) {
19+
self.p = p
20+
self.i = i
21+
}
22+
23+
consuming func derive() -> _consume(self) BV {
24+
// Technically, this "new" view does not depend on the 'view' argument.
25+
// This unsafely creates a new view with no dependence.
26+
return BV(self.p, self.i)
27+
}
28+
}
29+
30+
@_nonescapable
31+
struct NE {
32+
var bv: BV
33+
34+
init(_ bv: consuming BV) -> _consume(bv) Self {
35+
self.bv = bv
36+
return self
37+
}
38+
}
39+
40+
func bv_derive_local(bv: consuming BV) -> _consume(bv) BV {
41+
let bv2 = BV(bv.p, bv.i)
42+
return bv2.derive() // expected-error {{lifetime-dependent value escapes its scope}}
43+
// expected-note @-2 {{it depends on the lifetime of variable 'bv2'}}
44+
// expected-note @-2 {{this use causes the lifetime-dependent value to escape}}
45+
}

0 commit comments

Comments
 (0)