File: noimplicitcopy_consuming_parameters.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 (999 lines) | stat: -rw-r--r-- 27,759 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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// RUN: %target-swift-frontend -emit-sil %s -verify -sil-verify-all

////////////////////////
// MARK: Declarations //
////////////////////////

public class Klass {
}

public struct NonTrivialStruct {
    var k = Klass()
    let i = 0

    func doSomethingDefault() {}
    borrowing func doSomethingBorrowing() {}
    consuming func doSomethingConsuming() {}
    mutating func doSomethingMutating() {}
}

public protocol P {
    static var value: Self { get }
}

public struct GenericNonTrivialStruct<T : P> {
    var t = T.value

    func doSomethingDefault() {}
    borrowing func doSomethingBorrowing() {}
    consuming func doSomethingConsuming() {}
    mutating func doSomethingMutating() {}
}

public struct TrivialStruct {
    var k: Int = 5

    func doSomethingDefault() {}
    consuming func doSomethingConsuming() {}
    borrowing func doSomethingBorrowing() {}
    mutating func doSomethingMutating() {}
}

enum LoadableEnum {
    case x(NonTrivialStruct)
    case y(Int)
}

enum TrivialEnum {
    case x(Int64)
    case y(Int)
}

enum AddressOnlyEnum<T> {
    case x(NonTrivialStruct)
    case y(T)
}

func borrowValDefault(_ x: NonTrivialStruct) {}
func borrowValBorrowing(_ x: borrowing NonTrivialStruct) {}
func consumeVal(_ x: consuming NonTrivialStruct) {}
func borrowValDefault(_ x: TrivialStruct) {}
func borrowValBorrowing(_ x: borrowing TrivialStruct) {}
func consumeVal(_ x: consuming TrivialStruct) {}
func borrowValDefault<T : P>(_ x: GenericNonTrivialStruct<T>) {}
func borrowValBorrowing<T : P>(_ x: borrowing GenericNonTrivialStruct<T>) {}
func consumeVal<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {}

///////////////////////////////////////////
// MARK: Simple Loadable Borrowing Tests //
///////////////////////////////////////////

func testLoadableConsumingConsume(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    let _ = x.k // expected-note {{used here}}
}

func testLoadableConsumingConsume1a(_ x: consuming NonTrivialStruct) {
    let _ = x
    x = NonTrivialStruct()
    let _ = x.k
}

func testLoadableConsumingConsume2(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' consumed more than once}}
    var y = x // expected-note {{consumed here}}
    y = x // expected-note {{consumed again here}}
    _ = y
}

func testLoadableConsumingConsume3(_ x: consuming NonTrivialStruct) {
    let y = x
    _ = y
}

func testLoadableConsumingUse(_ x: consuming NonTrivialStruct) {
    _ = x
}

func testLoadableConsumingUseAndConsume(_ x: consuming NonTrivialStruct) {
    borrowValBorrowing(x)
    let _ = x
}

func testLoadableConsumingConsumeAndUse(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    borrowValBorrowing(x) // expected-note {{used here}}
}

func testLoadableConsumingConsumeAndUseReinit(_ x: consuming NonTrivialStruct) {
    let _ = x
    x = NonTrivialStruct()
    borrowValBorrowing(x)
}

func testLoadableConsumingCallBorrowValDefault(_ x: consuming NonTrivialStruct) {
    borrowValDefault(x)
}

func testLoadableConsumingCallBorrowValBorrowing(_ x: consuming NonTrivialStruct) {
    borrowValBorrowing(x)
}

func testLoadableConsumingCallConsumeVal(_ x: consuming NonTrivialStruct) {
    consumeVal(x)
}

func testLoadableConsumingCallConsumeValMultiple(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' consumed more than once}}
    consumeVal(x) // expected-note {{consumed here}}
    consumeVal(x) // expected-note {{consumed again here}}
}

func testLoadableConsumingCallMethodSelfDefault(_ x: consuming NonTrivialStruct) {
    x.doSomethingDefault()
}

func testLoadableConsumingCallMethodSelfConsuming(_ x: consuming NonTrivialStruct) {
    x.doSomethingConsuming()
}

func testLoadableConsumingCallMethodSelfMutating(_ x: consuming NonTrivialStruct) {
    x.doSomethingMutating()
}

func testLoadableConsumingEscapingClosure(_ x: consuming NonTrivialStruct) {
    var f: () -> () = {}
    f = {
        _ = x // expected-error{{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    }
    _ = f
}

func testLoadableConsumingEscapingClosure2(_ x: consuming NonTrivialStruct) {
    _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    var f: () -> () = {}
    f = {
        _ = x // expected-error{{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    }
    _ = f
}

func testLoadableConsumingEscapingClosure3(_ x: consuming NonTrivialStruct) {
    var f: () -> () = {}
    f = {
        _ = x // expected-error{{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    }
    _ = f
    _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
}

func testLoadableConsumingNonEscapingClosure(_ x: consuming NonTrivialStruct) { // expected-error{{}}
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping {
        _ = x // expected-note{{consumed here}}
    }
}

func testLoadableConsumingNonEscapingClosure2(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    // expected-error @-2 {{}}
    let _ = x // expected-note {{consumed here}}
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping { // expected-note {{used here}}
        _ = x // expected-note {{consumed here}}
    }
}

func testLoadableConsumingConsumeOperator(_ x: consuming NonTrivialStruct) {
    _ = consume x
}

func testLoadableConsumingConsumeOperator1(_ x: consuming NonTrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    _ = consume x // expected-note {{consumed here}}
    borrowValDefault(x) // expected-note {{used here}}
}

func testLoadableConsumingConsumeOperator2(_ x: consuming NonTrivialStruct) {
    x = consume x
}

func testLoadableConsumingConsumeOperator3(_ x: consuming NonTrivialStruct) {
    _ = consume x
    let y = NonTrivialStruct()
    x = consume y
}

func testLoadableConsumingEnum(_ x: consuming LoadableEnum) {
    switch x {
    case let .x(y):
        _ = y
        break
    case .y:
        break
    }
}

func testLoadableConsumingEnum2(_ x: consuming LoadableEnum) {
    // expected-error @-1 {{'x' consumed more than once}}
    switch x { // expected-note {{consumed here}}
    case let .x(y):
        _ = consume x // expected-note {{consumed again here}}
        _ = y
        break
    case .y:
        break
    }
}

func testLoadableConsumingCopyOperator(_ x: consuming NonTrivialStruct) {
    _ = copy x
}

func testLoadableConsumingTrivialLetField(_ x: consuming NonTrivialStruct) -> Int {
    return x.i
}

//////////////////////////////////////////
// MARK: Trivial Struct Consuming Tests //
//////////////////////////////////////////

func testTrivialConsumingConsume(_ x: consuming TrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    let _ = x.k // expected-note {{used here}}
}

func testTrivialConsumingConsume2(_ x: consuming TrivialStruct) {
    // expected-error @-1 {{'x' consumed more than once}}
    var y = x // expected-note {{consumed here}}
    y = x // expected-note {{consumed again here}}
    _ = y
}

func testTrivialConsumingConsume3(_ x: consuming TrivialStruct) {
    let y = x
    _ = y
}

func testTrivialConsumingUse(_ x: consuming TrivialStruct) {
    _ = x
}

func testTrivialConsumingUseAndConsume(_ x: consuming TrivialStruct) {
    borrowValBorrowing(x)
    let _ = x
}

func testTrivialConsumingConsumeAndUse(_ x: consuming TrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    borrowValBorrowing(x) // expected-note {{used here}}
}

func testTrivialConsumingConsumeAndUseReinit(_ x: consuming TrivialStruct) {
    let _ = x
    x = TrivialStruct()
    borrowValBorrowing(x)
}

func testTrivialConsumingCallBorrowValDefault(_ x: consuming TrivialStruct) {
    borrowValDefault(x)
}

func testTrivialConsumingCallBorrowValBorrowing(_ x: consuming TrivialStruct) {
    borrowValBorrowing(x)
}

func testTrivialConsumingCallConsumeVal(_ x: consuming TrivialStruct) {
    consumeVal(x)
}

func testTrivialConsumingCallMethodSelfDefault(_ x: consuming TrivialStruct) {
    x.doSomethingDefault()
}

func testTrivialConsumingCallMethodSelfConsuming(_ x: consuming TrivialStruct) {
    x.doSomethingConsuming()
}

func testTrivialConsumingEscapingClosure(_ x: consuming TrivialStruct) {
    var f: () -> () = {}
    f = {
        _ = x
    }
    _ = f
}

func testTrivialConsumingEscapingClosure2(_ x: consuming TrivialStruct) {
    let _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    var f: () -> () = {}
    f = {
        _ = x
    }
    _ = f
}

func testTrivialConsumingEscapingClosure3(_ x: consuming TrivialStruct) {
    var f: () -> () = {}
    f = {
        _ = x
    }
    _ = f
    let _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
}

func testTrivialConsumingNonEscapingClosure(_ x: consuming TrivialStruct) {
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping {
        _ = x
    }
}

func testTrivialConsumingNonEscapingClosure2(_ x: consuming TrivialStruct) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping { // expected-note {{used here}}
        _ = x
    }
}

func testTrivialConsumingConsumeOperator(_ x: consuming TrivialStruct) {
    _ = consume x
}

func testTrivialConsumingEnum(_ x: consuming TrivialEnum) {
    switch x {
    case let .x(y):
        _ = y
        break
    case .y:
        break
    }
}

func testTrivialConsumingEnum2(_ x: consuming TrivialEnum) {
    // expected-error @-1 {{'x' used after consume}}
    switch x { // expected-note {{consumed here}}
    case let .x(y):
        _ = y
        break
    case .y:
        _ = copy x // expected-note {{used here}}
        break
    }
}


func testTrivialConsumingCopyOperator(_ x: consuming TrivialStruct) {
    _ = copy x
}

//////////////////////////////////////////////
// MARK: Simple AddressOnly Consuming Tests //
//////////////////////////////////////////////

func testAddressOnlyBorrowSimple<T>(_ x: borrowing T) {}

func testAddressOnlyConsumingConsume<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    let _ = x.t // expected-note {{used here}}
}

func testAddressOnlyConsumingConsume2<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' consumed more than once}}
    var y = x // expected-note {{consumed here}}
    y = x // expected-note {{consumed again here}}
    _ = y
}

func testAddressOnlyConsumingConsume3<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    let y = x
    _ = y
}

func testAddressOnlyConsumingUse<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    _ = x
}

func testAddressOnlyConsumingUseAndConsume<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    borrowValDefault(x)
    let _ = x
}

func testAddressOnlyConsumingConsumeAndUse<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    borrowValDefault(x) // expected-note {{used here}}
}

func testAddressOnlyConsumingConsumeAndUseReinit<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    let _ = x
    x = GenericNonTrivialStruct<T>()
    borrowValDefault(x)
}

func testAddressOnlyConsumingCallBorrowValDefault<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    borrowValDefault(x)
}

func testAddressOnlyConsumingCallBorrowValBorrowing<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    borrowValBorrowing(x)
}

func testAddressOnlyConsumingCallConsumeVal<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    consumeVal(x)
}

func testAddressOnlyConsumingCallMethodSelfDefault<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    x.doSomethingDefault()
}

func testAddressOnlyConsumingCallMethodSelfConsuming<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    x.doSomethingConsuming()
}

func testAddressOnlyConsumingEscapingClosure<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    var f: () -> () = {}
    f = {
        _ = copy x
    }
    _ = f
}

func testAddressOnlyConsumingEscapingClosure2<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    let _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
    var f: () -> () = {}
    f = {
        _ = copy x
    }
    _ = f
}

func testAddressOnlyConsumingEscapingClosure3<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    var f: () -> () = {}
    f = {
        _ = copy x
    }
    _ = f
    let _ = x // expected-error {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
}

func testAddressOnlyConsumingNonEscapingClosure<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping {
        _ = copy x
    }
}

func testAddressOnlyConsumingNonEscapingClosure2<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' used after consume}}
    let _ = x // expected-note {{consumed here}}
    func useNonEscaping(_ f: () -> ()) {}
    useNonEscaping { // expected-note {{used here}}
        _ = copy x
    }
}

func testAddressOnlyConsumingCast<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    let _ = x as Any
}

func testAddressOnlyConsumingCast2<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' consumed more than once}}
    let _ = x as Any // expected-note {{consumed here}}
    let _ = x as Any // expected-note {{consumed again here}}
}

func testAddressOnlyConsumingCastCheck<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    if x is Any { // expected-warning {{'is' test is always true}}
    }
}

func testAddressOnlyConsumingCastCheck2<T : P>(_ x: consuming GenericNonTrivialStruct<T>) {
    // expected-error @-1 {{'x' consumed more than once}}
    if x is Any { // expected-note {{consumed here}}
        // expected-warning @-1 {{'is' test is always true}}

    }
    if x is Any { // expected-note {{consumed again here}}
        // expected-warning @-1 {{'is' test is always true}}
    }
}

func testAddressOnlyConsumingEnum<T>(_ x: consuming AddressOnlyEnum<T>) {
    switch x {
    case let .x(y):
        _ = y
        break
    case let .y(z):
        _ = z
        break
    }
}

func testAddressOnlyConsumingEnum2<T>(_ x: consuming AddressOnlyEnum<T>) {
    // expected-error @-1 {{'x' used after consume}}
    switch x { // expected-note {{consumed here}}
    case let .x(y):
        _ = y
        break
    case let .y(z):
        _ = z
        break
    }
    _ = copy x // expected-note {{used here}}
}

func testAddressOnlyConsumeOperator<T>(_ x: consuming GenericNonTrivialStruct<T>) {
    _ = consume x
}

func testAddressOnlyCopyOperator<T>(_ x: consuming GenericNonTrivialStruct<T>) {
    _ = copy x 
}

///////////////////////////////
// MARK: Loadable Self Tests //
///////////////////////////////

struct LoadableSelfTest {
    var k = Klass()

    consuming func consumeSelf() {}
    func doSomethingDefault() {}
    consuming func doSomethingConsuming() {}
    borrowing func doSomethingBorrowing() {}
    mutating func doSomethingMutating() {}

    consuming func testUseSelf() {
        _ = self
    }

    consuming func testUseSelf2() {
        self.doSomethingDefault()
        self.doSomethingDefault()
    }

    consuming func testUseSelf3() {
        self.doSomethingBorrowing()
        self.doSomethingDefault()
    }

    consuming func testUseSelf4() {
        self.doSomethingBorrowing()
        self.doSomethingBorrowing()
    }

    consuming func testLetUseSelf() {
        let _ = self
    }

    consuming func callDoSomethingDefault() {
        self.doSomethingDefault()
    }

    consuming func callDoSomethingBorrowing() {
        self.doSomethingBorrowing()
    }

    consuming func callDoSomethingConsuming() {
        self.doSomethingConsuming()
    }

    consuming func callDoSomethingMutating() {
        self.doSomethingMutating()
    }

    consuming func callDoSomethingMutating2() {
        self.doSomethingMutating()
        _ = consume self
    }

    consuming func callDoSomethingMutating3() {
        // expected-error @-1 {{'self' used after consume}}
        _ = consume self // expected-note {{consumed here}}
        self.doSomethingMutating() // expected-note {{used here}}
    }

    consuming func testConsumeOperatorSelf() {
        _ = consume self
    }

    consuming func testConsumeOperatorSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        _ = consume self // expected-note {{consumed here}}
        _ = consume self // expected-note {{consumed again here}}
    }

    consuming func testCopyOperatorSelf() {
        _ = copy self
    }

    consuming func testUseField() {
        _ = self.k
    }

    consuming func testConsumeField() {
        // No error, since our field is copyable.
        let _ = self.k
    }

    consuming func testCallConsumeMethod() {
        consumeSelf()
    }

    consuming func testCallConsumeMethod2() {
        // expected-error @-1 {{'self' consumed more than once}}
        consumeSelf() // expected-note {{consumed here}}
        consumeSelf() // expected-note {{consumed again here}}
    }

    consuming func testCallFreeFunctionConsumeSelf() {
        consumeLoadableSelfTest(self)
    }

    consuming func testCallFreeFunctionConsumeSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        consumeLoadableSelfTest(self) // expected-note {{consumed here}}
        consumeSelf() // expected-note {{consumed again here}}
    }

    consuming func testCallEscapingClosure() {
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
    }

    consuming func testCallEscapingClosure2() {
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
    }

    consuming func testCallEscapingClosure3() {
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
    }

    consuming func testCallNonEscapingClosure() {
        func f(_ x: () -> ()) {}
        f {
            _ = copy self
        }
    }

    consuming func testCallNonEscapingClosure2() {
        // expected-error @-1 {{'self' used after consume}}
        let _ = self // expected-note {{consumed here}}
        func f(_ x: () -> ()) {}
        f { // expected-note {{used here}}
            _ = copy self
        }
    }

    consuming func testCallNonEscapingClosure3() {
        let _ = self
        self = LoadableSelfTest()
        func f(_ x: () -> ()) {}
        f {
            _ = copy self
        }
    }
}

func consumeLoadableSelfTest(_ x: consuming LoadableSelfTest) {}

///////////////////////////////////
// MARK: Address Only Self Tests //
///////////////////////////////////

struct AddressOnlySelfTest<T> {
    var t: T

    consuming func consumeSelf() {}
    func doSomethingDefault() {}
    consuming func doSomethingConsuming() {}
    mutating func doSomethingMutating() {}
    borrowing func doSomethingBorrowing() {}

    consuming func testUseSelf() {
        // expected-error @-1 {{'self' consumed more than once}}
        _ = self // expected-note {{consumed here}}
        _ = self // expected-note {{consumed again here}}
    }

    consuming func testUseSelf2() {
        self.doSomethingDefault()
        self.doSomethingDefault()
    }

    consuming func testUseSelf3() {
        self.doSomethingBorrowing()
        self.doSomethingDefault()
    }

    consuming func testUseSelf4() {
        self.doSomethingDefault()
        self.doSomethingBorrowing()
    }

    consuming func testLetUseSelf() {
        // expected-error @-1 {{'self' consumed more than once}}
        let _ = self // expected-note {{consumed here}}
        let _ = self // expected-note {{consumed again here}}
    }

    consuming func callDoSomethingDefault() {
        self.doSomethingDefault()
        self.doSomethingBorrowing()
    }

    consuming func callDoSomethingConsuming() {
        self.doSomethingConsuming()
    }

    consuming func callDoSomethingConsuming2() {
        // expected-error @-1 {{'self' used after consume}}
        self.doSomethingConsuming() // expected-note {{consumed here}}
        self.doSomethingDefault() // expected-note {{used here}}
    }

    consuming func callDoSomethingConsuming3() {
        // expected-error @-1 {{'self' used after consume}}
        self.doSomethingConsuming() // expected-note {{consumed here}}
        self.doSomethingDefault() // expected-note {{used here}}
    }

    consuming func callDoSomethingConsuming4() {
        // expected-error @-1 {{'self' used after consume}}
        self.doSomethingConsuming() // expected-note {{consumed here}}
        self.doSomethingMutating() // expected-note {{used here}}
    }

    consuming func callDoSomethingConsuming4(_ x: AddressOnlySelfTest<T>) {
        self.doSomethingConsuming()
        self = x
        self.doSomethingDefault()
    }

    consuming func testConsumeOperatorSelf() {
        _ = consume self
    }

    consuming func testCopyOperatorSelf() {
        _ = copy self
    }

    consuming func testUseField() {
        _ = self.t
    }

    consuming func testConsumeField() {
        // No error, since our field is copyable.
        let _ = self.t
    }

    consuming func testCallConsumeMethod() {
        consumeSelf()
    }

    consuming func testCallFreeFunctionConsumeSelf() {
        consumeAddressOnlySelfTest(self)
    }

    consuming func testCallFreeFunctionConsumeSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        consumeAddressOnlySelfTest(self) // expected-note {{consumed here}}
        consumeSelf() // expected-note {{consumed again here}}
    }

    consuming func testCallEscapingClosure() {
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
    }

    consuming func testCallEscapingClosure2() {
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
    }

    consuming func testCallEscapingClosure3() {
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
    }

    consuming func testCallEscapingClosure4() {
        var f: () -> () = {}
        f = {
            let _ = copy self
        }
        f()
        f = {
            let _ = copy self
        }
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
    }

    consuming func testCallNonEscapingClosure() {
        func f(_ x: () -> ()) {}
        f {
            _ = copy self
        }
    }

    consuming func testCallNonEscapingClosure2() {
        // expected-error @-1 {{'self' used after consume}}
        let _ = self // expected-note {{consumed here}}
        func f(_ x: () -> ()) {}
        f { // expected-note {{used here}}
            _ = copy self
        }
        f {
            _ = copy self
        }
    }
}

func consumeAddressOnlySelfTest<T>(_ x: consuming AddressOnlySelfTest<T>) {}

//////////////////////////////
// MARK: Trivial Self Tests //
//////////////////////////////

struct TrivialSelfTest {
    var t = 5

    consuming func consumeSelf() {}
    func doSomethingDefault() {}
    borrowing func doSomethingBorrowing() {}
    consuming func doSomethingConsuming() {}
    mutating func doSomethingMutating() {}

    consuming func testUseSelf() {
        _ = self
    }

    consuming func testUseSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        _ = self // expected-note {{consumed here}}
        _ = self // expected-note {{consumed again here}}
    }

    consuming func testUseSelf3() {
        // expected-error @-1 {{'self' consumed more than once}}
        //
        // TODO: This is why we need to change SILFunctionType to contain
        // information about default conventions.
        self.doSomethingDefault() // expected-note {{consumed here}}
        self.doSomethingDefault() // expected-note {{consumed again here}}
    }

    consuming func testUseSelf4() {
        // expected-error @-1 {{'self' used after consume}}
        self.doSomethingDefault() // expected-note {{consumed here}}
        self.doSomethingBorrowing() // expected-note {{used here}}
    }

    consuming func testUseSelf5() {
        self.doSomethingBorrowing()
        self.doSomethingBorrowing()
    }

    consuming func testLetUseSelf() {
        let _ = self
    }

    consuming func callDoSomethingDefault() {
        self.doSomethingDefault()
    }

    consuming func callDoSomethingConsuming() {
        self.doSomethingConsuming()
    }

    consuming func testConsumeOperatorSelf() {
        _ = consume self
    }

    consuming func testConsumeOperatorSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        _ = consume self // expected-note {{consumed here}}
        _ = consume self // expected-note {{consumed again here}}
    }

    consuming func testCopyOperatorSelf() {
        _ = copy self
    }

    consuming func testUseField() {
        _ = self.t
    }

    consuming func testConsumeField() {
        // No error, since our field is copyable.
        let _ = self.t
    }

    consuming func testCallConsumeMethod() {
        consumeSelf()
    }

    consuming func testCallFreeFunctionConsumeSelf() {
        consumeTrivialSelfTest(self)
    }

    consuming func testCallFreeFunctionConsumeSelf2() {
        // expected-error @-1 {{'self' consumed more than once}}
        consumeTrivialSelfTest(self) // expected-note {{consumed here}}
        consumeSelf() // expected-note {{consumed again here}}
    }

    consuming func testCallEscapingClosure() {
        var f: () -> () = {}
        f = {
            let _ = self
        }
        f()
    }

    consuming func testCallEscapingClosure2() {
        let _ = self // expected-error {{noncopyable 'self' cannot be consumed when captured by an escaping closure}}
        var f: () -> () = {}
        f = {
            let _ = self
        }
        f()
    }

    consuming func testCallNonEscapingClosure() {
        func f(_ x: () -> ()) {}
        f {
            _ = self
        }
    }

    consuming func testCallNonEscapingClosure2() {
        // expected-error @-1 {{'self' used after consume}}
        let _ = self // expected-note {{consumed here}}
        func f(_ x: () -> ()) {}
        f { // expected-note {{used here}}
            _ = self
        }
    }

    consuming func testCallNonEscapingClosure3() {
        let _ = self
        self = TrivialSelfTest()
        func f(_ x: () -> ()) {}
        f {
            _ = self
        }
    }
}

func consumeTrivialSelfTest(_ x: consuming TrivialSelfTest) {}