1const E = enum { a, b };
2export fn entry1() void {
3 switch (E.a) {
4 .a => {},
5 }
6}
7export fn entry2() void {
8 switch (E.a) {
9 .a, .b => {},
10 else => {},
11 }
12}
13
14const T = union(E) { a: u32, b };
15export fn entry3() void {
16 switch (T{ .a = 0 }) {
17 .a => {},
18 }
19}
20export fn entry4() void {
21 switch (T{ .a = 0 }) {
22 .a, .b => {},
23 else => {},
24 }
25}
26
27const Error = error{ MyError, MyOtherError };
28export fn entry5() void {
29 switch (Error.MyError) {
30 error.MyError => {},
31 }
32}
33export fn entry6() void {
34 switch (Error.MyError) {
35 error.MyError, error.MyOtherError => {},
36 else => {},
37 }
38}
39
40const U = packed union { a: u1, b: i1 };
41export fn entry7() void {
42 switch (U{ .a = 0 }) {
43 .{ .a = 0 } => {},
44 }
45}
46export fn entry8() void {
47 switch (U{ .a = 0 }) {
48 .{ .a = 0 }, .{ .a = 1 } => {},
49 else => {},
50 }
51}
52
53const S = packed struct { a: u1 };
54export fn entry9() void {
55 switch (S{ .a = 0 }) {
56 .{ .a = 0 } => {},
57 }
58}
59export fn entry10() void {
60 switch (S{ .a = 0 }) {
61 .{ .a = 0 }, .{ .a = 1 } => {},
62 else => {},
63 }
64}
65
66export fn entry11() void {
67 switch (@as(u1, 0)) {
68 0 => {},
69 }
70}
71export fn entry12() void {
72 switch (@as(u1, 0)) {
73 0, 1 => {},
74 else => {},
75 }
76}
77
78export fn entry13() void {
79 switch (true) {
80 true => {},
81 }
82}
83export fn entry14() void {
84 switch (true) {
85 true, false => {},
86 else => {},
87 }
88}
89
90export fn entry15() void {
91 switch ({}) {}
92}
93export fn entry16() void {
94 switch ({}) {
95 {} => {},
96 else => {},
97 }
98}
99
100export fn entry17() void {
101 switch (123) {
102 123 => {},
103 }
104}
105
106export fn entry18() void {
107 switch (.foo) {
108 .foo => {},
109 }
110}
111
112fn bar() void {}
113export fn entry19() void {
114 switch (bar) {
115 bar => {},
116 }
117}
118
119const baz: *u8 = @ptrFromInt(123);
120export fn entry20() void {
121 switch (baz) {
122 baz => {},
123 }
124}
125
126export fn entry21() void {
127 switch (u32) {
128 u32 => {},
129 }
130}
131
132export fn entry22() void {
133 switch (@as(anyerror, error.MyError)) {
134 error.MyError => {},
135 }
136}
137
138// error
139//
140// :3:5: error: switch must handle all possibilities
141// :1:21: note: unhandled enumeration value: 'b'
142// :1:11: note: enum 'tmp.E' declared here
143// :10:14: error: unreachable else prong; all cases already handled
144// :16:5: error: switch must handle all possibilities
145// :1:21: note: unhandled enumeration value: 'b'
146// :1:11: note: enum 'tmp.E' declared here
147// :23:14: error: unreachable else prong; all cases already handled
148// :29:5: error: switch must handle all possibilities
149// :29:5: note: unhandled error value: 'error.MyOtherError'
150// :36:14: error: unreachable else prong; all cases already handled
151// :42:5: error: switch must handle all possibilities
152// :49:14: error: unreachable else prong; all cases already handled
153// :55:5: error: switch must handle all possibilities
154// :62:14: error: unreachable else prong; all cases already handled
155// :67:5: error: switch must handle all possibilities
156// :74:14: error: unreachable else prong; all cases already handled
157// :79:5: error: switch must handle all possibilities
158// :86:14: error: unreachable else prong; all cases already handled
159// :91:5: error: switch must handle all possibilities
160// :96:14: error: unreachable else prong; all cases already handled
161// :101:5: error: else prong required when switching on type 'comptime_int'
162// :107:5: error: else prong required when switching on type '@EnumLiteral()'
163// :114:5: error: else prong required when switching on type 'fn () void'
164// :121:5: error: else prong required when switching on type '*u8'
165// :127:5: error: else prong required when switching on type 'type'
166// :133:5: error: else prong required when switching on type 'anyerror'