File: moveonly_nonescaping_closures.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (178 lines) | stat: -rw-r--r-- 5,051 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// RUN: %target-swift-frontend -emit-sil -verify %s
// RUN: %target-swift-frontend -DNONTRIVIAL -emit-sil -verify %s
// RUN: %target-swift-frontend -DNONTRIVIAL -DREABSTRACT -emit-sil -verify %s
// TODO: test with (-DNONTRIVIAL | -DADDRESS_ONLY) * (REABSTRACT)

protocol P {}
extension Int: P {}

class C {}

@_moveOnly
struct M {
#if ADDRESS_ONLY
    private var x: P = 0
#elseif NONTRIVIAL
    private var x: C = C()
#else
    private var x: Int = 0
#endif

    borrowing func borrow() {}
}

func borrow(_: borrowing M) {}
func consume(_: __owned M) {}
func mutate(_: inout M) {}

func borrow(_: borrowing M, consume _: __owned M) {}

#if REABSTRACT
func clodger<T>(_: () -> T) {}
func clodger<T>(_: () -> T, borrow _: borrowing M) {}
func clodger<T>(_: () -> T, consume _: __owned M) {}
func clodger<T>(_: () -> T, mutate _: inout M) {}
func clodger<T>(_: () -> T, and _: () -> T) {}
func clodger<T>(_: () -> T, and _: () -> T, consume _: __owned M) {}
#else
func clodger(_: () -> ()) {}
func clodger(_: () -> (), borrow _: borrowing M) {}
func clodger(_: () -> (), consume _: __owned M) {}
func clodger(_: () -> (), mutate _: inout M) {}
func clodger(_: () -> (), and _: () -> ()) {}
func clodger(_: () -> (), and _: () -> (), consume _: __owned M) {}
#endif

func reabstractClodger<T>(_: (T) -> T) {}

func a(x: borrowing M) {
    clodger({ borrow(x) })
    borrow(x)
}

func b(x: __owned M) { // expected-error {{'x' used after consume}}
    clodger({ borrow(x) }, consume: x)
    // expected-note @-1:25 {{used here}}
    // expected-note @-2:37 {{consumed here}}
}

// We have a use after free here since we treat the passing of borrow to clodger
// to be a read use of x. So we consume x as part of preparing arguments and
// then have a read via the application of clodger.
func b2(x: consuming M) {
    // expected-error @-1 {{'x' used after consume}}
    clodger({ borrow(x) }, // expected-note {{conflicting access is here}}
            // expected-note @-1 {{used here}}
            consume: x) // expected-error {{overlapping accesses to 'x', but deinitialization requires exclusive access}}
            // expected-note @-1 {{consumed here}}
}

func c(x: __owned M) {
    clodger({ borrow(x) })
    borrow(x)
    consume(x)
}

func c2(x: consuming M) {
    clodger({ borrow(x) })
    borrow(x)
    consume(x)
}

func d(x: __owned M) { // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    clodger({ consume(x) })
    // expected-note @-1 {{consumed here}}
}

func d2(x: consuming M) { // expected-error {{missing reinitialization of closure capture 'x' after consume}}
    clodger({ consume(x) })
    // expected-note @-1 {{consumed here}}
}

func e(x: inout M) {
    clodger({ mutate(&x) })
    mutate(&x)
    borrow(x)
    consume(x)
    x = M()
}

func f(x: borrowing M) {
    clodger({ borrow(x) }, borrow: x)
}

func g(x: inout M) {
    clodger({ mutate(&x) }, // expected-error {{overlapping accesses to 'x', but modification requires exclusive access}}}
            borrow: x)      // expected-note {{conflicting access is here}}
}

func h(x: inout M) { // expected-error {{'x' used after consume}}
    clodger({ mutate(&x) }, // expected-note {{conflicting access is here}}
            // expected-note @-1 {{used here}}
            consume: x) // expected-error {{overlapping accesses to 'x', but deinitialization requires exclusive access}}}
    // expected-note @-1 {{consumed here}}
    x = M()
}

func i(x: inout M) {
    clodger({ mutate(&x) }, // expected-note {{conflicting access is here}}
            mutate: &x) // expected-error {{overlapping accesses to 'x', but modification requires exclusive access}}}
}

// Multiple closures are allowed to capture the same inout binding concurrently.
// Restrictions in the callee prevent them from being executed simultaneously.
func j(x: inout M) {
    clodger({ mutate(&x) }, and: { mutate(&x) })
}

func k(x: borrowing M) {
    clodger({ borrow(x) }, and: { borrow(x) })
    borrow(x)
}


func l(x: inout M) { // expected-error {{missing reinitialization of closure capture 'x' after consume}}
    clodger({ consume(x) }) // expected-note {{consumed here}}
}

func m(x: inout M) { // expected-error {{'x' used after consume}}
    consume(x) // expected-note {{consumed here}}
    clodger({ borrow(x) }) // expected-note {{used here}}
}

func n(x: inout M) { // expected-error {{'x' used after consume}}
    consume(x) // expected-note {{consumed here}}
    clodger({ // expected-note {{used here}}
        mutate(&x)
    })
}

func o(x: inout M) {
    clodger({ consume(x); x = M() })
}

func p(x: inout M) {
    consume(x)
    x = M()

    clodger({ consume(x); x = M() })
}

func takesClosureWithArg(_: (Int) -> ()) {}

func invokesWithClosureWithArg() {
    let m = M()

    takesClosureWithArg { _ in
      m.borrow()
    }

    m.borrow()
}

// need test cases for:
// - capturing local let
// - capturing local var
// - capture list binding can't be consumed
// - andy's bookmarked test cases
// - nested closure captures