Commit a62e19ec8e

Andrew Kelley <andrew@ziglang.org>
2021-04-08 05:50:57
AstGen: fix incorrect source loc for duplicate enum tag
1 parent 12087d4
Changed files (2)
src
test
stage2
src/AstGen.zig
@@ -1999,14 +1999,14 @@ fn containerDecl(
                             // don't need to waste time with a hash map.
                             const bad_node = for (container_decl.ast.members) |other_member_node| {
                                 const other_member = switch (node_tags[other_member_node]) {
-                                    .container_field_init => tree.containerFieldInit(member_node),
-                                    .container_field_align => tree.containerFieldAlign(member_node),
-                                    .container_field => tree.containerField(member_node),
+                                    .container_field_init => tree.containerFieldInit(other_member_node),
+                                    .container_field_align => tree.containerFieldAlign(other_member_node),
+                                    .container_field => tree.containerField(other_member_node),
                                     else => unreachable, // We checked earlier.
                                 };
                                 const other_tag_name = try mod.identifierTokenStringTreeArena(
                                     scope,
-                                    name_token,
+                                    other_member.ast.name_token,
                                     tree,
                                     arena,
                                 );
test/stage2/cbe.zig
@@ -631,6 +631,49 @@ pub fn addCases(ctx: *TestContext) !void {
             ":6:5: error: redundant non-exhaustive enum mark",
             ":3:5: note: other mark here",
         });
+
+        case.addError(
+            \\const E1 = enum {
+            \\    a,
+            \\    b,
+            \\    c,
+            \\    _ = 10,
+            \\};
+            \\export fn foo() void {
+            \\    const x = E1.a;
+            \\}
+        , &.{
+            ":5:9: error: '_' is used to mark an enum as non-exhaustive and cannot be assigned a value",
+        });
+
+        case.addError(
+            \\const E1 = enum {};
+            \\export fn foo() void {
+            \\    const x = E1.a;
+            \\}
+        , &.{
+            ":1:12: error: enum declarations must have at least one tag",
+        });
+
+        case.addError(
+            \\const E1 = enum { a, b, _ };
+            \\export fn foo() void {
+            \\    const x = E1.a;
+            \\}
+        , &.{
+            ":1:12: error: non-exhaustive enum missing integer tag type",
+            ":1:25: note: marked non-exhaustive here",
+        });
+
+        case.addError(
+            \\const E1 = enum { a, b, c, b, d };
+            \\export fn foo() void {
+            \\    const x = E1.a;
+            \\}
+        , &.{
+            ":1:28: error: duplicate enum tag",
+            ":1:22: note: other tag here",
+        });
     }
 
     ctx.c("empty start function", linux_x64,