authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-11-30 19:12:47+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
log69ab687156f1099570ec2208efae8be6883073c0
tree9b6f3361e5bfe5028b265afbbe426309d12055ec
parentfc6dc797ceff35c5bbaa51d914a62256bab3c409

test: add tests for switch_block_err_union


8 files changed, 935 insertions(+), 0 deletions(-)

test/behavior/switch_on_captured_error.zig created+750
...@@ -0,0 +1,750 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const expect = std.testing.expect;
4const expectError = std.testing.expectError;
5const expectEqual = std.testing.expectEqual;
6
7test "switch on error union catch capture" {
8 const S = struct {
9 const Error = error{ A, B, C };
10 fn doTheTest() !void {
11 try testScalar();
12 try testMulti();
13 try testElse();
14 try testCapture();
15 try testInline();
16 try testEmptyErrSet();
17 }
18
19 fn testScalar() !void {
20 {
21 var a: Error!u64 = 3;
22 _ = &a;
23 const b: u64 = a catch |err| switch (err) {
24 error.A => 0,
25 error.B => 1,
26 error.C => 2,
27 };
28 try expectEqual(@as(u64, 3), b);
29 }
30 {
31 var a: Error!u64 = 3;
32 _ = &a;
33 const b: u64 = a catch |err| switch (err) {
34 error.A => 0,
35 error.B => @intFromError(err) + 4,
36 error.C => @intFromError(err) + 4,
37 };
38 try expectEqual(@as(u64, 3), b);
39 }
40 {
41 var a: Error!u64 = error.A;
42 _ = &a;
43 const b: u64 = a catch |err| switch (err) {
44 error.A => 0,
45 error.B => @intFromError(err) + 4,
46 error.C => @intFromError(err) + 4,
47 };
48 try expectEqual(@as(u64, 0), b);
49 }
50 }
51
52 fn testMulti() !void {
53 {
54 var a: Error!u64 = 3;
55 _ = &a;
56 const b: u64 = a catch |err| switch (err) {
57 error.A, error.B => 0,
58 error.C => @intFromError(err) + 4,
59 };
60 try expectEqual(@as(u64, 3), b);
61 }
62 {
63 var a: Error!u64 = 3;
64 _ = &a;
65 const b: u64 = a catch |err| switch (err) {
66 error.A => 0,
67 error.B, error.C => @intFromError(err) + 4,
68 };
69 try expectEqual(@as(u64, 3), b);
70 }
71 {
72 var a: Error!u64 = error.A;
73 _ = &a;
74 const b: u64 = a catch |err| switch (err) {
75 error.A, error.B => 0,
76 error.C => @intFromError(err) + 4,
77 };
78 try expectEqual(@as(u64, 0), b);
79 }
80 {
81 var a: Error!u64 = error.A;
82 _ = &a;
83 const b: u64 = a catch |err| switch (err) {
84 error.A => 0,
85 error.B, error.C => @intFromError(err) + 4,
86 };
87 try expectEqual(@as(u64, 0), b);
88 }
89 {
90 var a: Error!u64 = error.B;
91 _ = &a;
92 const b: u64 = a catch |err| switch (err) {
93 error.A => 0,
94 error.B, error.C => @intFromError(err) + 4,
95 };
96 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
97 }
98 }
99
100 fn testElse() !void {
101 {
102 var a: Error!u64 = 3;
103 _ = &a;
104 const b: u64 = a catch |err| switch (err) {
105 error.A => 0,
106 else => 1,
107 };
108 try expectEqual(@as(u64, 3), b);
109 }
110 {
111 var a: Error!u64 = 3;
112 _ = &a;
113 const b: u64 = a catch |err| switch (err) {
114 error.A => 0,
115 else => @intFromError(err) + 4,
116 };
117 try expectEqual(@as(u64, 3), b);
118 }
119 {
120 var a: Error!u64 = error.A;
121 _ = &a;
122 const b: u64 = a catch |err| switch (err) {
123 error.A => 1,
124 else => @intFromError(err) + 4,
125 };
126 try expectEqual(@as(u64, 1), b);
127 }
128 {
129 var a: Error!u64 = error.B;
130 _ = &a;
131 const b: u64 = a catch |err| switch (err) {
132 error.A => 0,
133 else => 1,
134 };
135 try expectEqual(@as(u64, 1), b);
136 }
137 {
138 var a: Error!u64 = error.B;
139 _ = &a;
140 const b: u64 = a catch |err| switch (err) {
141 error.A => 0,
142 else => @intFromError(err) + 4,
143 };
144 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
145 }
146 }
147
148 fn testCapture() !void {
149 {
150 var a: Error!u64 = error.A;
151 _ = &a;
152 const b: u64 = a catch |err| switch (err) {
153 error.A => |e| @intFromError(e) + 4,
154 else => 0,
155 };
156 try expectEqual(@as(u64, @intFromError(error.A) + 4), b);
157 }
158 {
159 var a: Error!u64 = error.A;
160 _ = &a;
161 const b: u64 = a catch |err| switch (err) {
162 error.A => 0,
163 else => |e| @intFromError(e) + 4,
164 };
165 try expectEqual(@as(u64, 0), b);
166 }
167 {
168 var a: Error!u64 = error.B;
169 _ = &a;
170 const b: u64 = a catch |err| switch (err) {
171 error.A => 0,
172 else => |e| @intFromError(e) + 4,
173 };
174 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
175 }
176 {
177 var a: Error!u64 = error.B;
178 _ = &a;
179 const b: u64 = a catch |err| switch (err) {
180 error.A => |e| @intFromError(e) + 4,
181 else => |e| @intFromError(e) + 4,
182 };
183 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
184 }
185 {
186 var a: Error!u64 = error.B;
187 _ = &a;
188 const b: u64 = a catch |err| switch (err) {
189 error.A => 0,
190 error.B, error.C => |e| @intFromError(e) + 4,
191 };
192 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
193 }
194 }
195
196 fn testInline() !void {
197 {
198 var a: Error!u64 = error.B;
199 _ = &a;
200 const b: u64 = a catch |err| switch (err) {
201 error.A => 0,
202 inline else => @intFromError(err) + 4,
203 };
204 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
205 }
206 {
207 var a: Error!u64 = error.B;
208 _ = &a;
209 const b: u64 = a catch |err| switch (err) {
210 error.A => |e| @intFromError(e) + 4,
211 inline else => @intFromError(err) + 4,
212 };
213 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
214 }
215 {
216 var a: Error!u64 = error.B;
217 _ = &a;
218 const b: u64 = a catch |err| switch (err) {
219 inline else => |e| @intFromError(e) + 4,
220 };
221 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
222 }
223 {
224 var a: Error!u64 = error.B;
225 _ = &a;
226 const b: u64 = a catch |err| switch (err) {
227 error.A => 0,
228 inline error.B, error.C => |e| @intFromError(e) + 4,
229 };
230 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
231 }
232 }
233
234 fn testEmptyErrSet() !void {
235 {
236 var a: error{}!u64 = 0;
237 _ = &a;
238 const b: u64 = a catch |err| switch (err) {
239 else => |e| return e,
240 };
241 try expectEqual(@as(u64, 0), b);
242 }
243 {
244 var a: error{}!u64 = 0;
245 _ = &a;
246 const b: u64 = a catch |err| switch (err) {
247 error.UnknownError => return error.Fail,
248 else => |e| return e,
249 };
250 try expectEqual(@as(u64, 0), b);
251 }
252 }
253 };
254
255 try comptime S.doTheTest();
256 try S.doTheTest();
257}
258
259test "switch on error union if else capture" {
260 const S = struct {
261 const Error = error{ A, B, C };
262 fn doTheTest() !void {
263 try testScalar();
264 try testScalarPtr();
265 try testMulti();
266 try testMultiPtr();
267 try testElse();
268 try testElsePtr();
269 try testCapture();
270 try testCapturePtr();
271 try testInline();
272 try testInlinePtr();
273 try testEmptyErrSet();
274 try testEmptyErrSetPtr();
275 }
276
277 fn testScalar() !void {
278 {
279 var a: Error!u64 = 3;
280 _ = &a;
281 const b: u64 = if (a) |x| x else |err| switch (err) {
282 error.A => 0,
283 error.B => 1,
284 error.C => 2,
285 };
286 try expectEqual(@as(u64, 3), b);
287 }
288 {
289 var a: Error!u64 = 3;
290 _ = &a;
291 const b: u64 = if (a) |x| x else |err| switch (err) {
292 error.A => 0,
293 error.B => @intFromError(err) + 4,
294 error.C => @intFromError(err) + 4,
295 };
296 try expectEqual(@as(u64, 3), b);
297 }
298 {
299 var a: Error!u64 = error.A;
300 _ = &a;
301 const b: u64 = if (a) |x| x else |err| switch (err) {
302 error.A => 0,
303 error.B => @intFromError(err) + 4,
304 error.C => @intFromError(err) + 4,
305 };
306 try expectEqual(@as(u64, 0), b);
307 }
308 }
309
310 fn testScalarPtr() !void {
311 {
312 var a: Error!u64 = 3;
313 _ = &a;
314 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
315 error.A => 0,
316 error.B => 1,
317 error.C => 2,
318 };
319 try expectEqual(@as(u64, 3), b);
320 }
321 {
322 var a: Error!u64 = 3;
323 _ = &a;
324 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
325 error.A => 0,
326 error.B => @intFromError(err) + 4,
327 error.C => @intFromError(err) + 4,
328 };
329 try expectEqual(@as(u64, 3), b);
330 }
331 {
332 var a: Error!u64 = error.A;
333 _ = &a;
334 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
335 error.A => 0,
336 error.B => @intFromError(err) + 4,
337 error.C => @intFromError(err) + 4,
338 };
339 try expectEqual(@as(u64, 0), b);
340 }
341 }
342
343 fn testMulti() !void {
344 {
345 var a: Error!u64 = 3;
346 _ = &a;
347 const b: u64 = if (a) |x| x else |err| switch (err) {
348 error.A, error.B => 0,
349 error.C => @intFromError(err) + 4,
350 };
351 try expectEqual(@as(u64, 3), b);
352 }
353 {
354 var a: Error!u64 = 3;
355 _ = &a;
356 const b: u64 = if (a) |x| x else |err| switch (err) {
357 error.A => 0,
358 error.B, error.C => @intFromError(err) + 4,
359 };
360 try expectEqual(@as(u64, 3), b);
361 }
362 {
363 var a: Error!u64 = error.A;
364 _ = &a;
365 const b: u64 = if (a) |x| x else |err| switch (err) {
366 error.A, error.B => 0,
367 error.C => @intFromError(err) + 4,
368 };
369 try expectEqual(@as(u64, 0), b);
370 }
371 {
372 var a: Error!u64 = error.A;
373 _ = &a;
374 const b: u64 = if (a) |x| x else |err| switch (err) {
375 error.A => 0,
376 error.B, error.C => @intFromError(err) + 4,
377 };
378 try expectEqual(@as(u64, 0), b);
379 }
380 {
381 var a: Error!u64 = error.B;
382 _ = &a;
383 const b: u64 = if (a) |x| x else |err| switch (err) {
384 error.A => 0,
385 error.B, error.C => @intFromError(err) + 4,
386 };
387 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
388 }
389 }
390
391 fn testMultiPtr() !void {
392 {
393 var a: Error!u64 = 3;
394 _ = &a;
395 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
396 error.A, error.B => 0,
397 error.C => @intFromError(err) + 4,
398 };
399 try expectEqual(@as(u64, 3), b);
400 }
401 {
402 var a: Error!u64 = 3;
403 _ = &a;
404 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
405 error.A => 0,
406 error.B, error.C => @intFromError(err) + 4,
407 };
408 try expectEqual(@as(u64, 3), b);
409 }
410 {
411 var a: Error!u64 = error.A;
412 _ = &a;
413 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
414 error.A, error.B => 0,
415 error.C => @intFromError(err) + 4,
416 };
417 try expectEqual(@as(u64, 0), b);
418 }
419 {
420 var a: Error!u64 = error.A;
421 _ = &a;
422 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
423 error.A => 0,
424 error.B, error.C => @intFromError(err) + 4,
425 };
426 try expectEqual(@as(u64, 0), b);
427 }
428 {
429 var a: Error!u64 = error.B;
430 _ = &a;
431 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
432 error.A => 0,
433 error.B, error.C => @intFromError(err) + 4,
434 };
435 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
436 }
437 }
438
439 fn testElse() !void {
440 {
441 var a: Error!u64 = 3;
442 _ = &a;
443 const b: u64 = if (a) |x| x else |err| switch (err) {
444 error.A => 0,
445 else => 1,
446 };
447 try expectEqual(@as(u64, 3), b);
448 }
449 {
450 var a: Error!u64 = 3;
451 _ = &a;
452 const b: u64 = if (a) |x| x else |err| switch (err) {
453 error.A => 0,
454 else => @intFromError(err) + 4,
455 };
456 try expectEqual(@as(u64, 3), b);
457 }
458 {
459 var a: Error!u64 = error.A;
460 _ = &a;
461 const b: u64 = if (a) |x| x else |err| switch (err) {
462 error.A => 1,
463 else => @intFromError(err) + 4,
464 };
465 try expectEqual(@as(u64, 1), b);
466 }
467 {
468 var a: Error!u64 = error.B;
469 _ = &a;
470 const b: u64 = if (a) |x| x else |err| switch (err) {
471 error.A => 0,
472 else => 1,
473 };
474 try expectEqual(@as(u64, 1), b);
475 }
476 {
477 var a: Error!u64 = error.B;
478 _ = &a;
479 const b: u64 = if (a) |x| x else |err| switch (err) {
480 error.A => 0,
481 else => @intFromError(err) + 4,
482 };
483 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
484 }
485 }
486
487 fn testElsePtr() !void {
488 {
489 var a: Error!u64 = 3;
490 _ = &a;
491 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
492 error.A => 0,
493 else => 1,
494 };
495 try expectEqual(@as(u64, 3), b);
496 }
497 {
498 var a: Error!u64 = 3;
499 _ = &a;
500 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
501 error.A => 0,
502 else => @intFromError(err) + 4,
503 };
504 try expectEqual(@as(u64, 3), b);
505 }
506 {
507 var a: Error!u64 = error.A;
508 _ = &a;
509 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
510 error.A => 1,
511 else => @intFromError(err) + 4,
512 };
513 try expectEqual(@as(u64, 1), b);
514 }
515 {
516 var a: Error!u64 = error.B;
517 _ = &a;
518 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
519 error.A => 0,
520 else => 1,
521 };
522 try expectEqual(@as(u64, 1), b);
523 }
524 {
525 var a: Error!u64 = error.B;
526 _ = &a;
527 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
528 error.A => 0,
529 else => @intFromError(err) + 4,
530 };
531 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
532 }
533 }
534
535 fn testCapture() !void {
536 {
537 var a: Error!u64 = error.A;
538 _ = &a;
539 const b: u64 = if (a) |x| x else |err| switch (err) {
540 error.A => |e| @intFromError(e) + 4,
541 else => 0,
542 };
543 try expectEqual(@as(u64, @intFromError(error.A) + 4), b);
544 }
545 {
546 var a: Error!u64 = error.A;
547 _ = &a;
548 const b: u64 = if (a) |x| x else |err| switch (err) {
549 error.A => 0,
550 else => |e| @intFromError(e) + 4,
551 };
552 try expectEqual(@as(u64, 0), b);
553 }
554 {
555 var a: Error!u64 = error.B;
556 _ = &a;
557 const b: u64 = if (a) |x| x else |err| switch (err) {
558 error.A => 0,
559 else => |e| @intFromError(e) + 4,
560 };
561 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
562 }
563 {
564 var a: Error!u64 = error.B;
565 _ = &a;
566 const b: u64 = if (a) |x| x else |err| switch (err) {
567 error.A => |e| @intFromError(e) + 4,
568 else => |e| @intFromError(e) + 4,
569 };
570 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
571 }
572 {
573 var a: Error!u64 = error.B;
574 _ = &a;
575 const b: u64 = if (a) |x| x else |err| switch (err) {
576 error.A => 0,
577 error.B, error.C => |e| @intFromError(e) + 4,
578 };
579 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
580 }
581 }
582
583 fn testCapturePtr() !void {
584 {
585 var a: Error!u64 = error.A;
586 _ = &a;
587 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
588 error.A => |e| @intFromError(e) + 4,
589 else => 0,
590 };
591 try expectEqual(@as(u64, @intFromError(error.A) + 4), b);
592 }
593 {
594 var a: Error!u64 = error.A;
595 _ = &a;
596 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
597 error.A => 0,
598 else => |e| @intFromError(e) + 4,
599 };
600 try expectEqual(@as(u64, 0), b);
601 }
602 {
603 var a: Error!u64 = error.B;
604 _ = &a;
605 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
606 error.A => 0,
607 else => |e| @intFromError(e) + 4,
608 };
609 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
610 }
611 {
612 var a: Error!u64 = error.B;
613 _ = &a;
614 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
615 error.A => |e| @intFromError(e) + 4,
616 else => |e| @intFromError(e) + 4,
617 };
618 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
619 }
620 {
621 var a: Error!u64 = error.B;
622 _ = &a;
623 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
624 error.A => 0,
625 error.B, error.C => |e| @intFromError(e) + 4,
626 };
627 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
628 }
629 }
630
631 fn testInline() !void {
632 {
633 var a: Error!u64 = error.B;
634 _ = &a;
635 const b: u64 = if (a) |x| x else |err| switch (err) {
636 error.A => 0,
637 inline else => @intFromError(err) + 4,
638 };
639 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
640 }
641 {
642 var a: Error!u64 = error.B;
643 _ = &a;
644 const b: u64 = if (a) |x| x else |err| switch (err) {
645 error.A => |e| @intFromError(e) + 4,
646 inline else => @intFromError(err) + 4,
647 };
648 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
649 }
650 {
651 var a: Error!u64 = error.B;
652 _ = &a;
653 const b: u64 = if (a) |x| x else |err| switch (err) {
654 inline else => |e| @intFromError(e) + 4,
655 };
656 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
657 }
658 {
659 var a: Error!u64 = error.B;
660 _ = &a;
661 const b: u64 = if (a) |x| x else |err| switch (err) {
662 error.A => 0,
663 inline error.B, error.C => |e| @intFromError(e) + 4,
664 };
665 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
666 }
667 }
668
669 fn testInlinePtr() !void {
670 {
671 var a: Error!u64 = error.B;
672 _ = &a;
673 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
674 error.A => 0,
675 inline else => @intFromError(err) + 4,
676 };
677 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
678 }
679 {
680 var a: Error!u64 = error.B;
681 _ = &a;
682 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
683 error.A => |e| @intFromError(e) + 4,
684 inline else => @intFromError(err) + 4,
685 };
686 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
687 }
688 {
689 var a: Error!u64 = error.B;
690 _ = &a;
691 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
692 inline else => |e| @intFromError(e) + 4,
693 };
694 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
695 }
696 {
697 var a: Error!u64 = error.B;
698 _ = &a;
699 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
700 error.A => 0,
701 inline error.B, error.C => |e| @intFromError(e) + 4,
702 };
703 try expectEqual(@as(u64, @intFromError(error.B) + 4), b);
704 }
705 }
706
707 fn testEmptyErrSet() !void {
708 {
709 var a: error{}!u64 = 0;
710 _ = &a;
711 const b: u64 = if (a) |x| x else |err| switch (err) {
712 else => |e| return e,
713 };
714 try expectEqual(@as(u64, 0), b);
715 }
716 {
717 var a: error{}!u64 = 0;
718 _ = &a;
719 const b: u64 = if (a) |x| x else |err| switch (err) {
720 error.UnknownError => return error.Fail,
721 else => |e| return e,
722 };
723 try expectEqual(@as(u64, 0), b);
724 }
725 }
726
727 fn testEmptyErrSetPtr() !void {
728 {
729 var a: error{}!u64 = 0;
730 _ = &a;
731 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
732 else => |e| return e,
733 };
734 try expectEqual(@as(u64, 0), b);
735 }
736 {
737 var a: error{}!u64 = 0;
738 _ = &a;
739 const b: u64 = if (a) |*x| x.* else |err| switch (err) {
740 error.UnknownError => return error.Fail,
741 else => |e| return e,
742 };
743 try expectEqual(@as(u64, 0), b);
744 }
745 }
746 };
747
748 try comptime S.doTheTest();
749 try S.doTheTest();
750}
test/cases/compile_errors/switch_expression-duplicate_error_prong.zig created+33
...@@ -0,0 +1,33 @@
1fn f(n: Error!i32) i32 {
2 if (n) |x|
3 _ = x
4 else |e| switch (e) {
5 error.Foo => 1,
6 error.Bar => 2,
7 error.Baz => 3,
8 error.Foo => 2,
9 }
10}
11fn g(n: Error!i32) i32 {
12 n catch |e| switch (e) {
13 error.Foo => 1,
14 error.Bar => 2,
15 error.Baz => 3,
16 error.Foo => 2,
17 };
18}
19
20const Error = error{ Foo, Bar, Baz };
21
22export fn entry() usize {
23 return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g));
24}
25
26// error
27// backend=stage2
28// target=native
29//
30// :8:9: error: duplicate switch value
31// :5:9: note: previous value here
32// :16:9: error: duplicate switch value
33// :13:9: note: previous value here
test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig created+35
...@@ -0,0 +1,35 @@
1fn f(n: Error!i32) i32 {
2 if (n) |x|
3 _ = x
4 else |e| switch (e) {
5 error.Foo => 1,
6 error.Bar => 2,
7 error.Baz => 3,
8 error.Foo => 2,
9 else => 10,
10 }
11}
12fn g(n: Error!i32) i32 {
13 n catch |e| switch (e) {
14 error.Foo => 1,
15 error.Bar => 2,
16 error.Baz => 3,
17 error.Foo => 2,
18 else => 10,
19 };
20}
21
22const Error = error{ Foo, Bar, Baz };
23
24export fn entry() usize {
25 return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g));
26}
27
28// error
29// backend=stage2
30// target=native
31//
32// :8:9: error: duplicate switch value
33// :5:9: note: previous value here
34// :17:9: error: duplicate switch value
35// :14:9: note: previous value here
test/cases/compile_errors/switch_expression-missing_error_prong.zig created+33
...@@ -0,0 +1,33 @@
1const Error = error {
2 One,
3 Two,
4 Three,
5 Four,
6};
7fn f(n: Error!i32) i32 {
8 if (n) |x| x else |e| switch (e) {
9 error.One => 1,
10 error.Two => 2,
11 error.Three => 3,
12 }
13}
14fn h(n: Error!i32) i32 {
15 n catch |e| switch (e) {
16 error.One => 1,
17 error.Two => 2,
18 error.Three => 3,
19 };
20}
21
22export fn entry() usize {
23 return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&h));
24}
25
26// error
27// backend=stage2
28// target=native
29//
30// :8:27: error: switch must handle all possibilities
31// :8:27: note: unhandled error value: 'error.Four'
32// :15:17: error: switch must handle all possibilities
33// :15:17: note: unhandled error value: 'error.Four'
test/cases/compile_errors/switch_expression-multiple_else_prongs.zig+20
...@@ -5,8 +5,24 @@ fn f(x: u32) void {...@@ -5,8 +5,24 @@ fn f(x: u32) void {
5 else => true,5 else => true,
6 };6 };
7}7}
8fn g(x: error{Foo, Bar, Baz}!u32) void {
9 const value: bool = if (x) |_| true else |e| switch (e) {
10 error.Foo => false,
11 else => true,
12 else => true,
13 };
14}
15fn h(x: error{Foo, Bar, Baz}!u32) void {
16 const value: u32 = x catch |e| switch (e) {
17 error.Foo => 1,
18 else => 2,
19 else => 3,
20 };
21}
8export fn entry() void {22export fn entry() void {
9 f(1234);23 f(1234);
24 g(1234);
25 h(1234);
10}26}
1127
12// error28// error
...@@ -15,3 +31,7 @@ export fn entry() void {...@@ -15,3 +31,7 @@ export fn entry() void {
15//31//
16// :5:9: error: multiple else prongs in switch expression32// :5:9: error: multiple else prongs in switch expression
17// :4:9: note: previous else prong here33// :4:9: note: previous else prong here
34// :12:9: error: multiple else prongs in switch expression
35// :11:9: note: previous else prong here
36// :19:9: error: multiple else prongs in switch expression
37// :18:9: note: previous else prong here
test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig created+32
...@@ -0,0 +1,32 @@
1fn foo(x: u2) void {
2 const y: Error!u2 = x;
3 if (y) |_| {} else |e| switch (e) {
4 error.Foo => {},
5 error.Bar => {},
6 error.Baz => {},
7 else => {},
8 }
9}
10
11fn bar(x: u2) void {
12 const y: Error!u2 = x;
13 y catch |e| switch (e) {
14 error.Foo => {},
15 error.Bar => {},
16 error.Baz => {},
17 else => {},
18 };
19}
20
21const Error = error{ Foo, Bar, Baz };
22
23export fn entry() usize {
24 return @sizeOf(@TypeOf(&foo)) + @sizeOf(@TypeOf(&bar));
25}
26
27// error
28// backend=stage2
29// target=native
30//
31// :7:14: error: unreachable else prong; all cases already handled
32// :17:14: error: unreachable else prong; all cases already handled
test/cases/compile_errors/switch_on_error_union_discard.zig created+12
...@@ -0,0 +1,12 @@
1export fn entry() void {
2 const x: error{}!u32 = 0;
3 if (x) |v| v else |_| switch (_) {
4 }
5}
6
7
8// error
9// backend=stage2
10// target=native
11//
12// :3:24: error: discard of error capture; omit it instead
test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig created+20
...@@ -0,0 +1,20 @@
1const Error = error{M};
2
3export fn entry() void {
4 const f: Error!void = void{};
5 if (f) {} else |e| switch (e) {}
6}
7
8export fn entry2() void {
9 const f: Error!void = void{};
10 f catch |e| switch (e) {};
11}
12
13// error
14// backend=stage2
15// target=native
16//
17// :5:24: error: switch must handle all possibilities
18// :5:24: note: unhandled error value: 'error.M'
19// :10:17: error: switch must handle all possibilities
20// :10:17: note: unhandled error value: 'error.M'