forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_folding.swift
875 lines (696 loc) · 20.4 KB
/
cast_folding.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
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
// RUN: %target-swift-frontend -O -emit-sil %s | FileCheck %s
// We want to check two things here:
// - Correctness
// - That certain "is" checks are eliminated based on static analysis at compile-time
//
// In ideal world, all those testNN functions should be simplified down to a single basic block
// which returns either true or false, i.e. all type checks should folded statically.
protocol P {}
protocol Q: P {}
class A:P {}
class X {}
class B:P {}
private struct S:P {}
struct T:Q {}
private struct U {}
public protocol CP1: class {}
public protocol CP2: class {}
// Class D implements only one of class protocols
// and it cannot be extended elsewhere as it is private
private class D: CP1 {}
private final class F: CP1 {}
// Class E implements both class protocols at once
class E: CP1, CP2 {}
func cast0<T>(a:T) -> Bool {
// Succeeds if T is A
return A().dynamicType is T.Type
}
func cast1<T>(a:T) -> Bool {
// Succeeds if T is A
return (A() as AnyObject).dynamicType is T.Type
}
func cast2<T>(a:T) -> Bool {
// Succeeds if T is A
let ao:AnyObject = A() as AnyObject
return ao.dynamicType is T.Type
}
func cast3(p:AnyObject) -> Bool {
// Always fails
return p.dynamicType is AnyObject.Protocol
}
func cast4(p:AnyObject) -> Bool {
return p.dynamicType is A.Type
}
func cast5(t:AnyObject.Type) -> Bool {
// Succeeds if t is B.self
return t is B.Type
}
func cast6<T>(t:T) -> Bool {
// Always fails
return AnyObject.self is T.Type
}
func cast7<T>(t:T.Type) -> Bool {
// Succeeds if t is AnyObject.self
return t is AnyObject.Protocol
}
func cast8<T>(a:T) -> Bool {
// Succeeds if T is A
return (A() as P).dynamicType is T.Type
}
func cast9<T>(a:T) -> Bool {
// Succeeds if T is A
let ao:P = A() as P
return ao.dynamicType is T.Type
}
func cast10(p:P) -> Bool {
return p.dynamicType is P.Protocol
}
func cast11(p:P) -> Bool {
// Succeeds if p is of type A
return p.dynamicType is A.Type
}
func cast12(t:P.Type) -> Bool {
return t is B.Type
}
func cast13<T>(t:T) -> Bool {
// Succeeds if T is P
return P.self is T.Type
}
func cast14<T>(t:T.Type) -> Bool {
// Succeeds if p is P.self
return t is P.Protocol
}
func cast15<T>(t:T) -> Bool {
// Succeeds if T is P
return P.self is T.Type
}
func cast16<T>(t:T) -> Bool {
// Succeeds if T is P
return T.self is P.Protocol
}
func cast17<T>(t:T) -> Bool {
// Succeeds if T is AnyObject
return AnyObject.self is T.Type
}
func cast18<T>(t:T) -> Bool {
// Succeeds if T is AnyObject
return T.self is AnyObject.Protocol
}
func cast20<T>(t:T) -> Bool {
// Succeeds if T is P
return T.self is P.Type
}
func cast21<T>(t:T) -> Bool {
// Succeeds if T is P
return T.self is P.Protocol
}
func cast22(existential: P.Type) -> Bool {
// Succeeds if existential is S.self
return existential is S.Type
}
func cast23(concrete: P.Protocol) -> Bool {
// Always fails
return concrete is S.Type
}
func cast24(existential: P.Type) -> Bool {
// Succeeds if existential is Q.self
return existential is Q.Type
}
func cast25(concrete: P.Protocol) -> Bool {
// Always fails, because P != Q
return concrete is Q.Type
}
func cast26(existential: Q.Type) -> Bool {
// Succeeds always, because Q:P
return existential is P.Type
}
func cast27(existential: CP1.Type) -> Bool {
// Fails always, because existential is a class
// and it cannot be struct
return existential is S.Type
}
func cast28(existential: CP1.Type) -> Bool {
// Succeeds if existential conforms to CP1 and CP2
return existential is CP2.Type
}
func cast29(o: Any) -> Bool {
// Succeeds if o is P.Type
return o is P.Type
}
func cast30(o: AnyObject) -> Bool {
// Succeeds if o is P.Type
return o is P.Type
}
func cast32(p: P) -> Bool {
// Fails always, because a metatype cannot conform to a protocol
return p is P.Type
}
func cast33(p: P) -> Bool {
// Same as above, but non-existential metatype
return p is X.Type
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test0FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test0() -> Bool {
return cast0(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test1() -> Bool {
return cast1(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test2() -> Bool {
return cast2(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test3FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test3() -> Bool {
return cast3(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test4FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test4() -> Bool {
return cast4(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test5_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test5_1() -> Bool {
return cast5(B.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test5_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test5_2() -> Bool {
return cast5(AnyObject.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test6_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test6_1() -> Bool {
return cast6(B.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test6_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test6_2() -> Bool {
return cast6(AnyObject.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test7_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test7_1() -> Bool {
return cast7(B.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding7test7_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test7_2() -> Bool {
return cast7(AnyObject.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test8FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test8() -> Bool {
return cast8(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding5test9FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test9() -> Bool {
return cast9(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test10FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test10() -> Bool {
return cast10(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test11FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test11() -> Bool {
return cast11(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test12FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test12() -> Bool {
return cast12(A.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test13_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test13_1() -> Bool {
return cast13(A.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test13_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test13_2() -> Bool {
return cast13(P.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test13_3FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test13_3() -> Bool {
return cast13(A() as P)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test14_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test14_1() -> Bool {
return cast14(A.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test14_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test14_2() -> Bool {
return cast14(P.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test15_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test15_1() -> Bool {
return cast15(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test15_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test15_2() -> Bool {
return cast15(A() as P)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test16_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test16_1() -> Bool {
return cast16(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test16_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test16_2() -> Bool {
return cast16(A() as P)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test17_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test17_1() -> Bool {
return cast17(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test17_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test17_2() -> Bool {
return cast17(A() as AnyObject)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test18_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test18_1() -> Bool {
return cast18(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test18_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test18_2() -> Bool {
return cast18(A() as AnyObject)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test19FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test19() -> Bool {
let t: Any.Type = (1 as Any).dynamicType
return t is Int.Type
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test20_1FT_Sb : $@convention(thin) () -> Bool
@inline(never)
func test20_1() -> Bool {
return cast20(S.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test20_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test20_2() -> Bool {
return cast20(U())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test21_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test21_1() -> Bool {
return cast21(S.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test21_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test21_2() -> Bool {
return cast21(A() as P)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test22_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test22_1() -> Bool {
return cast22(T.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test22_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test22_2() -> Bool {
return cast22(S.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test23FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test23() -> Bool {
return cast23(P.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test24_1FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test24_1() -> Bool {
return cast24(T.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test24_2FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test24_2() -> Bool {
return cast24(S.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test25FT_Sb : $@convention(thin) () -> Bool
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test25() -> Bool {
return cast25(P.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test26FT_Sb
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test26() -> Bool {
return cast26(T.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test27FT_Sb
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test27() -> Bool {
return cast27(D.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test28_1FT_Sb
// CHECK: checked_cast
// CHECK: return
@inline(never)
func test28_1() -> Bool {
return cast28(D.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test28_2FT_Sb
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, -1
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test28_2() -> Bool {
return cast28(E.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding8test28_3FT_Sb
// CHECK: bb0
// CHECK-NEXT: %0 = integer_literal $Builtin.Int1, 0
// CHECK-NEXT: %1 = struct $Bool
// CHECK-NEXT: return %1
@inline(never)
func test28_3() -> Bool {
return cast28(F.self)
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test29FT_Sb
// CHECK: bb0
// CHECK: checked_cast
// CHECK: return
@inline(never)
func test29() -> Bool {
return cast29(X())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test30FT_Sb
// CHECK: bb0
// CHECK: checked_cast
// CHECK: return
@inline(never)
func test30() -> Bool {
return cast30(X())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test32FT_Sb
// CHECK: bb0
// CHECK: checked_cast
// CHECK: return
@inline(never)
func test32() -> Bool {
// We don't actually fold this right now, but at least make sure it
// doesn't crash
return cast32(A())
}
// CHECK-LABEL: sil hidden [noinline] @_TF12cast_folding6test33FT_Sb
// CHECK: bb0
// CHECK: checked_cast
// CHECK: return
@inline(never)
func test33() -> Bool {
// Ditto...
return cast33(A())
}
protocol PP {
func foo() -> Int
}
public class CC : PP {
func foo() -> Int { return 0 }
}
public class DD : PP {
func foo() -> Int { return 1 }
}
// Check that the body of the function
// contains a trap followed by unreachable
// and no code afterwards.
// CHECK-LABEL: sil @_TF12cast_folding7getAsDDFCS_2CCCS_2DD
// CHECK: builtin "int_trap"
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
public func getAsDD(c: CC) -> DD {
return c as! DD
}
// Check that the body of the function
// contains a trap followed by unreachable
// and no code afterwards.
// CHECK-LABEL: sil @_TF12cast_folding7callFooFCS_2CCSi
// CHECK: builtin "int_trap"
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
public func callFoo(c: CC) -> Int {
return (c as! DD).foo()
}
@inline(never)
func callFooGeneric<T : PP>(c: T) -> Int {
return (c as! DD).foo()
}
// Check that the inlined version of callFooGeneric contains only a trap
// followed by unreachable and no code afterwards
// CHECK-LABEL: sil [noinline] @_TF12cast_folding16callForGenericCCFCS_2CCT_
// CHECK: builtin "int_trap"
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
@inline(never)
public func callForGenericCC(c: CC) {
callFoo(c)
}
// Check that this conversion does not crash a compiler.
@inline(never)
public func test34() {
if let _ = A.self as? P {
print("A: dice")
} else {
print("A: no dice")
}
}
// Check that this conversion does not crash a compiler.
@inline(never)
public func test35() {
if let _ = X.self as? P {
print("X: dice")
} else {
print("X: no dice")
}
}
print("test0=\(test0())")
print("test1=\(test1())")
print("test2=\(test2())")
print("test3=\(test3())")
print("test4=\(test4())")
print("test5_1=\(test5_1())")
print("test5_2=\(test5_2())")
print("test6_1=\(test6_1())")
print("test6_2=\(test6_2())")
print("test7_1=\(test7_1())")
print("test7_2=\(test7_2())")
print("test8=\(test8())")
print("test9=\(test9())")
print("test10=\(test10())")
print("test11=\(test11())")
print("test12=\(test12())")
print("test13_1=\(test13_1())")
print("test13_2=\(test13_2())")
print("test13_3=\(test13_3())")
print("test14_1=\(test14_1())")
print("test14_2=\(test14_2())")
print("test15_1=\(test15_1())")
print("test15_2=\(test15_2())")
print("test16_1=\(test16_1())")
print("test16_2=\(test16_2())")
print("test17_1=\(test17_1())")
print("test17_2=\(test17_2())")
print("test18_1=\(test18_1())")
print("test18_2=\(test18_2())")
print("test19=\(test19())")
print("test20_1=\(test20_1())")
print("test20_2=\(test20_2())")
print("test21_1=\(test21_1())")
print("test21_2=\(test21_2())")
print("test22_1=\(test22_1())")
print("test22_2=\(test22_2())")
print("test23=\(test23())")
print("test24_1=\(test24_1())")
print("test24_2=\(test24_2())")
print("test25=\(test25())")
print("test26=\(test26())")
print("test27=\(test27())")
print("test28_1=\(test28_1())")
print("test28_2=\(test28_2())")
print("test28_3=\(test28_3))")
print("test29=\(test29())")
print("test30=\(test30())")
print("test32=\(test32())")
print("test33=\(test33())")