-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
lifetime_dependence_diagnostics.swift
108 lines (93 loc) · 3.88 KB
/
lifetime_dependence_diagnostics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -sil-verify-all \
// RUN: -module-name test \
// RUN: -enable-experimental-feature NoncopyableGenerics \
// RUN: -enable-experimental-feature NonescapableTypes \
// RUN: 2>&1 | %FileCheck %s
// REQUIRES: asserts
// REQUIRES: swift_in_compiler
struct BV : ~Escapable {
let p: UnsafeRawPointer
let c: Int
@_unsafeNonescapableResult
init(_ p: UnsafeRawPointer, _ c: Int) {
self.p = p
self.c = c
}
}
func bv_copy(_ bv: borrowing BV) -> dependsOn(bv) BV {
copy bv
}
struct NCInt: ~Copyable {
var i: Int
}
public struct NEInt: ~Escapable {
var i: Int
// Test yielding an address.
// CHECK-LABEL: sil hidden @$s4test5NEIntV5ipropSivM : $@yield_once @convention(method) (@inout NEInt) -> @yields @inout Int {
// CHECK: bb0(%0 : $*NEInt):
// CHECK: [[A:%.*]] = begin_access [modify] [static] %0 : $*NEInt
// CHECK: [[E:%.*]] = struct_element_addr [[A]] : $*NEInt, #NEInt.i
// CHECK: yield [[E]] : $*Int, resume bb1, unwind bb2
// CHECK: end_access [[A]] : $*NEInt
// CHECK: end_access [[A]] : $*NEInt
// CHECK-LABEL: } // end sil function '$s4test5NEIntV5ipropSivM'
var iprop: Int {
_read { yield i }
_modify { yield &i }
}
init(owner: borrowing NCInt) -> dependsOn(owner) Self {
self.i = owner.i
}
}
public enum NEOptional<Wrapped: ~Escapable>: ~Escapable {
case none
case some(Wrapped)
}
extension NEOptional where Wrapped: ~Escapable {
// Test that enum initialization passes diagnostics.
public init(_ some: consuming Wrapped) { self = .some(some) }
}
func takeClosure(_: () -> ()) {}
// No mark_dependence is needed for a inherited scope.
//
// CHECK-LABEL: sil hidden @$s4test14bv_borrow_copyyAA2BVVADYlsF : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV {
// CHECK: bb0(%0 : @noImplicitCopy $BV):
// CHECK: apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _inherit(1) @owned BV
// CHECK-NEXT: return %3 : $BV
// CHECK-LABEL: } // end sil function '$s4test14bv_borrow_copyyAA2BVVADYlsF'
func bv_borrow_copy(_ bv: borrowing BV) -> dependsOn(scoped bv) BV {
bv_copy(bv)
}
// The mark_dependence for the borrow scope should be marked
// [nonescaping] after diagnostics.
//
// CHECK-LABEL: sil hidden @$s4test010bv_borrow_C00B0AA2BVVAEYls_tF : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV {
// CHECK: bb0(%0 : @noImplicitCopy $BV):
// CHECK: [[R:%.*]] = apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> _scope(1) @owned BV
// CHECK: %{{.*}} = mark_dependence [nonescaping] [[R]] : $BV on %0 : $BV
// CHECK-NEXT: return %{{.*}} : $BV
// CHECK-LABEL: } // end sil function '$s4test010bv_borrow_C00B0AA2BVVAEYls_tF'
func bv_borrow_borrow(bv: borrowing BV) -> dependsOn(scoped bv) BV {
bv_borrow_copy(bv)
}
// This already has a mark_dependence [nonescaping] before diagnostics. If it triggers diagnostics again, it will fail
// because lifetime dependence does not expect a dependence directly on an 'inout' address without any 'begin_access'
// marker.
func ncint_capture(ncInt: inout NCInt) {
takeClosure { _ = ncInt.i }
}
func neint_throws(ncInt: borrowing NCInt) throws -> NEInt {
return NEInt(owner: ncInt)
}
// CHECK-LABEL: sil hidden @$s4test9neint_try5ncIntAA5NEIntVAA5NCIntVYls_tKF : $@convention(thin) (@guaranteed NCInt) -> _scope(1) (@owned NEInt, @error any Error) {
// CHECK: try_apply %{{.*}}(%0) : $@convention(thin) (@guaranteed NCInt) -> _scope(1) (@owned NEInt, @error any Error), normal bb1, error bb2
// CHECK: bb1([[R:%.*]] : $NEInt):
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] %5 : $NEInt on %0 : $NCInt
// CHECK: return [[MD]] : $NEInt
// CHECK: bb2([[E:%.*]] : $any Error):
// CHECK: throw [[E]] : $any Error
// CHECK-LABEL: } // end sil function '$s4test9neint_try5ncIntAA5NEIntVAA5NCIntVYls_tKF'
func neint_try(ncInt: borrowing NCInt) throws -> NEInt {
try neint_throws(ncInt: ncInt)
}