File: moveonly_address_maximize.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 (308 lines) | stat: -rw-r--r-- 11,556 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all) | %FileCheck %s
// RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all) | %FileCheck %s

// REQUIRES: executable_test

struct S : ~Copyable {
  let s: String
  init(_ s: String) { self.s = s }
  deinit {
    print("destroying \(s)")
  }
}
struct S2 : ~Copyable {
    var s1: S
    var s2: S
    init(_ s: String) {
      self.s1 = S("\(s).s1")
      self.s2 = S("\(s).s2")
    }
}
struct S3 : ~Copyable {
    var s1: S
    var s2: S
    var s3: S
    init(_ s: String) {
      self.s1 = S("\(s).s1")
      self.s2 = S("\(s).s2")
      self.s3 = S("\(s).s3")
    }
}

func consumeVal(_ s: consuming S) {}
func consumeVal(_ s: consuming S2) {}
func borrowVal(_ s: borrowing S) {}
func borrowVal(_ s: borrowing S2) {}

func marker(_ s: String) {
  print("\(#function): \(s)")
}

// Simple test that makes sure that we still after we consume have the lifetime
// of s be completely consumed by consumeVal.
// CHECK: destroying simpleTestVar().first.s1
// CHECK: destroying simpleTestVar().first.s2
// CHECK: destroying simpleTestVar().second.s1
// CHECK: destroying simpleTestVar().second.s2
// CHECK: marker(_:): simpleTestVar().1
@_silgen_name("simpleTestVar")
func simpleTestVar() {
    var s = S2("\(#function).first")
    s = S2("\(#function).second")
    consumeVal(s) // Lifetime of s should end here before end of scope.
    marker("\(#function).1")
}

// Simple test that proves that we can maximize lifetimes in a field sensitive
// manner. Since we only consume s.s1, s.s2's lifetime should still be maximized
// and be at end of scope.
// CHECK: destroying simpleTestVar2().first.s1
// CHECK: destroying simpleTestVar2().first.s2
// CHECK: destroying simpleTestVar2().second.s1
// CHECK: marker(_:): simpleTestVar2().1
// CHECK: destroying simpleTestVar2().second.s2
func simpleTestVar2() {
    var s = S2("\(#function).first")
    s = S2("\(#function).second")
    consumeVal(s.s1) // Lifetime of s1 should end here.
    marker("\(#function).1")
    // Lifetime of s2 should end at end of scope after marker.
}

// In this case, we consume all of s by consuming s.s1 and s.s2 separately, so
// all lifetimes should be done before marker.
// CHECK: destroying simpleTestVar3().first.s1
// CHECK: destroying simpleTestVar3().first.s2
// CHECK: destroying simpleTestVar3().second.s1
// CHECK: destroying simpleTestVar3().second.s2
// CHECK: marker(_:): simpleTestVar3().1
func simpleTestVar3() {
    var s = S2("\(#function).first")
    s = S2("\(#function).second")
    consumeVal(s.s1)
    consumeVal(s.s2)
    marker("\(#function).1") // Lifetimes should end before marker.
}

// In this case, we completely consume s and then reinitialize s implying we
// need to deal with two disjoint lifetimes. The second lifetime of s should end
// after marker.
// CHECK: destroying simpleTestVar3a().first.s1
// CHECK: destroying simpleTestVar3a().first.s2
// CHECK: destroying simpleTestVar3a().second.s1
// CHECK: destroying simpleTestVar3a().second.s2
// CHECK: marker(_:): simpleTestVar3a().1
// CHECK: marker(_:): simpleTestVar3a().2
// CHECK: destroying simpleTestVar3a().third.s1
// CHECK: destroying simpleTestVar3a().third.s2
func simpleTestVar3a() {
    var s = S2("\(#function).first")
    s = S2("\(#function).second")
    consumeVal(s.s1)
    consumeVal(s.s2)

    marker("\(#function).1")

    s = S2("\(#function).third")
    marker("\(#function).2")
}

// In this case, we have another borrowVal of s.s2. That should still let s.s2's
// lifetime end after marker.
// CHECK: destroying simpleTestVar3b().first.s1
// CHECK: destroying simpleTestVar3b().first.s2
// CHECK: destroying simpleTestVar3b().second.s1
// CHECK: marker(_:): simpleTestVar3b().1
// CHECK: destroying simpleTestVar3b().second.s2
func simpleTestVar3b() {
    var s = S2("\(#function).first")
    s = S2("\(#function).second")
    consumeVal(s.s1)
    borrowVal(s.s2)
    marker("\(#function).1") // s2 should end its lifetime after marker.
}

// In this case, we are testing reinitialization and making sure that we can
// handle two initializations properly. We also are testing conditional merge
// logic. Since in both cases below s is completely consumed in b, s's lifetime
// would end at marker.

// CHECK: destroying simpleTestVar4(_:_:)[false, false)].first.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, false)].first.s2
// CHECK: marker(_:): simpleTestVar4(_:_:)[false, false)].1
// CHECK: destroying simpleTestVar4(_:_:)[false, false)].second.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, false)].second.s2
// CHECK: destroying simpleTestVar4(_:_:)[false, false)].third.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, false)].third.s2
// CHECK: marker(_:): simpleTestVar4(_:_:)[false, false)].2

// CHECK: destroying simpleTestVar4(_:_:)[false, true)].first.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, true)].first.s2
// CHECK: marker(_:): simpleTestVar4(_:_:)[false, true)].1
// CHECK: destroying simpleTestVar4(_:_:)[false, true)].second.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, true)].second.s2
// CHECK: destroying simpleTestVar4(_:_:)[false, true)].third.s1
// CHECK: destroying simpleTestVar4(_:_:)[false, true)].third.s2

// CHECK: destroying simpleTestVar4(_:_:)[true, false)].first.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, false)].first.s2
// CHECK: destroying simpleTestVar4(_:_:)[true, false)].second.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, false)].second.s2
// CHECK: destroying simpleTestVar4(_:_:)[true, false)].third.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, false)].third.s2
// CHECK: marker(_:): simpleTestVar4(_:_:)[true, false)].2

// CHECK: destroying simpleTestVar4(_:_:)[true, true)].first.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, true)].first.s2
// CHECK: destroying simpleTestVar4(_:_:)[true, true)].second.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, true)].second.s2
// CHECK: destroying simpleTestVar4(_:_:)[true, true)].third.s1
// CHECK: destroying simpleTestVar4(_:_:)[true, true)].third.s2
func simpleTestVar4(_ b1: Bool, _ b2: Bool) {
    var s = S2("\(#function)[\(b1), \(b2))].first")
    s = S2("\(#function)[\(b1), \(b2))].second")

    if b1 {
        consumeVal(s)
    } else {
        marker("\(#function)[\(b1), \(b2))].1")
        // S's lifetime should end after marker in this block.
    }

    s = S2("\(#function)[\(b1), \(b2))].third")

    if b2 {
        consumeVal(s)
    } else {
        marker("\(#function)[\(b1), \(b2))].2")
        // S's 2nd lifetime should end after marker in this block.
    }
}

// This test is similar to the previous, except we are consuming different
// values along the if/else branch that completely covers the value. As a result
// of this, we need to end the lifetime of s in the branches.
// CHECK: destroying simpleTestVar6(_:)[false].first.s1
// CHECK: destroying simpleTestVar6(_:)[false].first.s2
// CHECK: destroying simpleTestVar6(_:)[false].second.s2
// CHECK: marker(_:): simpleTestVar6(_:)[false].2
// CHECK: destroying simpleTestVar6(_:)[false].second.s1
// CHECK: destroying simpleTestVar6(_:)[false].third.s1
// CHECK: destroying simpleTestVar6(_:)[false].third.s2

// CHECK: destroying simpleTestVar6(_:)[true].first.s1
// CHECK: destroying simpleTestVar6(_:)[true].first.s2
// CHECK: destroying simpleTestVar6(_:)[true].second.s1
// CHECK: marker(_:): simpleTestVar6(_:)[true].1
// CHECK: destroying simpleTestVar6(_:)[true].second.s2
// CHECK: destroying simpleTestVar6(_:)[true].third.s1
// CHECK: destroying simpleTestVar6(_:)[true].third.s2
func simpleTestVar6(_ b: Bool) {
    var s = S2("\(#function)[\(b)].first")
    s = S2("\(#function)[\(b)].second")

    if b {
        consumeVal(s.s1) // end of s.s1's lifetime.
        marker("\(#function)[\(b)].1")
        // s.s2 should end here.
    } else {
        consumeVal(s.s2) // end of s.s2's lifetime
        marker("\(#function)[\(b)].2")
        // end of s.s1's lifetime should end after marker.
    }

    s = S2("\(#function)[\(b)].third")
}

// In this case, we are using S3 implying we have three fields. So despite the
// fact that we are deleting these two values in the if-else branches, s3's
// lifetime needs to end at end of scope.
// CHECK: destroying simpleTestVar6a(_:)[false].first.s1
// CHECK: destroying simpleTestVar6a(_:)[false].first.s2
// CHECK: destroying simpleTestVar6a(_:)[false].first.s3
// CHECK: destroying simpleTestVar6a(_:)[false].second.s2
// CHECK: marker(_:): simpleTestVar6a(_:)[false].2
// CHECK: destroying simpleTestVar6a(_:)[false].second.s1
// CHECK: marker(_:): simpleTestVar6a(_:)[false].3
// CHECK: destroying simpleTestVar6a(_:)[false].second.s3

// CHECK: destroying simpleTestVar6a(_:)[true].first.s1
// CHECK: destroying simpleTestVar6a(_:)[true].first.s2
// CHECK: destroying simpleTestVar6a(_:)[true].first.s3
// CHECK: destroying simpleTestVar6a(_:)[true].second.s1
// CHECK: marker(_:): simpleTestVar6a(_:)[true].1
// CHECK: destroying simpleTestVar6a(_:)[true].second.s2
// CHECK: marker(_:): simpleTestVar6a(_:)[true].3
// CHECK: destroying simpleTestVar6a(_:)[true].second.s3
func simpleTestVar6a(_ b: Bool) {
    var s = S3("\(#function)[\(b)].first")
    s = S3("\(#function)[\(b)].second")

    if b {
        consumeVal(s.s1) // end of s.s1's lifetime.
        marker("\(#function)[\(b)].1")
        // s.s2 should end here.
    } else {
        consumeVal(s.s2) // end of s.s2's lifetime
        marker("\(#function)[\(b)].2")
        // end of s.s1's lifetime should end after marker.
    }

    marker("\(#function)[\(b)].3")
    // s.s3's lifetime should end here.
}

// In this case, we are using S3, but we are consuming two disjoint parts of S
// in the if statement so we cover again completely.
// CHECK: destroying simpleTestVar6b(_:)[false].first.s1
// CHECK: destroying simpleTestVar6b(_:)[false].first.s2
// CHECK: destroying simpleTestVar6b(_:)[false].first.s3
// CHECK: destroying simpleTestVar6b(_:)[false].second.s2
// CHECK: marker(_:): simpleTestVar6b(_:)[false].2
// CHECK: destroying simpleTestVar6b(_:)[false].second.s3
// CHECK: destroying simpleTestVar6b(_:)[false].second.s1
// CHECK: marker(_:): simpleTestVar6b(_:)[false].3

// CHECK: destroying simpleTestVar6b(_:)[true].first.s1
// CHECK: destroying simpleTestVar6b(_:)[true].first.s2
// CHECK: destroying simpleTestVar6b(_:)[true].first.s3
// CHECK: destroying simpleTestVar6b(_:)[true].second.s1
// CHECK: destroying simpleTestVar6b(_:)[true].second.s3
// CHECK: marker(_:): simpleTestVar6b(_:)[true].1
// CHECK: destroying simpleTestVar6b(_:)[true].second.s2
// CHECK: marker(_:): simpleTestVar6b(_:)[true].3
func simpleTestVar6b(_ b: Bool) {
    var s = S3("\(#function)[\(b)].first")
    s = S3("\(#function)[\(b)].second")

    if b {
        consumeVal(s.s1) // end of s.s1's lifetime.
        consumeVal(s.s3) // end of s.s3's lifetime
        marker("\(#function)[\(b)].1")
        // s.s2 should end here.
    } else {
        consumeVal(s.s2) // end of s.s2's lifetime
        marker("\(#function)[\(b)].2")
        // end of s.s1's lifetime should end after marker.
        // end of s.s3's lifetime should end after marker.
    }

    marker("\(#function)[\(b)].3")
}


simpleTestVar()
simpleTestVar2()
simpleTestVar3()
simpleTestVar3a()
simpleTestVar3b()
simpleTestVar4(false, false)
simpleTestVar4(false, true)
simpleTestVar4(true, false)
simpleTestVar4(true, true)
simpleTestVar6(false)
simpleTestVar6(true)
simpleTestVar6a(false)
simpleTestVar6a(true)
simpleTestVar6b(false)
simpleTestVar6b(true)