-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathomit_return_ifdecl.swift
2526 lines (2103 loc) · 41.2 KB
/
omit_return_ifdecl.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
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
1000
// RUN: %target-swift-frontend %s -typecheck -verify
// MARK: - Helpers
@discardableResult
func logAndReturn<T : CustomStringConvertible>(_ t: T) -> String {
let log = "\(t)"
print(log)
return log
}
@discardableResult
func failableLogAndReturn<T : CustomStringConvertible>(_ t: T) throws -> String {
let log = "\(t)"
print(log)
return log
}
typealias MyOwnVoid = ()
func failableIdentity<T>(_ t: T) throws -> T {
#if true
t
#endif
}
enum MyOwnNever {}
func myOwnFatalError() -> MyOwnNever { fatalError() }
struct MyOwnInt : ExpressibleByIntegerLiteral { init(integerLiteral: Int) {} }
struct MyOwnFloat : ExpressibleByFloatLiteral { init(floatLiteral: Double) {} }
struct MyOwnBoolean : ExpressibleByBooleanLiteral { init(booleanLiteral: Bool) {} }
struct MyOwnString : ExpressibleByStringLiteral { init(stringLiteral: String) {} }
struct MyOwnInterpolation : ExpressibleByStringInterpolation { init(stringLiteral: String) {} }
enum Unit {
case only
}
struct Initable {}
struct StructWithProperty {
var foo: Int
}
@dynamicMemberLookup
struct DynamicStruct {
subscript(dynamicMember input: String) -> String { return input }
}
struct MyOwnArray<Element> : ExpressibleByArrayLiteral {
init(arrayLiteral elements: Element...) {}
}
struct MyOwnDictionary<Key, Value> : ExpressibleByDictionaryLiteral {
init(dictionaryLiteral elements: (Key, Value)...) {}
}
struct SubscriptableStruct {
subscript(int: Int) -> Int {
#if true
int
#endif
}
}
extension Int {
var zero: Int {
#if true
0
#endif
}
}
extension Optional where Wrapped == Int {
var someZero: Int? {
#if true
Int?.some(0)
#endif
}
}
protocol SomeProto {
func foo() -> String
}
struct SomeProtoConformer : SomeProto {
func foo() -> String { "howdy" }
}
class Base {}
class Derived : Base {}
extension Int {
init() { self = 0 }
}
// MARK: - Notable Free Functions
func identity<T>(_ t: T) -> T {
#if true
t
#endif
}
internal func _fatalErrorFlags() -> UInt32 {
#if true
return 0
#endif
}
internal func _assertionFailure(
_ prefix: StaticString, _ message: String,
flags: UInt32
) -> Never {
#if true
fatalError()
#endif
}
internal func _diagnoseUnexpectedEnumCaseValue<SwitchedValue, RawValue>(
type: SwitchedValue.Type,
rawValue: RawValue
) -> Never {
#if true
_assertionFailure("Fatal error",
"unexpected enum case '\(type)(rawValue: \(rawValue))'",
flags: _fatalErrorFlags())
#endif
}
// MARK: - Free Functions
func ff_nop() {
#if true
#endif
}
func ff_nop_false() {
#if false
#endif
}
func ff_missing() -> String {
#if true
#endif
}
func ff_implicit() -> String {
#if true
"hello"
#endif
}
func ff_explicit() -> String {
#if true
return "hello"
#endif
}
func ff_explicitClosure() -> () -> Void {
#if true
return { print("howdy") }
#endif
}
func ff_implicitClosure() -> () -> Void {
#if true
{ print("howdy") }
#endif
}
func ff_explicitMultilineClosure() -> () -> String {
#if true
return {
let one = "big a"
let two = "little a"
return "\(one) + \(two)"
}
#endif
}
func ff_implicitMultilineClosure() -> () -> String {
#if true
{
let one = "big a"
let two = "little a"
return "\(one) + \(two)"
}
#endif
}
func ff_implicitWrong() -> String {
#if true
17 // expected-error {{cannot convert return expression of type 'Int' to return type 'String'}}
#endif
}
func ff_explicitWrong() -> String {
#if true
return 17 // expected-error {{cannot convert return expression of type 'Int' to return type 'String'}}
#endif
}
func ff_implicitMulti() -> String {
#if true
print("uh oh")
"shucks howdy" // expected-warning {{string literal is unused}}
#endif
}
func ff_explicitMulti() -> String {
#if true
print("okay")
return "all right"
#endif
}
func ff_effectfulUsed() -> String {
#if true
logAndReturn("okay")
#endif
}
// Unused Returns
func ff_effectfulIgnored() {
#if true
logAndReturn("okay")
#endif
}
func ff_effectfulIgnoredExplicitReturnTypeVoid() -> Void {
#if true
logAndReturn("okay")
#endif
}
func ff_effectfulIgnoredExplicitReturnTypeSwiftVoid() -> Swift.Void {
#if true
logAndReturn("okay")
#endif
}
func ff_effectfulIgnoredExplicitReturnTypeMyVoidTypealias() -> MyOwnVoid {
#if true
logAndReturn("okay")
#endif
}
func ff_effectfulIgnoredExplicitReturnTypeEmptyTuple() -> () {
#if true
logAndReturn("okay")
#endif
}
// Stubs
func ff_stubImplicitReturn() {
#if true
fatalError()
#endif
}
func ff_stubExplicitReturnTypeVoid() -> Void {
#if true
fatalError()
#endif
}
func ff_stubExplicitReturnTypeSwiftVoid() -> Swift.Void {
#if true
fatalError()
#endif
}
func ff_stubExplicitReturnTypeMyVoidTypealias() -> MyOwnVoid {
#if true
fatalError()
#endif
}
func ff_stubExplicitReturnTypeEmptyTuple() -> () {
#if true
fatalError()
#endif
}
func ff_stubImplicitReturnNever() -> Never {
#if true
fatalError()
#endif
}
func ff_stubExplicitReturnNever() -> Never {
#if true
return fatalError()
#endif
}
func ff_stubExplicitReturnNeverAsMyOwnNever() -> MyOwnNever {
#if true
return fatalError() // expected-error {{cannot convert return expression of type 'Never' to return type 'MyOwnNever'}}
#endif
}
func ff_stubExplicitReturnMyOwnNeverAsNever() -> Never {
#if true
return myOwnFatalError() // expected-error {{cannot convert return expression of type 'MyOwnNever' to return type 'Never'}}
#endif
}
func ff_stubImplicitReturnNeverAsMyOwnNever() -> MyOwnNever {
#if true
fatalError()
#endif
}
func ff_stubImplicitReturnMyOwnNeverAsNever() -> Never {
#if true
myOwnFatalError()
#endif
}
func ff_stubReturnString() -> String {
#if true
fatalError()
#endif
}
func ff_stubReturnGeneric<T>() -> T {
#if true
fatalError()
#endif
}
// Trying
func ff_tryExplicit() throws -> String {
#if true
return try failableIdentity("shucks")
#endif
}
func ff_tryImplicit() throws -> String {
#if true
try failableIdentity("howdy")
#endif
}
func ff_tryExplicitMissingThrows() -> String {
#if true
return try failableIdentity("shucks") // expected-error {{errors thrown from here are not handled}}
#endif
}
func ff_tryImplicitMissingThrows() -> String {
#if true
try failableIdentity("howdy") // expected-error {{errors thrown from here are not handled}}
#endif
}
// Forced Trying
func ff_forceTryExplicit() -> String {
#if true
return try! failableIdentity("howdy")
#endif
}
func ff_forceTryImplicit() -> String {
#if true
try! failableIdentity("shucks")
#endif
}
func ff_forceTryExplicitAddingThrows() throws -> String {
#if true
return try! failableIdentity("howdy")
#endif
}
func ff_forceTryImplicitAddingThrows() throws -> String {
#if true
try! failableIdentity("shucks")
#endif
}
// Optional Trying
func ff_optionalTryExplicit() -> String? {
#if true
return try? failableIdentity("howdy")
#endif
}
func ff_optionalTryImplicit() -> String? {
#if true
try? failableIdentity("shucks")
#endif
}
func ff_optionalTryExplicitAddingThrows() throws -> String? {
#if true
return try? failableIdentity("shucks")
#endif
}
func ff_optionalTryImplicitAddingThrows() throws -> String? {
#if true
try? failableIdentity("howdy")
#endif
}
// Inferred Return Types
func ff_inferredIntegerLiteralInt() -> Int {
#if true
0
#endif
}
func ff_inferredIntegerLiteralInt8() -> Int8 {
#if true
0
#endif
}
func ff_inferredIntegerLiteralInt16() -> Int16 {
#if true
0
#endif
}
func ff_inferredIntegerLiteralInt32() -> Int32 {
#if true
0
#endif
}
func ff_inferredIntegerLiteralInt64() -> Int64 {
#if true
0
#endif
}
func ff_inferredIntegerLiteralMyOwnInt() -> MyOwnInt {
#if true
0
#endif
}
func ff_nilLiteralInt() -> Int? {
#if true
nil
#endif
}
func ff_inferredFloatLiteralFloat() -> Float {
#if true
0.0
#endif
}
func ff_inferredFloatLiteralDouble() -> Double {
#if true
0.0
#endif
}
func ff_inferredFloatLiteralMyOwnDouble() -> MyOwnFloat {
#if true
0.0
#endif
}
func ff_inferredBooleanLiteralBool() -> Bool {
#if true
true
#endif
}
func ff_inferredBooleanLiteralMyOwnBoolean() -> MyOwnBoolean {
#if true
true
#endif
}
func ff_inferredStringLiteralString() -> String {
#if true
"howdy"
#endif
}
func ff_inferredStringLiteralMyOwnString() -> MyOwnString {
#if true
"howdy"
#endif
}
func ff_inferredInterpolatedStringLiteralString() -> String {
#if true
"\(0) \(1)"
#endif
}
func ff_inferredInterpolatedStringLiteralString() -> MyOwnInterpolation {
#if true
"\(0) \(1)"
#endif
}
func ff_inferredMagicFile() -> StaticString {
#if true
#file
#endif
}
func ff_inferredMagicLine() -> UInt {
#if true
#line // expected-error {{#line directive was renamed to #sourceLocation}}
#endif // expected-error {{parameterless closing #sourceLocation() directive without prior opening #sourceLocation(file:,line:) directive}}
}
func ff_inferredMagicColumn() -> UInt {
#if true
#column
#endif
}
func ff_inferredMagicFunction() -> StaticString {
#if true
#function
#endif
}
func ff_inferredMagicDSOHandle() -> UnsafeRawPointer {
#if true
#dsohandle
#endif
}
func ff_implicitDiscardExpr() {
#if true
_ = 3
#endif
}
func ff_implicitMetatype() -> String.Type {
#if true
String.self
#endif
}
func ff_implicitMemberRef(_ instance: StructWithProperty) -> Int {
#if true
instance.foo
#endif
}
func ff_implicitDynamicMember(_ s: DynamicStruct) -> String {
#if true
s.foo
#endif
}
func ff_implicitParenExpr() -> Int {
#if true
(3 + 5)
#endif
}
func ff_implicitTupleExpr() -> (Int, Int) {
#if true
(3, 5)
#endif
}
func ff_implicitArrayExprArray() -> [Int] {
#if true
[1, 3, 5]
#endif
}
func ff_implicitArrayExprSet() -> Set<Int> {
#if true
[1, 3, 5]
#endif
}
func ff_implicitArrayExprMyOwnArray() -> MyOwnArray<Int> {
#if true
[1, 3, 5]
#endif
}
func ff_implicitDictionaryExprDictionary() -> [Int : Int] {
#if true
[1 : 1, 2 : 2]
#endif
}
func ff_implicitDictionaryExprMyOwnDictionary() -> MyOwnDictionary<Int, Int> {
#if true
[1 : 1, 2 : 2]
#endif
}
func ff_implicitSubscriptExpr(_ s: SubscriptableStruct) -> Int {
#if true
s[13]
#endif
}
func ff_implicitKeyPathExprWritableKeyPath() -> WritableKeyPath<Int, Int> {
#if true
\Int.self
#endif
}
func ff_implicitKeyPathExprKeyPath() -> WritableKeyPath<Int, Int> {
#if true
\Int.self.self
#endif
}
func ff_implicitTupleElementExpr() -> Int {
#if true
(1,field:2).field
#endif
}
func ff_implicitBindExpr(_ opt: Int?) -> Int? {
#if true
opt?.zero
#endif
}
func ff_implicitOptionalEvaluation(_ opt: Int?) -> Int? {
#if true
(opt?.zero.zero).someZero
#endif
}
func ff_implicitForceValue(_ opt: Int?) -> Int {
#if true
opt!
#endif
}
func ff_implicitTemporarilyEscapableExpr(_ cl: () -> Void) -> () -> Void {
#if true
withoutActuallyEscaping(cl) { $0 }
#endif
}
func ff_implicitOpenExistentialExpr(_ f: SomeProto) -> String {
#if true
f.foo()
#endif
}
func ff_implicitInjectIntoOptionalExpr(_ int: Int) -> Int? {
#if true
int
#endif
}
func ff_implicitTupleShuffle(_ input: (one: Int, two: Int)) -> (two: Int, one: Int) {
#if true
input // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
#endif
}
func ff_implicitCollectionUpcast(_ derived: [Derived]) -> [Base] {
#if true
derived
#endif
}
func ff_implicitErasureExpr(_ conformer: SomeProtoConformer) -> SomeProto {
#if true
conformer
#endif
}
func ff_implicitAnyHashableErasureExpr(_ int: Int) -> AnyHashable {
#if true
int
#endif
}
func ff_implicitCallExpr<Input, Output>(input: Input, function: (Input) -> Output) -> Output {
#if true
function(input)
#endif
}
func ff_implicitPrefixUnaryOperator(int: Int) -> Int {
#if true
-int
#endif
}
func ff_implicitBinaryOperator(lhs: Int, rhs: Int) -> Int {
#if true
lhs - rhs
#endif
}
func ff_implicitConstructorCallRefExpr(lhs: Int, rhs: Int) -> Int {
#if true
Int()
#endif
}
func ff_implicitIsExpr<T>(t: T) -> Bool {
#if true
t is Int
#endif
}
func ff_implicitCoerceExpr<T>() -> T.Type {
#if true
T.self as T.Type
#endif
}
func ff_implicitConditionalCheckedCastExprAs<T>(t: T) -> Int? {
#if true
t as? Int
#endif
}
func ff_implicitForceCheckedCastExpr<T>(t: T) -> Int {
#if true
t as! Int
#endif
}
func ff_conditional(_ condition: Bool) -> Int {
#if true
condition ? 1 : -1
#endif
}
var __ff_implicitAssignExpr: Int = 0
func ff_implicitAssignExpr(newValue: Int) -> Void {
#if true
__ff_implicitAssignExpr = newValue
#endif
}
func ff_implicitMemberAccessInit() -> Initable {
#if true
Initable.init()
#endif
}
func ff_implicitMemberAccessEnumCase() -> Unit {
#if true
Unit.only
#endif
}
// MARK: - Free Properties : Implicit Get
var fv_nop: () {
#if true
#endif
}
var fv_nop_false: () {
#if false
#endif
}
var fv_missing: String {
#if true
#endif
}
var fv_missing_test: String {
get {}
}
var fv_implicit: String {
#if true
"hello"
#endif
}
var fv_explicit: String {
#if true
return "hello"
#endif
}
var fv_explicitClosure: () -> Void {
#if true
return { print("howdy") }
#endif
}
var fv_implicitClosure: () -> Void {
#if true
{ print("howdy") }
#endif
}
var fv_explicitMultilineClosure: () -> String {
#if true
return {
let one = "big a"
let two = "little a"
return "\(one) + \(two)"
}
#endif
}
var fv_implicitMultilineClosure: () -> String {
#if true
{
let one = "big a"
let two = "little a"
return "\(one) + \(two)"
}
#endif
}
var fv_implicitWrong: String {
#if true
17 // expected-error {{cannot convert return expression of type 'Int' to return type 'String'}}
#endif
}
var fv_explicitWrong: String {
#if true
return 17 // expected-error {{cannot convert return expression of type 'Int' to return type 'String'}}
#endif
}
var fv_implicitMulti: String {
#if true
print("uh oh")
"shucks howdy" // expected-warning {{string literal is unused}}
#endif
}
var fv_explicitMulti: String {
#if true
print("okay")
return "all right"
#endif
}
var fv_effectfulUsed: String {
#if true
logAndReturn("okay")
#endif
}
// Unused returns
var fv_effectfulIgnored: () {
#if true
logAndReturn("okay")
#endif
}
var fv_effectfulIgnoredVoid: Void {
#if true
logAndReturn("okay")
#endif
}
var fv_effectfulIgnoredSwiftVoid: Swift.Void {
#if true
logAndReturn("okay")
#endif
}
// Stubs
var fv_stubEmptyTuple: () {
#if true
fatalError()
#endif
}
var fv_stubVoid: Void {
#if true
fatalError()
#endif
}
var fv_stubSwiftVoid: Swift.Void {
#if true
fatalError()
#endif
}
var fv_stubMyVoidTypealias: MyOwnVoid {
#if true
fatalError()
#endif
}
var fv_stubImplicitReturnNever: Never {
#if true
fatalError()
#endif
}
var fv_stubExplicitReturnNever: Never {
#if true
return fatalError()
#endif
}
var fv_stubExplicitReturnNeverAsMyOwnNever: MyOwnNever {
#if true
return fatalError() // expected-error {{cannot convert return expression of type 'Never' to return type 'MyOwnNever'}}
#endif
}
var fv_stubExplicitReturnMyOwnNeverAsNever: Never {
#if true
return myOwnFatalError() // expected-error {{cannot convert return expression of type 'MyOwnNever' to return type 'Never'}}
#endif
}
var fv_stubImplicitReturnNeverAsMyOwnNever: MyOwnNever {
#if true
fatalError()
#endif
}
var fv_stubImplicitReturnMyOwnNeverAsNever: Never {
#if true
myOwnFatalError()
#endif
}
var fv_stubString: String {
#if true
fatalError()
#endif
}
// Forced Trying
var fv_forceTryUnusedExplicit: () {
#if true
return try! failableLogAndReturn("oh") //expected-error {{unexpected non-void return value in void function}}
#endif
}
var fv_forceTryUnusedImplicit: () {
#if true
try! failableLogAndReturn("uh")
#endif
}
var fv_forceTryExplicit: String {
#if true
return try! failableIdentity("shucks")
#endif
}
var fv_forceTryImplicit: String {
#if true
try! failableIdentity("howdy")
#endif
}
// Optional Trying
var fv_optionalTryUnusedExplicit: () {
#if true
return try? failableLogAndReturn("uh") //expected-error {{unexpected non-void return value in void function}}
#endif
}
var fv_optionalTryUnusedImplicit: () {
#if true
try? failableLogAndReturn("oh") //expected-warning {{result of 'try?' is unused}}