File: arguments.sil

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 (769 lines) | stat: -rw-r--r-- 27,820 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// RUN: %target-sil-opt -module-name Swift -sil-ownership-verifier-enable-testing -ownership-verifier-textual-error-dumper -enable-sil-verify-all=0 %s -o /dev/null 2>&1 | %FileCheck %s
// REQUIRES: asserts

// This is a test that verifies ownership behavior around arguments that should
// be flagged as failures.

sil_stage canonical

import Builtin

//////////////////
// Declarations //
//////////////////

struct NativeObjectPair {
  var obj1 : Builtin.NativeObject
  var obj2 : Builtin.NativeObject
}

enum FakeOptional<T> {
case none
case some(T)
}

///////////
// Tests //
///////////

// CHECK-NOT: Function: 'no_end_borrow_error1'
sil [ossa] @no_end_borrow_error1 : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  br bb1(%0 : $Builtin.NativeObject)

bb1(%1 : @guaranteed $Builtin.NativeObject):
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'no_end_borrow_error2'
// CHECK: Non trivial values, non address values, and non guaranteed function args must have at least one lifetime ending use?!
// CHECK: Value: %3 = argument of bb1 : $Builtin.NativeObject
// CHECK-LABEL: Error#: 0. End Error in Function: 'no_end_borrow_error2'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'no_end_borrow_error2'
sil [ossa] @no_end_borrow_error2 : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject):
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'leak_along_path'
// CHECK: Error! Found a leak due to a consuming post-dominance failure!
// CHECK: Value: %3 = argument of bb1 : $Builtin.NativeObject
// CHECK: Post Dominating Failure Blocks:
// CHECK: bb3
// CHECK: Error#: 0. End Error in Function: 'leak_along_path'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'leak_along_path'
sil [ossa] @leak_along_path : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  end_borrow %2 : $Builtin.NativeObject
  br bb4

bb3:
  br bb4

bb4:
  %9999 = tuple()
  return %9999 : $()
}

// Make sure that we only flag the subargument leak and not the parent
// argument. Also check for over consuming due to phi nodes.

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'leak_along_subarg_path'
// CHECK: Error! Found a leak due to a consuming post-dominance failure!
// CHECK: Value: %7 = argument of bb3 : $Builtin.NativeObject
// CHECK: Post Dominating Failure Blocks:
// CHECK: bb6
// CHECK: Error#: 0. End Error in Function: 'leak_along_subarg_path'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'leak_along_subarg_path'
sil [ossa] @leak_along_subarg_path : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb5

bb2:
  %2a = begin_borrow %2 : $Builtin.NativeObject
  br bb3(%2a : $Builtin.NativeObject)

bb3(%3 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb4, bb6

bb4:
  end_borrow %3 : $Builtin.NativeObject
  br bb7

bb5:
  br bb7

bb6:
  br bb7
  
bb7:
  end_borrow %2 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-NOT: Function: 'good_order_1'
sil [ossa] @good_order_1 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  cond_br undef, bb1, bb5

bb1:
  br bb2(%1 : $Builtin.NativeObject)

bb2(%2 : @guaranteed $Builtin.NativeObject):
  end_borrow %2 : $Builtin.NativeObject
  cond_br undef, bb3, bb6

bb3:
  br bb4

bb4:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()

bb5:
  end_borrow %1 : $Builtin.NativeObject
  br bb4

bb6:
  br bb4
}

// CHECK-NOT: Function: 'good_order_2'
sil [ossa] @good_order_2 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  cond_br undef, bb1, bb5

bb1:
  %1a = begin_borrow %1 : $Builtin.NativeObject
  br bb2(%1a : $Builtin.NativeObject)

bb2(%2 : @guaranteed $Builtin.NativeObject):
  end_borrow %2 : $Builtin.NativeObject
  end_borrow %1 : $Builtin.NativeObject
  cond_br undef, bb3, bb6

bb3:
  br bb4

bb4:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()

bb5:
  end_borrow %1 : $Builtin.NativeObject
  br bb4

bb6:
  br bb4
}


// Make sure that we only flag the end_borrow "use after free" in bb2.
//
// CHECK-LABEL: Error#: 0. Begin Error in Function: 'bad_order'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject
// CHECK: Block: bb2
// CHECK: Error#: 0. End Error in Function: 'bad_order'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'bad_order'
sil [ossa] @bad_order : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  cond_br undef, bb1, bb5

bb1:
  %1a = begin_borrow %1 : $Builtin.NativeObject
  br bb2(%1a : $Builtin.NativeObject)

bb2(%2 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  cond_br undef, bb3, bb6

bb3:
  br bb4

bb4:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()

bb5:
  end_borrow %1 : $Builtin.NativeObject
  br bb4

bb6:
  br bb4
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %19, %14, %6, %3
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject           // id: %6
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %7
// CHECK: Block: bb2
// CHECK-LABEL: Error#: 0. End Error in Function: 'bad_order_add_a_level'

// CHECK-LABEL: Error#: 1. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found over consume?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %19, %14, %6, %3
// CHECK: User:   end_borrow %1 : $Builtin.NativeObject           // id: %14
// CHECK: Block: bb2
// CHECK: Consuming Users:
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %19
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %14
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %6
// CHECK: Error#: 1. End Error in Function: 'bad_order_add_a_level'
//
// This block is reported as leaking block since given our partial-cfg:
//
//                /---> BB3
// BB1 -> BB2 ------> BB5
//
// we create the borrow at BB1 and end its lifetime at BB2 and BB5. This causes
// us to walk up, identify they double consume at BB2 from BB5. Since we skipped
// BB3 that is identified as a leaking block appropriately.
//
// CHECK-LABEL: Error#: 2. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Error! Found a leak due to a consuming post-dominance failure!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %19, %14, %6, %3
// CHECK: Post Dominating Failure Blocks:
// CHECK: bb3
// CHECK-LABEL: Error#: 2. End Error in Function: 'bad_order_add_a_level'
//
// CHECK-LABEL: Error#: 3. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found outside of lifetime use!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %19, %14, %6, %3
// CHECK: User:  %9 = begin_borrow %5 : $Builtin.NativeObject    // user: %10
// CHECK: Block: bb3
// CHECK-LABEL: Error#: 3. End Error in Function: 'bad_order_add_a_level'
//
// CHECK-LABEL: Error#: 6. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found over consume?!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %13, %9, %7
// CHECK: User:   end_borrow %5 : $Builtin.NativeObject           // id: %13
// CHECK: Block: bb2
// CHECK: Consuming Users:
// CHECK:   end_borrow %5 : $Builtin.NativeObject           // id: %13
// CHECK:   end_borrow %5 : $Builtin.NativeObject           // id: %7
// CHECK: Error#: 6. End Error in Function: 'bad_order_add_a_level'
//
// CHECK-LABEL: Error#: 7. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Error! Found a leak due to a consuming post-dominance failure!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %13, %9, %7
// CHECK: Post Dominating Failure Blocks:
// CHECK: bb3
// CHECK: Error#: 7. End Error in Function: 'bad_order_add_a_level'

// CHECK: Error#: 9. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Error! Found a leak due to a consuming post-dominance failure!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %13, %9, %7
// CHECK: Post Dominating Failure Blocks:
// CHECK: bb3
// CHECK-LABEL: Error#: 9. End Error in Function: 'bad_order_add_a_level'
//
// CHECK-LABEL: Error#: 10. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found outside of lifetime use!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %13, %9, %7
// CHECK: User:  br bb4(%9 : $Builtin.NativeObject)              // id: %10
// CHECK: Block: bb3
// CHECK-LABEL: Error#: 10. End Error in Function: 'bad_order_add_a_level'
//
// We found a use that we didn't visit. For our purposes, we consider this to be
// an outside lifetime use rather than use after freer to unite it with errors
// from uses that are not reachable from the definition of the value.
//
// CHECK-LABEL: Error#: 11. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Found outside of lifetime use!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %13, %9, %7
// CHECK: User:  %9 = begin_borrow %5 : $Builtin.NativeObject    // user: %10
// CHECK: Block: bb3
// CHECK: Error#: 11. End Error in Function: 'bad_order_add_a_level'
//
// CHECK-LABEL: Error#: 12. Begin Error in Function: 'bad_order_add_a_level'
// CHECK: Non trivial values, non address values, and non guaranteed function args must have at least one lifetime ending use?!
// CHECK: Value: %11 = argument of bb4 : $Builtin.NativeObject
// CHECK: Error#: 12. End Error in Function: 'bad_order_add_a_level'
sil [ossa] @bad_order_add_a_level : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  cond_br undef, bb1, bb7

bb1:
  %2 = begin_borrow %1 : $Builtin.NativeObject
  br bb2(%2 : $Builtin.NativeObject)

bb2(%4 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Builtin.NativeObject
  end_borrow %4 : $Builtin.NativeObject
  cond_br undef, bb3, bb5

bb3:
  %4a = begin_borrow %4 : $Builtin.NativeObject
  br bb4(%4a : $Builtin.NativeObject)

bb4(%9 : @guaranteed $Builtin.NativeObject):
  br bb6

bb5:
  end_borrow %4 : $Builtin.NativeObject
  end_borrow %1 : $Builtin.NativeObject
  br bb6

bb6:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()

bb7:
  end_borrow %1 : $Builtin.NativeObject
  br bb6
}

// Check that we only add an argument to the use set of its parent argument.
// This ensures that when checking we do not waste a bunch of compile time by
// propagating up end_borrows through many arguments. Also make sure that we do
// properly visit terminators so that we do not erroneously flag them as
// improper uses.
//
// CHECK-LABEL: Error#: 0. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %22, %16, %12, %6, %3
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject           // id: %16
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %17
// CHECK: Block: bb5
// CHECK-LABEL: Error#: 0. End Error in Function: 'bad_order_add_a_level_2'
//
// CHECK-LABEL: Error#: 1. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %22, %16, %12, %6, %3
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject           // id: %12
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %13
// CHECK: Block: bb4
// CHECK-LABEL:Error#: 1. End Error in Function: 'bad_order_add_a_level_2'

// CHECK-LABEL: Error#: 2. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %22, %16, %12, %6, %3
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject           // id: %6
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %7
// CHECK: Block: bb2
// CHECK-LABEL: Error#: 2. End Error in Function: 'bad_order_add_a_level_2'
//
// CHECK-LABEL: Error#: 3. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found over consume?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %22, %16, %12, %6, %3
// CHECK: User:   end_borrow %1 : $Builtin.NativeObject           // id: %16
// CHECK: Block: bb2
// CHECK: Consuming Users:
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %22
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %16
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %12
// CHECK:   end_borrow %1 : $Builtin.NativeObject           // id: %6
// CHECK-LABEL: Error#: 3. End Error in Function: 'bad_order_add_a_level_2'
//
// CHECK-LABEL: Error#: 7. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found outside of lifetime use?!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %17, %13, %9, %7
// CHECK: Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %13
// CHECK: Non Consuming User:   end_borrow %11 : $Builtin.NativeObject          // id: %14
// CHECK: Block: bb4
// CHECK-LABEL: Error#: 7. End Error in Function: 'bad_order_add_a_level_2'
// CHECK-LABEL: Error#: 8. Begin Error in Function: 'bad_order_add_a_level_2'
// CHECK: Found over consume?!
// CHECK: Value: %5 = argument of bb2 : $Builtin.NativeObject      // users: %17, %13, %9, %7
// CHECK: User:   end_borrow %5 : $Builtin.NativeObject           // id: %17
// CHECK: Block: bb2
// CHECK: Consuming Users:
// CHECK:   end_borrow %5 : $Builtin.NativeObject           // id: %17
// CHECK:   end_borrow %5 : $Builtin.NativeObject           // id: %13
// CHECK:   end_borrow %5 : $Builtin.NativeObject           // id: %7
// CHECK-LABEL: Error#: 8. End Error in Function: 'bad_order_add_a_level_2'
//
// NOTE: There are 2-3 errors here we are not pattern matching. We should add
// patterns for them so we track if they are changed.
sil [ossa] @bad_order_add_a_level_2 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  cond_br undef, bb1, bb7

bb1:
  %1a = begin_borrow %1 : $Builtin.NativeObject
  br bb2(%1a : $Builtin.NativeObject)

bb2(%4 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Builtin.NativeObject
  end_borrow %4 : $Builtin.NativeObject
  cond_br undef, bb3, bb5

bb3:
  %4a = begin_borrow %4 : $Builtin.NativeObject
  br bb4(%4a : $Builtin.NativeObject)

bb4(%9 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Builtin.NativeObject
  end_borrow %4 : $Builtin.NativeObject
  end_borrow %9 : $Builtin.NativeObject
  br bb6

bb5:
  end_borrow %1 : $Builtin.NativeObject
  end_borrow %4 : $Builtin.NativeObject
  br bb6

bb6:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()

bb7:
  end_borrow %1 : $Builtin.NativeObject
  br bb6
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'owned_argument_overuse_br1'
// CHECK-NEXT: Found over consume?!
// CHECK-NEXT: Value: %0 = argument of bb0 : $Builtin.NativeObject
// CHECK-NEXT: User:   destroy_value %0 : $Builtin.NativeObject
// CHECK-NEXT: Block: bb0
// CHECK: Error#: 0. End Error in Function: 'owned_argument_overuse_br1'
//
// CHECK-LABEL: Error#: 1. Begin Error in Function: 'owned_argument_overuse_br1'
// CHECK-NEXT: Error! Found a leaked owned value that was never consumed.
// CHECK-NEXT: Value: %3 = argument of bb1 : $Builtin.NativeObject
// CHECK-NOT: Block
// CHECK: Error#: 1. End Error in Function: 'owned_argument_overuse_br1'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'owned_argument_overuse_br1'
sil [ossa] @owned_argument_overuse_br1 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  destroy_value %0 : $Builtin.NativeObject
  br bb1(%0 : $Builtin.NativeObject)

bb1(%1 : @owned $Builtin.NativeObject):
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'owned_argument_overuse_br2'
// CHECK-NEXT: Found over consume?!
// CHECK-NEXT: Value: %0 = argument of bb0 : $Builtin.NativeObject
// CHECK-NEXT: User:   destroy_value %0 : $Builtin.NativeObject
// CHECK-NEXT: Block: bb0
// CHECK-NOT: Block
// CHECK: Error#: 0. End Error in Function: 'owned_argument_overuse_br2'
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'owned_argument_overuse_br2'
sil [ossa] @owned_argument_overuse_br2 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  destroy_value %0 : $Builtin.NativeObject
  br bb1(%0 : $Builtin.NativeObject)

bb1(%1 : @owned $Builtin.NativeObject):
  destroy_value %1 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

/////////////////////////////////////////
// Movable Guaranteed Scope Test Cases //
/////////////////////////////////////////
//
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'simple_non_postdominating_diamond_with_forwarding_uses'
sil [ossa] @simple_non_postdominating_diamond_with_forwarding_uses : $@convention(thin) (@in_guaranteed NativeObjectPair, @guaranteed NativeObjectPair) -> () {
bb0(%0 : $*NativeObjectPair, %1 : @guaranteed $NativeObjectPair):
  cond_br undef, bb1, bb2

bb1:
  %3 = load_borrow %0 : $*NativeObjectPair
  %4 = struct_extract %3 : $NativeObjectPair, #NativeObjectPair.obj1
  br bb3(%4 : $Builtin.NativeObject, %3 : $NativeObjectPair)

bb2:
  %5 = begin_borrow %1 : $NativeObjectPair
  %6 = struct_extract %5 : $NativeObjectPair, #NativeObjectPair.obj1
  br bb3(%6 : $Builtin.NativeObject, %5 : $NativeObjectPair)

bb3(%7 : @guaranteed $Builtin.NativeObject, %8 : @guaranteed $NativeObjectPair):
  end_borrow %8 : $NativeObjectPair
  %9999 = tuple()
  return %9999 : $()
}

sil [ossa] @simple_loop_carry_borrow_owned_arg : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  br bb1(%2 : $Builtin.NativeObject)

bb3:
  // Should error b/c destroy_value before end_borrow.
  destroy_value %0 : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

sil [ossa] @simple_loop_carry_implicitregularusers_do_not_loop_carry : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1a = begin_borrow %0 : $Builtin.NativeObject
  %1b = copy_value %0 : $Builtin.NativeObject
  br bb1(%1a : $Builtin.NativeObject, %1b : $Builtin.NativeObject)

bb1(%2a : @guaranteed $Builtin.NativeObject, %2b : @owned $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  %3 = copy_value %2a : $Builtin.NativeObject
  %3a = begin_borrow %3 : $Builtin.NativeObject
  end_borrow %2a : $Builtin.NativeObject
  destroy_value %2b : $Builtin.NativeObject
  br bb1(%3a : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb3:
  end_borrow %2a : $Builtin.NativeObject
  destroy_value %2b : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// We should identify bb0a as consuming the value twice.
//
// CHECK-LABEL: Error#: 0. Begin Error in Function: 'simple_loop_carry_over_consume'
// CHECK-NEXT: Found over consume?!
// CHECK-NEXT: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject    // users: %3, %3
// CHECK-NEXT: User:   br bb2(%1 : $Builtin.NativeObject, %1 : $Builtin.NativeObject) // id: %3
// CHECK-NEXT: Block: bb1
// CHECK-NEXT: Consuming Users:
// CHECK-NEXT:   br bb2(%1 : $Builtin.NativeObject, %1 : $Builtin.NativeObject) // id: %3
// CHECK-NEXT:   br bb2(%1 : $Builtin.NativeObject, %1 : $Builtin.NativeObject) // id: %3
// CHECK: Error#: 0. End Error in Function: 'simple_loop_carry_over_consume'
//
// CHECK-LABEL: Error#: 1. Begin Error in Function: 'simple_loop_carry_over_consume'
// CHECK: Found outside of lifetime use?!
// CHECK: Value: %4 = argument of bb2 : $Builtin.NativeObject
// CHECK: Consuming User:   end_borrow %4 : $Builtin.NativeObject
// CHECK: Non Consuming User:   br bb2(%5 : $Builtin.NativeObject, %8 : $Builtin.NativeObject)
// CHECK: Block: bb4
// CHECK-LABEL: Error#: 1. End Error in Function: 'simple_loop_carry_over_consume'
// Some more errors are emitted
sil [ossa] @simple_loop_carry_over_consume : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb0a

bb0a:
  br bb1(%1 : $Builtin.NativeObject, %1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject, %2a : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  %3 = begin_borrow %2a : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  br bb1(%2a : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb3:
  end_borrow %2a : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-NOT: Function: 'simple_loop_carry_cycle'
sil [ossa] @simple_loop_carry_cycle : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  br bb1(%2 : $Builtin.NativeObject)

bb3:
  end_borrow %2 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'simple_loop_carry_borrows_do_not_loop_carry'
// CHECK: Found outside of lifetime use?!
// CHECK: Value: %4 = argument of bb1 : $Builtin.NativeObject
// CHECK: Consuming User:   end_borrow %4 : $Builtin.NativeObject
// CHECK: Non Consuming User:   br bb1(%5 : $Builtin.NativeObject, %8 : $Builtin.NativeObject)
// CHECK: Block: bb3
// CHECK-LABEL: Error#: 0. End Error in Function: 'simple_loop_carry_borrows_do_not_loop_carry'
sil [ossa] @simple_loop_carry_borrows_do_not_loop_carry : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  %1a = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject, %1a : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject, %2a : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  %3 = begin_borrow %2a : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  br bb1(%2a : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb3:
  end_borrow %2a : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'simple_loop_carry_borrows_do_not_loop_carry2'
// CHECK: Found outside of lifetime use?!
// CHECK: Value: %4 = argument of bb1 : $Builtin.NativeObject      // users: %11, %9
// CHECK: Consuming User:   end_borrow %4 : $Builtin.NativeObject           // id: %11
// CHECK: Non Consuming User:   end_borrow %5 : $Builtin.NativeObject           // id: %12
// CHECK: Block: bb4
// CHECK: Error#: 0. End Error in Function: 'simple_loop_carry_borrows_do_not_loop_carry2'
sil [ossa] @simple_loop_carry_borrows_do_not_loop_carry2 : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  %1a = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%1 : $Builtin.NativeObject, %1a : $Builtin.NativeObject)

bb1(%2 : @guaranteed $Builtin.NativeObject, %2a : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  %3 = begin_borrow %2a : $Builtin.NativeObject
  end_borrow %2 : $Builtin.NativeObject
  br bb1(%2a : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb3:
  end_borrow %2 : $Builtin.NativeObject
  end_borrow %2a : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-NOT: Function: 'simple_validate_enclosing_borrow_around_loop'
sil [ossa] @simple_validate_enclosing_borrow_around_loop : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  %2 = begin_borrow %1 : $Builtin.NativeObject
  %3 = begin_borrow %1 : $Builtin.NativeObject
  br bb1(%2 : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb1(%4 : @guaranteed $Builtin.NativeObject, %5 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  br bb1(%5 : $Builtin.NativeObject, %4 : $Builtin.NativeObject)

bb3:
  end_borrow %4 : $Builtin.NativeObject
  end_borrow %5 : $Builtin.NativeObject
  end_borrow %1 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-NOT: Function: 'simple_validate_enclosing_borrow_around_loop_2'
sil [ossa] @simple_validate_enclosing_borrow_around_loop_2 : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  %2 = begin_borrow %0 : $Builtin.NativeObject
  %3 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%2 : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb1(%4 : @guaranteed $Builtin.NativeObject, %5 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  br bb1(%5 : $Builtin.NativeObject, %4 : $Builtin.NativeObject)

bb3:
  end_borrow %4 : $Builtin.NativeObject
  end_borrow %5 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-NOT: Function: 'simple_validate_enclosing_borrow_around_loop_3'
sil [ossa] @simple_validate_enclosing_borrow_around_loop_3 : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = begin_borrow %0 : $Builtin.NativeObject
  %3 = begin_borrow %0 : $Builtin.NativeObject
  br bb1(%2 : $Builtin.NativeObject, %3 : $Builtin.NativeObject)

bb1(%4 : @guaranteed $Builtin.NativeObject, %5 : @guaranteed $Builtin.NativeObject):
  cond_br undef, bb2, bb3

bb2:
  br bb2a

bb2a:
  br bb1(%5 : $Builtin.NativeObject, %4 : $Builtin.NativeObject)

bb3:
  end_borrow %4 : $Builtin.NativeObject
  end_borrow %5 : $Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}