From a62e19ec8ea4afcaf31a27dd32fab195a12a4877 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 7 Apr 2021 20:50:57 -0700 Subject: [PATCH] AstGen: fix incorrect source loc for duplicate enum tag --- src/AstGen.zig | 8 ++++---- test/stage2/cbe.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 6aaa739a6f..a061f519ed 100644 --- a/src/AstGen.zig +++ b/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, ); diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index a8b663c61e..49a0ca686b 100644 --- a/test/stage2/cbe.zig +++ b/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,