authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-07 20:50:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-07 20:50:57-07:00
loga62e19ec8ea4afcaf31a27dd32fab195a12a4877
treede4ceb25900afe970aa6b46750d178478bdf4b10
parent12087d4cbaab39acadc29716e92765c92b92e28c

AstGen: fix incorrect source loc for duplicate enum tag


2 files changed, 47 insertions(+), 4 deletions(-)

src/AstGen.zig+4-4
...@@ -1999,14 +1999,14 @@ fn containerDecl(...@@ -1999,14 +1999,14 @@ fn containerDecl(
1999 // don't need to waste time with a hash map.1999 // don't need to waste time with a hash map.
2000 const bad_node = for (container_decl.ast.members) |other_member_node| {2000 const bad_node = for (container_decl.ast.members) |other_member_node| {
2001 const other_member = switch (node_tags[other_member_node]) {2001 const other_member = switch (node_tags[other_member_node]) {
2002 .container_field_init => tree.containerFieldInit(member_node),2002 .container_field_init => tree.containerFieldInit(other_member_node),
2003 .container_field_align => tree.containerFieldAlign(member_node),2003 .container_field_align => tree.containerFieldAlign(other_member_node),
2004 .container_field => tree.containerField(member_node),2004 .container_field => tree.containerField(other_member_node),
2005 else => unreachable, // We checked earlier.2005 else => unreachable, // We checked earlier.
2006 };2006 };
2007 const other_tag_name = try mod.identifierTokenStringTreeArena(2007 const other_tag_name = try mod.identifierTokenStringTreeArena(
2008 scope,2008 scope,
2009 name_token,2009 other_member.ast.name_token,
2010 tree,2010 tree,
2011 arena,2011 arena,
2012 );2012 );
test/stage2/cbe.zig+43
...@@ -631,6 +631,49 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -631,6 +631,49 @@ pub fn addCases(ctx: *TestContext) !void {
631 ":6:5: error: redundant non-exhaustive enum mark",631 ":6:5: error: redundant non-exhaustive enum mark",
632 ":3:5: note: other mark here",632 ":3:5: note: other mark here",
633 });633 });
634
635 case.addError(
636 \\const E1 = enum {
637 \\ a,
638 \\ b,
639 \\ c,
640 \\ _ = 10,
641 \\};
642 \\export fn foo() void {
643 \\ const x = E1.a;
644 \\}
645 , &.{
646 ":5:9: error: '_' is used to mark an enum as non-exhaustive and cannot be assigned a value",
647 });
648
649 case.addError(
650 \\const E1 = enum {};
651 \\export fn foo() void {
652 \\ const x = E1.a;
653 \\}
654 , &.{
655 ":1:12: error: enum declarations must have at least one tag",
656 });
657
658 case.addError(
659 \\const E1 = enum { a, b, _ };
660 \\export fn foo() void {
661 \\ const x = E1.a;
662 \\}
663 , &.{
664 ":1:12: error: non-exhaustive enum missing integer tag type",
665 ":1:25: note: marked non-exhaustive here",
666 });
667
668 case.addError(
669 \\const E1 = enum { a, b, c, b, d };
670 \\export fn foo() void {
671 \\ const x = E1.a;
672 \\}
673 , &.{
674 ":1:28: error: duplicate enum tag",
675 ":1:22: note: other tag here",
676 });
634 }677 }
635678
636 ctx.c("empty start function", linux_x64,679 ctx.c("empty start function", linux_x64,