From a1b952f4b03b7becad85ad47f96a75c0be620cf8 Mon Sep 17 00:00:00 2001 From: emekoi Date: Wed, 3 Jul 2019 13:12:14 -0500 Subject: [PATCH] added tests for #1107 and a note in the reference --- doc/langref.html.in | 6 ++++-- src/ir.cpp | 4 ++-- test/compile_errors.zig | 17 +++++++++++++++++ test/stage1/behavior/switch.zig | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 4bd594c113..3b8e77595c 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2973,6 +2973,7 @@ test "switch on tagged union" { A: u32, C: Point, D, + E: u32, }; var a = Item{ .C = Point{ .x = 1, .y = 2 } }; @@ -2980,8 +2981,9 @@ test "switch on tagged union" { // Switching on more complex enums is allowed. const b = switch (a) { // A capture group is allowed on a match, and will return the enum - // value matched. - Item.A => |item| item, + // value matched. If the payloads of both cases are the same + // they can be put into the same switch prong. + Item.A, Item.E => |item| item, // A reference to the matched value can be obtained using `*` syntax. Item.C => |*item| blk: { diff --git a/src/ir.cpp b/src/ir.cpp index d4cb5f90de..83155da4c1 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -19160,8 +19160,8 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru if (field->type_entry != payload) { if (!invalid_payload) { invalid_payload = ir_add_error(ira, &instruction->base, - buf_sprintf("switch prong contains cases with differing payloads")); - invalid_payload_list = buf_sprintf("types %s", buf_ptr(&field->type_entry->name)); + buf_sprintf("switch prong contains cases with different payloads")); + invalid_payload_list = buf_sprintf("payload types are %s", buf_ptr(&field->type_entry->name)); } if (i == instruction->prongs_len - 1) diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 94cd152eb7..592870c678 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -6048,4 +6048,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:5:30: error: expression value is ignored", "tmp.zig:9:30: error: expression value is ignored", ); + + cases.add( + "capture group on switch prong with different payloads", + \\const Union = union(enum) { + \\ A: usize, + \\ B: isize, + \\}; + \\comptime { + \\ var u = Union{ .A = 8 }; + \\ switch (u) { + \\ .A, .B => |e| unreachable, + \\ } + \\} + , + "tmp.zig:8:20: error: switch prong contains cases with different payloads", + "tmp.zig:8:20: note: payload types are usize and isize", + ); } diff --git a/test/stage1/behavior/switch.zig b/test/stage1/behavior/switch.zig index 12e026d0ba..2b7422fa6d 100644 --- a/test/stage1/behavior/switch.zig +++ b/test/stage1/behavior/switch.zig @@ -391,3 +391,21 @@ test "switch with null and T peer types and inferred result location type" { S.doTheTest(1); comptime S.doTheTest(1); } + +test "switch prongs with cases with identical payloads" { + const Union = union(enum) { + A: usize, + B: isize, + C: usize, + }; + const S = struct { + fn doTheTest(u: Union) void { + switch (u) { + .A, .C => |e| expect(@typeOf(e) == usize), + .B => |e| expect(@typeOf(e) == isize), + } + } + }; + S.doTheTest(Union{ .A = 8 }); + comptime S.doTheTest(Union{ .B = -8 }); +}