1 | // Copyright 2020 The Go Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE file. |
4 | |
5 | package unreachable |
6 | |
7 | // This file produces masses of errors from the type checker due to |
8 | // missing returns statements and other things. |
9 | |
10 | type T int |
11 | |
12 | var x interface{} |
13 | var c chan int |
14 | |
15 | func external() int // ok |
16 | |
17 | func _() int { |
18 | } |
19 | |
20 | func _() int { |
21 | print(1) |
22 | } |
23 | |
24 | func _() int { |
25 | print(1) |
26 | return 2 |
27 | // want "unreachable code" |
28 | } |
29 | |
30 | func _() int { |
31 | L: |
32 | print(1) |
33 | goto L |
34 | // want "unreachable code" |
35 | } |
36 | |
37 | func _() int { |
38 | print(1) |
39 | panic(2) |
40 | // want "unreachable code" |
41 | } |
42 | |
43 | // but only builtin panic |
44 | func _() int { |
45 | var panic = func(int) {} |
46 | print(1) |
47 | panic(2) |
48 | println() // ok |
49 | } |
50 | |
51 | func _() int { |
52 | { |
53 | print(1) |
54 | return 2 |
55 | // want "unreachable code" |
56 | } |
57 | println() // ok |
58 | } |
59 | |
60 | func _() int { |
61 | { |
62 | print(1) |
63 | return 2 |
64 | } |
65 | // want "unreachable code" |
66 | } |
67 | |
68 | func _() int { |
69 | L: |
70 | { |
71 | print(1) |
72 | goto L |
73 | // want "unreachable code" |
74 | } |
75 | println() // ok |
76 | } |
77 | |
78 | func _() int { |
79 | L: |
80 | { |
81 | print(1) |
82 | goto L |
83 | } |
84 | // want "unreachable code" |
85 | } |
86 | |
87 | func _() int { |
88 | print(1) |
89 | { |
90 | panic(2) |
91 | } |
92 | } |
93 | |
94 | func _() int { |
95 | print(1) |
96 | { |
97 | panic(2) |
98 | // want "unreachable code" |
99 | } |
100 | } |
101 | |
102 | func _() int { |
103 | print(1) |
104 | { |
105 | panic(2) |
106 | } |
107 | // want "unreachable code" |
108 | } |
109 | |
110 | func _() int { |
111 | print(1) |
112 | return 2 |
113 | |
114 | } |
115 | |
116 | func _() int { |
117 | L: |
118 | print(1) |
119 | goto L |
120 | |
121 | } |
122 | |
123 | func _() int { |
124 | print(1) |
125 | panic(2) |
126 | |
127 | } |
128 | |
129 | func _() int { |
130 | { |
131 | print(1) |
132 | return 2 |
133 | |
134 | } |
135 | } |
136 | |
137 | func _() int { |
138 | L: |
139 | { |
140 | print(1) |
141 | goto L |
142 | |
143 | } |
144 | } |
145 | |
146 | func _() int { |
147 | print(1) |
148 | { |
149 | panic(2) |
150 | |
151 | } |
152 | } |
153 | |
154 | func _() int { |
155 | { |
156 | print(1) |
157 | return 2 |
158 | } |
159 | |
160 | } |
161 | |
162 | func _() int { |
163 | L: |
164 | { |
165 | print(1) |
166 | goto L |
167 | } |
168 | |
169 | } |
170 | |
171 | func _() int { |
172 | print(1) |
173 | { |
174 | panic(2) |
175 | } |
176 | |
177 | } |
178 | |
179 | func _() int { |
180 | print(1) |
181 | if x == nil { |
182 | panic(2) |
183 | } else { |
184 | panic(3) |
185 | } |
186 | // want "unreachable code" |
187 | } |
188 | |
189 | func _() int { |
190 | L: |
191 | print(1) |
192 | if x == nil { |
193 | panic(2) |
194 | } else { |
195 | goto L |
196 | } |
197 | // want "unreachable code" |
198 | } |
199 | |
200 | func _() int { |
201 | L: |
202 | print(1) |
203 | if x == nil { |
204 | panic(2) |
205 | } else if x == 1 { |
206 | return 0 |
207 | } else if x != 2 { |
208 | panic(3) |
209 | } else { |
210 | goto L |
211 | } |
212 | // want "unreachable code" |
213 | } |
214 | |
215 | // if-else chain missing final else is not okay, even if the |
216 | // conditions cover every possible case. |
217 | |
218 | func _() int { |
219 | print(1) |
220 | if x == nil { |
221 | panic(2) |
222 | } else if x != nil { |
223 | panic(3) |
224 | } |
225 | println() // ok |
226 | } |
227 | |
228 | func _() int { |
229 | print(1) |
230 | if x == nil { |
231 | panic(2) |
232 | } |
233 | println() // ok |
234 | } |
235 | |
236 | func _() int { |
237 | L: |
238 | print(1) |
239 | if x == nil { |
240 | panic(2) |
241 | } else if x == 1 { |
242 | return 0 |
243 | } else if x != 1 { |
244 | panic(3) |
245 | } |
246 | println() // ok |
247 | } |
248 | |
249 | func _() int { |
250 | print(1) |
251 | for { |
252 | } |
253 | // want "unreachable code" |
254 | } |
255 | |
256 | func _() int { |
257 | for { |
258 | for { |
259 | break |
260 | } |
261 | } |
262 | // want "unreachable code" |
263 | } |
264 | |
265 | func _() int { |
266 | for { |
267 | for { |
268 | break |
269 | // want "unreachable code" |
270 | } |
271 | } |
272 | } |
273 | |
274 | func _() int { |
275 | for { |
276 | for { |
277 | continue |
278 | // want "unreachable code" |
279 | } |
280 | } |
281 | } |
282 | |
283 | func _() int { |
284 | for { |
285 | L: |
286 | for { |
287 | break L |
288 | } |
289 | } |
290 | // want "unreachable code" |
291 | } |
292 | |
293 | func _() int { |
294 | print(1) |
295 | for { |
296 | break |
297 | } |
298 | println() // ok |
299 | } |
300 | |
301 | func _() int { |
302 | for { |
303 | for { |
304 | } |
305 | // want "unreachable code" |
306 | } |
307 | println() // ok |
308 | } |
309 | |
310 | func _() int { |
311 | L: |
312 | for { |
313 | for { |
314 | break L |
315 | } |
316 | } |
317 | println() // ok |
318 | } |
319 | |
320 | func _() int { |
321 | print(1) |
322 | for x == nil { |
323 | } |
324 | println() // ok |
325 | } |
326 | |
327 | func _() int { |
328 | for x == nil { |
329 | for { |
330 | break |
331 | } |
332 | } |
333 | println() // ok |
334 | } |
335 | |
336 | func _() int { |
337 | for x == nil { |
338 | L: |
339 | for { |
340 | break L |
341 | } |
342 | } |
343 | println() // ok |
344 | } |
345 | |
346 | func _() int { |
347 | print(1) |
348 | for true { |
349 | } |
350 | println() // ok |
351 | } |
352 | |
353 | func _() int { |
354 | for true { |
355 | for { |
356 | break |
357 | } |
358 | } |
359 | println() // ok |
360 | } |
361 | |
362 | func _() int { |
363 | for true { |
364 | L: |
365 | for { |
366 | break L |
367 | } |
368 | } |
369 | println() // ok |
370 | } |
371 | |
372 | func _() int { |
373 | print(1) |
374 | select {} |
375 | // want "unreachable code" |
376 | } |
377 | |
378 | func _() int { |
379 | print(1) |
380 | select { |
381 | case <-c: |
382 | print(2) |
383 | panic("abc") |
384 | // want "unreachable code" |
385 | } |
386 | } |
387 | |
388 | func _() int { |
389 | print(1) |
390 | select { |
391 | case <-c: |
392 | print(2) |
393 | panic("abc") |
394 | } |
395 | // want "unreachable code" |
396 | } |
397 | |
398 | func _() int { |
399 | print(1) |
400 | select { |
401 | case <-c: |
402 | print(2) |
403 | for { |
404 | } |
405 | // want "unreachable code" |
406 | } |
407 | } |
408 | |
409 | func _() int { |
410 | print(1) |
411 | select { |
412 | case <-c: |
413 | print(2) |
414 | for { |
415 | } |
416 | } |
417 | // want "unreachable code" |
418 | } |
419 | |
420 | func _() int { |
421 | L: |
422 | print(1) |
423 | select { |
424 | case <-c: |
425 | print(2) |
426 | panic("abc") |
427 | // want "unreachable code" |
428 | case c <- 1: |
429 | print(2) |
430 | goto L |
431 | // want "unreachable code" |
432 | } |
433 | } |
434 | |
435 | func _() int { |
436 | L: |
437 | print(1) |
438 | select { |
439 | case <-c: |
440 | print(2) |
441 | panic("abc") |
442 | case c <- 1: |
443 | print(2) |
444 | goto L |
445 | } |
446 | // want "unreachable code" |
447 | } |
448 | |
449 | func _() int { |
450 | print(1) |
451 | select { |
452 | case <-c: |
453 | print(2) |
454 | panic("abc") |
455 | // want "unreachable code" |
456 | default: |
457 | select {} |
458 | // want "unreachable code" |
459 | } |
460 | } |
461 | |
462 | func _() int { |
463 | print(1) |
464 | select { |
465 | case <-c: |
466 | print(2) |
467 | panic("abc") |
468 | default: |
469 | select {} |
470 | } |
471 | // want "unreachable code" |
472 | } |
473 | |
474 | func _() int { |
475 | print(1) |
476 | select { |
477 | case <-c: |
478 | print(2) |
479 | } |
480 | println() // ok |
481 | } |
482 | |
483 | func _() int { |
484 | L: |
485 | print(1) |
486 | select { |
487 | case <-c: |
488 | print(2) |
489 | panic("abc") |
490 | // want "unreachable code" |
491 | case c <- 1: |
492 | print(2) |
493 | } |
494 | println() // ok |
495 | } |
496 | |
497 | func _() int { |
498 | print(1) |
499 | select { |
500 | case <-c: |
501 | print(2) |
502 | panic("abc") |
503 | default: |
504 | print(2) |
505 | } |
506 | println() // ok |
507 | } |
508 | |
509 | func _() int { |
510 | print(1) |
511 | select { |
512 | default: |
513 | break |
514 | } |
515 | println() // ok |
516 | } |
517 | |
518 | func _() int { |
519 | print(1) |
520 | select { |
521 | case <-c: |
522 | print(2) |
523 | panic("abc") |
524 | // want "unreachable code" |
525 | } |
526 | println() // ok |
527 | } |
528 | |
529 | func _() int { |
530 | print(1) |
531 | L: |
532 | select { |
533 | case <-c: |
534 | print(2) |
535 | for { |
536 | break L |
537 | } |
538 | } |
539 | println() // ok |
540 | } |
541 | |
542 | func _() int { |
543 | print(1) |
544 | L: |
545 | select { |
546 | case <-c: |
547 | print(2) |
548 | panic("abc") |
549 | case c <- 1: |
550 | print(2) |
551 | break L |
552 | } |
553 | println() // ok |
554 | } |
555 | |
556 | func _() int { |
557 | print(1) |
558 | select { |
559 | case <-c: |
560 | print(1) |
561 | panic("abc") |
562 | default: |
563 | select {} |
564 | // want "unreachable code" |
565 | } |
566 | println() // ok |
567 | } |
568 | |
569 | func _() int { |
570 | print(1) |
571 | switch x { |
572 | case 1: |
573 | print(2) |
574 | panic(3) |
575 | // want "unreachable code" |
576 | default: |
577 | return 4 |
578 | // want "unreachable code" |
579 | } |
580 | } |
581 | |
582 | func _() int { |
583 | print(1) |
584 | switch x { |
585 | case 1: |
586 | print(2) |
587 | panic(3) |
588 | default: |
589 | return 4 |
590 | } |
591 | // want "unreachable code" |
592 | } |
593 | |
594 | func _() int { |
595 | print(1) |
596 | switch x { |
597 | default: |
598 | return 4 |
599 | // want "unreachable code" |
600 | case 1: |
601 | print(2) |
602 | panic(3) |
603 | // want "unreachable code" |
604 | } |
605 | } |
606 | |
607 | func _() int { |
608 | print(1) |
609 | switch x { |
610 | default: |
611 | return 4 |
612 | case 1: |
613 | print(2) |
614 | panic(3) |
615 | } |
616 | // want "unreachable code" |
617 | } |
618 | |
619 | func _() int { |
620 | print(1) |
621 | switch x { |
622 | case 1: |
623 | print(2) |
624 | fallthrough |
625 | default: |
626 | return 4 |
627 | // want "unreachable code" |
628 | } |
629 | } |
630 | |
631 | func _() int { |
632 | print(1) |
633 | switch x { |
634 | case 1: |
635 | print(2) |
636 | fallthrough |
637 | default: |
638 | return 4 |
639 | } |
640 | // want "unreachable code" |
641 | } |
642 | |
643 | func _() int { |
644 | print(1) |
645 | switch { |
646 | } |
647 | println() // ok |
648 | } |
649 | |
650 | func _() int { |
651 | print(1) |
652 | switch x { |
653 | case 1: |
654 | print(2) |
655 | panic(3) |
656 | case 2: |
657 | return 4 |
658 | } |
659 | println() // ok |
660 | } |
661 | |
662 | func _() int { |
663 | print(1) |
664 | switch x { |
665 | case 2: |
666 | return 4 |
667 | case 1: |
668 | print(2) |
669 | panic(3) |
670 | } |
671 | println() // ok |
672 | } |
673 | |
674 | func _() int { |
675 | print(1) |
676 | switch x { |
677 | case 1: |
678 | print(2) |
679 | fallthrough |
680 | case 2: |
681 | return 4 |
682 | } |
683 | println() // ok |
684 | } |
685 | |
686 | func _() int { |
687 | print(1) |
688 | switch x { |
689 | case 1: |
690 | print(2) |
691 | panic(3) |
692 | } |
693 | println() // ok |
694 | } |
695 | |
696 | func _() int { |
697 | print(1) |
698 | L: |
699 | switch x { |
700 | case 1: |
701 | print(2) |
702 | panic(3) |
703 | // want "unreachable code" |
704 | default: |
705 | return 4 |
706 | } |
707 | println() // ok |
708 | } |
709 | |
710 | func _() int { |
711 | print(1) |
712 | switch x { |
713 | default: |
714 | return 4 |
715 | // want "unreachable code" |
716 | case 1: |
717 | print(2) |
718 | panic(3) |
719 | } |
720 | println() // ok |
721 | } |
722 | |
723 | func _() int { |
724 | print(1) |
725 | L: |
726 | switch x { |
727 | case 1: |
728 | print(2) |
729 | for { |
730 | break L |
731 | } |
732 | default: |
733 | return 4 |
734 | } |
735 | println() // ok |
736 | } |
737 | |
738 | func _() int { |
739 | print(1) |
740 | switch x.(type) { |
741 | case int: |
742 | print(2) |
743 | panic(3) |
744 | // want "unreachable code" |
745 | default: |
746 | return 4 |
747 | // want "unreachable code" |
748 | } |
749 | } |
750 | |
751 | func _() int { |
752 | print(1) |
753 | switch x.(type) { |
754 | case int: |
755 | print(2) |
756 | panic(3) |
757 | default: |
758 | return 4 |
759 | } |
760 | // want "unreachable code" |
761 | } |
762 | |
763 | func _() int { |
764 | print(1) |
765 | switch x.(type) { |
766 | default: |
767 | return 4 |
768 | // want "unreachable code" |
769 | case int: |
770 | print(2) |
771 | panic(3) |
772 | // want "unreachable code" |
773 | } |
774 | } |
775 | |
776 | func _() int { |
777 | print(1) |
778 | switch x.(type) { |
779 | default: |
780 | return 4 |
781 | case int: |
782 | print(2) |
783 | panic(3) |
784 | } |
785 | // want "unreachable code" |
786 | } |
787 | |
788 | func _() int { |
789 | print(1) |
790 | switch x.(type) { |
791 | case int: |
792 | print(2) |
793 | fallthrough |
794 | default: |
795 | return 4 |
796 | // want "unreachable code" |
797 | } |
798 | } |
799 | |
800 | func _() int { |
801 | print(1) |
802 | switch x.(type) { |
803 | case int: |
804 | print(2) |
805 | fallthrough |
806 | default: |
807 | return 4 |
808 | } |
809 | // want "unreachable code" |
810 | } |
811 | |
812 | func _() int { |
813 | print(1) |
814 | switch { |
815 | } |
816 | println() // ok |
817 | } |
818 | |
819 | func _() int { |
820 | print(1) |
821 | switch x.(type) { |
822 | case int: |
823 | print(2) |
824 | panic(3) |
825 | case float64: |
826 | return 4 |
827 | } |
828 | println() // ok |
829 | } |
830 | |
831 | func _() int { |
832 | print(1) |
833 | switch x.(type) { |
834 | case float64: |
835 | return 4 |
836 | case int: |
837 | print(2) |
838 | panic(3) |
839 | } |
840 | println() // ok |
841 | } |
842 | |
843 | func _() int { |
844 | print(1) |
845 | switch x.(type) { |
846 | case int: |
847 | print(2) |
848 | fallthrough |
849 | case float64: |
850 | return 4 |
851 | } |
852 | println() // ok |
853 | } |
854 | |
855 | func _() int { |
856 | print(1) |
857 | switch x.(type) { |
858 | case int: |
859 | print(2) |
860 | panic(3) |
861 | } |
862 | println() // ok |
863 | } |
864 | |
865 | func _() int { |
866 | print(1) |
867 | L: |
868 | switch x.(type) { |
869 | case int: |
870 | print(2) |
871 | panic(3) |
872 | // want "unreachable code" |
873 | default: |
874 | return 4 |
875 | } |
876 | println() // ok |
877 | } |
878 | |
879 | func _() int { |
880 | print(1) |
881 | switch x.(type) { |
882 | default: |
883 | return 4 |
884 | // want "unreachable code" |
885 | case int: |
886 | print(2) |
887 | panic(3) |
888 | } |
889 | println() // ok |
890 | } |
891 | |
892 | func _() int { |
893 | print(1) |
894 | L: |
895 | switch x.(type) { |
896 | case int: |
897 | print(2) |
898 | for { |
899 | break L |
900 | } |
901 | default: |
902 | return 4 |
903 | } |
904 | println() // ok |
905 | } |
906 | |
907 | // again, but without the leading print(1). |
908 | // testing that everything works when the terminating statement is first. |
909 | |
910 | func _() int { |
911 | println() // ok |
912 | } |
913 | |
914 | func _() int { |
915 | return 2 |
916 | // want "unreachable code" |
917 | } |
918 | |
919 | func _() int { |
920 | L: |
921 | goto L |
922 | // want "unreachable code" |
923 | } |
924 | |
925 | func _() int { |
926 | panic(2) |
927 | // want "unreachable code" |
928 | } |
929 | |
930 | // but only builtin panic |
931 | func _() int { |
932 | var panic = func(int) {} |
933 | panic(2) |
934 | println() // ok |
935 | } |
936 | |
937 | func _() int { |
938 | { |
939 | return 2 |
940 | // want "unreachable code" |
941 | } |
942 | } |
943 | |
944 | func _() int { |
945 | { |
946 | return 2 |
947 | } |
948 | // want "unreachable code" |
949 | } |
950 | |
951 | func _() int { |
952 | L: |
953 | { |
954 | goto L |
955 | // want "unreachable code" |
956 | } |
957 | } |
958 | |
959 | func _() int { |
960 | L: |
961 | { |
962 | goto L |
963 | } |
964 | // want "unreachable code" |
965 | } |
966 | |
967 | func _() int { |
968 | { |
969 | panic(2) |
970 | // want "unreachable code" |
971 | } |
972 | } |
973 | |
974 | func _() int { |
975 | { |
976 | panic(2) |
977 | } |
978 | // want "unreachable code" |
979 | } |
980 | |
981 | func _() int { |
982 | return 2 |
983 | |
984 | println() // ok |
985 | } |
986 | |
987 | func _() int { |
988 | L: |
989 | goto L |
990 | |
991 | println() // ok |
992 | } |
993 | |
994 | func _() int { |
995 | panic(2) |
996 | |
997 | println() // ok |
998 | } |
999 | |
1000 | func _() int { |
1001 | { |
1002 | return 2 |
1003 | |
1004 | } |
1005 | println() // ok |
1006 | } |
1007 | |
1008 | func _() int { |
1009 | L: |
1010 | { |
1011 | goto L |
1012 | |
1013 | } |
1014 | println() // ok |
1015 | } |
1016 | |
1017 | func _() int { |
1018 | { |
1019 | panic(2) |
1020 | |
1021 | } |
1022 | println() // ok |
1023 | } |
1024 | |
1025 | func _() int { |
1026 | { |
1027 | return 2 |
1028 | } |
1029 | |
1030 | println() // ok |
1031 | } |
1032 | |
1033 | func _() int { |
1034 | L: |
1035 | { |
1036 | goto L |
1037 | } |
1038 | |
1039 | println() // ok |
1040 | } |
1041 | |
1042 | func _() int { |
1043 | { |
1044 | panic(2) |
1045 | } |
1046 | |
1047 | println() // ok |
1048 | } |
1049 | |
1050 | // again, with func literals |
1051 | |
1052 | var _ = func() int { |
1053 | } |
1054 | |
1055 | var _ = func() int { |
1056 | print(1) |
1057 | } |
1058 | |
1059 | var _ = func() int { |
1060 | print(1) |
1061 | return 2 |
1062 | // want "unreachable code" |
1063 | } |
1064 | |
1065 | var _ = func() int { |
1066 | L: |
1067 | print(1) |
1068 | goto L |
1069 | // want "unreachable code" |
1070 | } |
1071 | |
1072 | var _ = func() int { |
1073 | print(1) |
1074 | panic(2) |
1075 | // want "unreachable code" |
1076 | } |
1077 | |
1078 | // but only builtin panic |
1079 | var _ = func() int { |
1080 | var panic = func(int) {} |
1081 | print(1) |
1082 | panic(2) |
1083 | println() // ok |
1084 | } |
1085 | |
1086 | var _ = func() int { |
1087 | { |
1088 | print(1) |
1089 | return 2 |
1090 | // want "unreachable code" |
1091 | } |
1092 | println() // ok |
1093 | } |
1094 | |
1095 | var _ = func() int { |
1096 | { |
1097 | print(1) |
1098 | return 2 |
1099 | } |
1100 | // want "unreachable code" |
1101 | } |
1102 | |
1103 | var _ = func() int { |
1104 | L: |
1105 | { |
1106 | print(1) |
1107 | goto L |
1108 | // want "unreachable code" |
1109 | } |
1110 | println() // ok |
1111 | } |
1112 | |
1113 | var _ = func() int { |
1114 | L: |
1115 | { |
1116 | print(1) |
1117 | goto L |
1118 | } |
1119 | // want "unreachable code" |
1120 | } |
1121 | |
1122 | var _ = func() int { |
1123 | print(1) |
1124 | { |
1125 | panic(2) |
1126 | } |
1127 | } |
1128 | |
1129 | var _ = func() int { |
1130 | print(1) |
1131 | { |
1132 | panic(2) |
1133 | // want "unreachable code" |
1134 | } |
1135 | } |
1136 | |
1137 | var _ = func() int { |
1138 | print(1) |
1139 | { |
1140 | panic(2) |
1141 | } |
1142 | // want "unreachable code" |
1143 | } |
1144 | |
1145 | var _ = func() int { |
1146 | print(1) |
1147 | return 2 |
1148 | |
1149 | } |
1150 | |
1151 | var _ = func() int { |
1152 | L: |
1153 | print(1) |
1154 | goto L |
1155 | |
1156 | } |
1157 | |
1158 | var _ = func() int { |
1159 | print(1) |
1160 | panic(2) |
1161 | |
1162 | } |
1163 | |
1164 | var _ = func() int { |
1165 | { |
1166 | print(1) |
1167 | return 2 |
1168 | |
1169 | } |
1170 | } |
1171 | |
1172 | var _ = func() int { |
1173 | L: |
1174 | { |
1175 | print(1) |
1176 | goto L |
1177 | |
1178 | } |
1179 | } |
1180 | |
1181 | var _ = func() int { |
1182 | print(1) |
1183 | { |
1184 | panic(2) |
1185 | |
1186 | } |
1187 | } |
1188 | |
1189 | var _ = func() int { |
1190 | { |
1191 | print(1) |
1192 | return 2 |
1193 | } |
1194 | |
1195 | } |
1196 | |
1197 | var _ = func() int { |
1198 | L: |
1199 | { |
1200 | print(1) |
1201 | goto L |
1202 | } |
1203 | |
1204 | } |
1205 | |
1206 | var _ = func() int { |
1207 | print(1) |
1208 | { |
1209 | panic(2) |
1210 | } |
1211 | |
1212 | } |
1213 | |
1214 | var _ = func() int { |
1215 | print(1) |
1216 | if x == nil { |
1217 | panic(2) |
1218 | } else { |
1219 | panic(3) |
1220 | } |
1221 | // want "unreachable code" |
1222 | } |
1223 | |
1224 | var _ = func() int { |
1225 | L: |
1226 | print(1) |
1227 | if x == nil { |
1228 | panic(2) |
1229 | } else { |
1230 | goto L |
1231 | } |
1232 | // want "unreachable code" |
1233 | } |
1234 | |
1235 | var _ = func() int { |
1236 | L: |
1237 | print(1) |
1238 | if x == nil { |
1239 | panic(2) |
1240 | } else if x == 1 { |
1241 | return 0 |
1242 | } else if x != 2 { |
1243 | panic(3) |
1244 | } else { |
1245 | goto L |
1246 | } |
1247 | // want "unreachable code" |
1248 | } |
1249 | |
1250 | // if-else chain missing final else is not okay, even if the |
1251 | // conditions cover every possible case. |
1252 | |
1253 | var _ = func() int { |
1254 | print(1) |
1255 | if x == nil { |
1256 | panic(2) |
1257 | } else if x != nil { |
1258 | panic(3) |
1259 | } |
1260 | println() // ok |
1261 | } |
1262 | |
1263 | var _ = func() int { |
1264 | print(1) |
1265 | if x == nil { |
1266 | panic(2) |
1267 | } |
1268 | println() // ok |
1269 | } |
1270 | |
1271 | var _ = func() int { |
1272 | L: |
1273 | print(1) |
1274 | if x == nil { |
1275 | panic(2) |
1276 | } else if x == 1 { |
1277 | return 0 |
1278 | } else if x != 1 { |
1279 | panic(3) |
1280 | } |
1281 | println() // ok |
1282 | } |
1283 | |
1284 | var _ = func() int { |
1285 | print(1) |
1286 | for { |
1287 | } |
1288 | // want "unreachable code" |
1289 | } |
1290 | |
1291 | var _ = func() int { |
1292 | for { |
1293 | for { |
1294 | break |
1295 | } |
1296 | } |
1297 | // want "unreachable code" |
1298 | } |
1299 | |
1300 | var _ = func() int { |
1301 | for { |
1302 | for { |
1303 | break |
1304 | // want "unreachable code" |
1305 | } |
1306 | } |
1307 | } |
1308 | |
1309 | var _ = func() int { |
1310 | for { |
1311 | for { |
1312 | continue |
1313 | // want "unreachable code" |
1314 | } |
1315 | } |
1316 | } |
1317 | |
1318 | var _ = func() int { |
1319 | for { |
1320 | L: |
1321 | for { |
1322 | break L |
1323 | } |
1324 | } |
1325 | // want "unreachable code" |
1326 | } |
1327 | |
1328 | var _ = func() int { |
1329 | print(1) |
1330 | for { |
1331 | break |
1332 | } |
1333 | println() // ok |
1334 | } |
1335 | |
1336 | var _ = func() int { |
1337 | for { |
1338 | for { |
1339 | } |
1340 | // want "unreachable code" |
1341 | } |
1342 | println() // ok |
1343 | } |
1344 | |
1345 | var _ = func() int { |
1346 | L: |
1347 | for { |
1348 | for { |
1349 | break L |
1350 | } |
1351 | } |
1352 | println() // ok |
1353 | } |
1354 | |
1355 | var _ = func() int { |
1356 | print(1) |
1357 | for x == nil { |
1358 | } |
1359 | println() // ok |
1360 | } |
1361 | |
1362 | var _ = func() int { |
1363 | for x == nil { |
1364 | for { |
1365 | break |
1366 | } |
1367 | } |
1368 | println() // ok |
1369 | } |
1370 | |
1371 | var _ = func() int { |
1372 | for x == nil { |
1373 | L: |
1374 | for { |
1375 | break L |
1376 | } |
1377 | } |
1378 | println() // ok |
1379 | } |
1380 | |
1381 | var _ = func() int { |
1382 | print(1) |
1383 | for true { |
1384 | } |
1385 | println() // ok |
1386 | } |
1387 | |
1388 | var _ = func() int { |
1389 | for true { |
1390 | for { |
1391 | break |
1392 | } |
1393 | } |
1394 | println() // ok |
1395 | } |
1396 | |
1397 | var _ = func() int { |
1398 | for true { |
1399 | L: |
1400 | for { |
1401 | break L |
1402 | } |
1403 | } |
1404 | println() // ok |
1405 | } |
1406 | |
1407 | var _ = func() int { |
1408 | print(1) |
1409 | select {} |
1410 | // want "unreachable code" |
1411 | } |
1412 | |
1413 | var _ = func() int { |
1414 | print(1) |
1415 | select { |
1416 | case <-c: |
1417 | print(2) |
1418 | panic("abc") |
1419 | // want "unreachable code" |
1420 | } |
1421 | } |
1422 | |
1423 | var _ = func() int { |
1424 | print(1) |
1425 | select { |
1426 | case <-c: |
1427 | print(2) |
1428 | panic("abc") |
1429 | } |
1430 | // want "unreachable code" |
1431 | } |
1432 | |
1433 | var _ = func() int { |
1434 | print(1) |
1435 | select { |
1436 | case <-c: |
1437 | print(2) |
1438 | for { |
1439 | } |
1440 | // want "unreachable code" |
1441 | } |
1442 | } |
1443 | |
1444 | var _ = func() int { |
1445 | print(1) |
1446 | select { |
1447 | case <-c: |
1448 | print(2) |
1449 | for { |
1450 | } |
1451 | } |
1452 | // want "unreachable code" |
1453 | } |
1454 | |
1455 | var _ = func() int { |
1456 | L: |
1457 | print(1) |
1458 | select { |
1459 | case <-c: |
1460 | print(2) |
1461 | panic("abc") |
1462 | // want "unreachable code" |
1463 | case c <- 1: |
1464 | print(2) |
1465 | goto L |
1466 | // want "unreachable code" |
1467 | } |
1468 | } |
1469 | |
1470 | var _ = func() int { |
1471 | L: |
1472 | print(1) |
1473 | select { |
1474 | case <-c: |
1475 | print(2) |
1476 | panic("abc") |
1477 | case c <- 1: |
1478 | print(2) |
1479 | goto L |
1480 | } |
1481 | // want "unreachable code" |
1482 | } |
1483 | |
1484 | var _ = func() int { |
1485 | print(1) |
1486 | select { |
1487 | case <-c: |
1488 | print(2) |
1489 | panic("abc") |
1490 | // want "unreachable code" |
1491 | default: |
1492 | select {} |
1493 | // want "unreachable code" |
1494 | } |
1495 | } |
1496 | |
1497 | var _ = func() int { |
1498 | print(1) |
1499 | select { |
1500 | case <-c: |
1501 | print(2) |
1502 | panic("abc") |
1503 | default: |
1504 | select {} |
1505 | } |
1506 | // want "unreachable code" |
1507 | } |
1508 | |
1509 | var _ = func() int { |
1510 | print(1) |
1511 | select { |
1512 | case <-c: |
1513 | print(2) |
1514 | } |
1515 | println() // ok |
1516 | } |
1517 | |
1518 | var _ = func() int { |
1519 | L: |
1520 | print(1) |
1521 | select { |
1522 | case <-c: |
1523 | print(2) |
1524 | panic("abc") |
1525 | // want "unreachable code" |
1526 | case c <- 1: |
1527 | print(2) |
1528 | } |
1529 | println() // ok |
1530 | } |
1531 | |
1532 | var _ = func() int { |
1533 | print(1) |
1534 | select { |
1535 | case <-c: |
1536 | print(2) |
1537 | panic("abc") |
1538 | default: |
1539 | print(2) |
1540 | } |
1541 | println() // ok |
1542 | } |
1543 | |
1544 | var _ = func() int { |
1545 | print(1) |
1546 | select { |
1547 | default: |
1548 | break |
1549 | } |
1550 | println() // ok |
1551 | } |
1552 | |
1553 | var _ = func() int { |
1554 | print(1) |
1555 | select { |
1556 | case <-c: |
1557 | print(2) |
1558 | panic("abc") |
1559 | // want "unreachable code" |
1560 | } |
1561 | println() // ok |
1562 | } |
1563 | |
1564 | var _ = func() int { |
1565 | print(1) |
1566 | L: |
1567 | select { |
1568 | case <-c: |
1569 | print(2) |
1570 | for { |
1571 | break L |
1572 | } |
1573 | } |
1574 | println() // ok |
1575 | } |
1576 | |
1577 | var _ = func() int { |
1578 | print(1) |
1579 | L: |
1580 | select { |
1581 | case <-c: |
1582 | print(2) |
1583 | panic("abc") |
1584 | case c <- 1: |
1585 | print(2) |
1586 | break L |
1587 | } |
1588 | println() // ok |
1589 | } |
1590 | |
1591 | var _ = func() int { |
1592 | print(1) |
1593 | select { |
1594 | case <-c: |
1595 | print(1) |
1596 | panic("abc") |
1597 | default: |
1598 | select {} |
1599 | // want "unreachable code" |
1600 | } |
1601 | println() // ok |
1602 | } |
1603 | |
1604 | var _ = func() int { |
1605 | print(1) |
1606 | switch x { |
1607 | case 1: |
1608 | print(2) |
1609 | panic(3) |
1610 | // want "unreachable code" |
1611 | default: |
1612 | return 4 |
1613 | // want "unreachable code" |
1614 | } |
1615 | } |
1616 | |
1617 | var _ = func() int { |
1618 | print(1) |
1619 | switch x { |
1620 | case 1: |
1621 | print(2) |
1622 | panic(3) |
1623 | default: |
1624 | return 4 |
1625 | } |
1626 | // want "unreachable code" |
1627 | } |
1628 | |
1629 | var _ = func() int { |
1630 | print(1) |
1631 | switch x { |
1632 | default: |
1633 | return 4 |
1634 | // want "unreachable code" |
1635 | case 1: |
1636 | print(2) |
1637 | panic(3) |
1638 | // want "unreachable code" |
1639 | } |
1640 | } |
1641 | |
1642 | var _ = func() int { |
1643 | print(1) |
1644 | switch x { |
1645 | default: |
1646 | return 4 |
1647 | case 1: |
1648 | print(2) |
1649 | panic(3) |
1650 | } |
1651 | // want "unreachable code" |
1652 | } |
1653 | |
1654 | var _ = func() int { |
1655 | print(1) |
1656 | switch x { |
1657 | case 1: |
1658 | print(2) |
1659 | fallthrough |
1660 | default: |
1661 | return 4 |
1662 | // want "unreachable code" |
1663 | } |
1664 | } |
1665 | |
1666 | var _ = func() int { |
1667 | print(1) |
1668 | switch x { |
1669 | case 1: |
1670 | print(2) |
1671 | fallthrough |
1672 | default: |
1673 | return 4 |
1674 | } |
1675 | // want "unreachable code" |
1676 | } |
1677 | |
1678 | var _ = func() int { |
1679 | print(1) |
1680 | switch { |
1681 | } |
1682 | println() // ok |
1683 | } |
1684 | |
1685 | var _ = func() int { |
1686 | print(1) |
1687 | switch x { |
1688 | case 1: |
1689 | print(2) |
1690 | panic(3) |
1691 | case 2: |
1692 | return 4 |
1693 | } |
1694 | println() // ok |
1695 | } |
1696 | |
1697 | var _ = func() int { |
1698 | print(1) |
1699 | switch x { |
1700 | case 2: |
1701 | return 4 |
1702 | case 1: |
1703 | print(2) |
1704 | panic(3) |
1705 | } |
1706 | println() // ok |
1707 | } |
1708 | |
1709 | var _ = func() int { |
1710 | print(1) |
1711 | switch x { |
1712 | case 1: |
1713 | print(2) |
1714 | fallthrough |
1715 | case 2: |
1716 | return 4 |
1717 | } |
1718 | println() // ok |
1719 | } |
1720 | |
1721 | var _ = func() int { |
1722 | print(1) |
1723 | switch x { |
1724 | case 1: |
1725 | print(2) |
1726 | panic(3) |
1727 | } |
1728 | println() // ok |
1729 | } |
1730 | |
1731 | var _ = func() int { |
1732 | print(1) |
1733 | L: |
1734 | switch x { |
1735 | case 1: |
1736 | print(2) |
1737 | panic(3) |
1738 | // want "unreachable code" |
1739 | default: |
1740 | return 4 |
1741 | } |
1742 | println() // ok |
1743 | } |
1744 | |
1745 | var _ = func() int { |
1746 | print(1) |
1747 | switch x { |
1748 | default: |
1749 | return 4 |
1750 | // want "unreachable code" |
1751 | case 1: |
1752 | print(2) |
1753 | panic(3) |
1754 | } |
1755 | println() // ok |
1756 | } |
1757 | |
1758 | var _ = func() int { |
1759 | print(1) |
1760 | L: |
1761 | switch x { |
1762 | case 1: |
1763 | print(2) |
1764 | for { |
1765 | break L |
1766 | } |
1767 | default: |
1768 | return 4 |
1769 | } |
1770 | println() // ok |
1771 | } |
1772 | |
1773 | var _ = func() int { |
1774 | print(1) |
1775 | switch x.(type) { |
1776 | case int: |
1777 | print(2) |
1778 | panic(3) |
1779 | // want "unreachable code" |
1780 | default: |
1781 | return 4 |
1782 | // want "unreachable code" |
1783 | } |
1784 | } |
1785 | |
1786 | var _ = func() int { |
1787 | print(1) |
1788 | switch x.(type) { |
1789 | case int: |
1790 | print(2) |
1791 | panic(3) |
1792 | default: |
1793 | return 4 |
1794 | } |
1795 | // want "unreachable code" |
1796 | } |
1797 | |
1798 | var _ = func() int { |
1799 | print(1) |
1800 | switch x.(type) { |
1801 | default: |
1802 | return 4 |
1803 | // want "unreachable code" |
1804 | case int: |
1805 | print(2) |
1806 | panic(3) |
1807 | // want "unreachable code" |
1808 | } |
1809 | } |
1810 | |
1811 | var _ = func() int { |
1812 | print(1) |
1813 | switch x.(type) { |
1814 | default: |
1815 | return 4 |
1816 | case int: |
1817 | print(2) |
1818 | panic(3) |
1819 | } |
1820 | // want "unreachable code" |
1821 | } |
1822 | |
1823 | var _ = func() int { |
1824 | print(1) |
1825 | switch x.(type) { |
1826 | case int: |
1827 | print(2) |
1828 | fallthrough |
1829 | default: |
1830 | return 4 |
1831 | // want "unreachable code" |
1832 | } |
1833 | } |
1834 | |
1835 | var _ = func() int { |
1836 | print(1) |
1837 | switch x.(type) { |
1838 | case int: |
1839 | print(2) |
1840 | fallthrough |
1841 | default: |
1842 | return 4 |
1843 | } |
1844 | // want "unreachable code" |
1845 | } |
1846 | |
1847 | var _ = func() int { |
1848 | print(1) |
1849 | switch { |
1850 | } |
1851 | println() // ok |
1852 | } |
1853 | |
1854 | var _ = func() int { |
1855 | print(1) |
1856 | switch x.(type) { |
1857 | case int: |
1858 | print(2) |
1859 | panic(3) |
1860 | case float64: |
1861 | return 4 |
1862 | } |
1863 | println() // ok |
1864 | } |
1865 | |
1866 | var _ = func() int { |
1867 | print(1) |
1868 | switch x.(type) { |
1869 | case float64: |
1870 | return 4 |
1871 | case int: |
1872 | print(2) |
1873 | panic(3) |
1874 | } |
1875 | println() // ok |
1876 | } |
1877 | |
1878 | var _ = func() int { |
1879 | print(1) |
1880 | switch x.(type) { |
1881 | case int: |
1882 | print(2) |
1883 | fallthrough |
1884 | case float64: |
1885 | return 4 |
1886 | } |
1887 | println() // ok |
1888 | } |
1889 | |
1890 | var _ = func() int { |
1891 | print(1) |
1892 | switch x.(type) { |
1893 | case int: |
1894 | print(2) |
1895 | panic(3) |
1896 | } |
1897 | println() // ok |
1898 | } |
1899 | |
1900 | var _ = func() int { |
1901 | print(1) |
1902 | L: |
1903 | switch x.(type) { |
1904 | case int: |
1905 | print(2) |
1906 | panic(3) |
1907 | // want "unreachable code" |
1908 | default: |
1909 | return 4 |
1910 | } |
1911 | println() // ok |
1912 | } |
1913 | |
1914 | var _ = func() int { |
1915 | print(1) |
1916 | switch x.(type) { |
1917 | default: |
1918 | return 4 |
1919 | // want "unreachable code" |
1920 | case int: |
1921 | print(2) |
1922 | panic(3) |
1923 | } |
1924 | println() // ok |
1925 | } |
1926 | |
1927 | var _ = func() int { |
1928 | print(1) |
1929 | L: |
1930 | switch x.(type) { |
1931 | case int: |
1932 | print(2) |
1933 | for { |
1934 | break L |
1935 | } |
1936 | default: |
1937 | return 4 |
1938 | } |
1939 | println() // ok |
1940 | } |
1941 | |
1942 | // again, but without the leading print(1). |
1943 | // testing that everything works when the terminating statement is first. |
1944 | |
1945 | var _ = func() int { |
1946 | println() // ok |
1947 | } |
1948 | |
1949 | var _ = func() int { |
1950 | return 2 |
1951 | // want "unreachable code" |
1952 | } |
1953 | |
1954 | var _ = func() int { |
1955 | L: |
1956 | goto L |
1957 | // want "unreachable code" |
1958 | } |
1959 | |
1960 | var _ = func() int { |
1961 | panic(2) |
1962 | // want "unreachable code" |
1963 | } |
1964 | |
1965 | // but only builtin panic |
1966 | var _ = func() int { |
1967 | var panic = func(int) {} |
1968 | panic(2) |
1969 | println() // ok |
1970 | } |
1971 | |
1972 | var _ = func() int { |
1973 | { |
1974 | return 2 |
1975 | // want "unreachable code" |
1976 | } |
1977 | } |
1978 | |
1979 | var _ = func() int { |
1980 | { |
1981 | return 2 |
1982 | } |
1983 | // want "unreachable code" |
1984 | } |
1985 | |
1986 | var _ = func() int { |
1987 | L: |
1988 | { |
1989 | goto L |
1990 | // want "unreachable code" |
1991 | } |
1992 | } |
1993 | |
1994 | var _ = func() int { |
1995 | L: |
1996 | { |
1997 | goto L |
1998 | } |
1999 | // want "unreachable code" |
2000 | } |
2001 | |
2002 | var _ = func() int { |
2003 | { |
2004 | panic(2) |
2005 | // want "unreachable code" |
2006 | } |
2007 | } |
2008 | |
2009 | var _ = func() int { |
2010 | { |
2011 | panic(2) |
2012 | } |
2013 | // want "unreachable code" |
2014 | } |
2015 | |
2016 | var _ = func() int { |
2017 | return 2 |
2018 | |
2019 | println() // ok |
2020 | } |
2021 | |
2022 | var _ = func() int { |
2023 | L: |
2024 | goto L |
2025 | |
2026 | println() // ok |
2027 | } |
2028 | |
2029 | var _ = func() int { |
2030 | panic(2) |
2031 | |
2032 | println() // ok |
2033 | } |
2034 | |
2035 | var _ = func() int { |
2036 | { |
2037 | return 2 |
2038 | |
2039 | } |
2040 | println() // ok |
2041 | } |
2042 | |
2043 | var _ = func() int { |
2044 | L: |
2045 | { |
2046 | goto L |
2047 | |
2048 | } |
2049 | println() // ok |
2050 | } |
2051 | |
2052 | var _ = func() int { |
2053 | { |
2054 | panic(2) |
2055 | |
2056 | } |
2057 | println() // ok |
2058 | } |
2059 | |
2060 | var _ = func() int { |
2061 | { |
2062 | return 2 |
2063 | } |
2064 | |
2065 | println() // ok |
2066 | } |
2067 | |
2068 | var _ = func() int { |
2069 | L: |
2070 | { |
2071 | goto L |
2072 | } |
2073 | |
2074 | println() // ok |
2075 | } |
2076 | |
2077 | var _ = func() int { |
2078 | { |
2079 | panic(2) |
2080 | } |
2081 | |
2082 | println() // ok |
2083 | } |
2084 | |
2085 | var _ = func() { |
2086 | // goto without label used to panic |
2087 | goto |
2088 | } |
2089 | |
2090 | func _() int { |
2091 | // Empty switch tag with non-bool case value used to panic. |
2092 | switch { |
2093 | case 1: |
2094 | println() |
2095 | } |
2096 | println() |
2097 | } |
2098 |
Members