From 174a889364d285e32534016f7425e8adaf9cab3c Mon Sep 17 00:00:00 2001 From: John Schmidt Date: Sun, 3 Apr 2022 14:01:06 +0200 Subject: [PATCH] sema: add compile error for duplicate union field --- src/Sema.zig | 17 ++++++++++++++++- .../stage2/union_duplicate_field_definition.zig | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/compile_errors/stage2/union_duplicate_field_definition.zig diff --git a/src/Sema.zig b/src/Sema.zig index fb37f3f1ba..8744548892 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22215,7 +22215,22 @@ fn semaUnionFields(block: *Block, mod: *Module, union_obj: *Module.Union) Compil } const gop = union_obj.fields.getOrPutAssumeCapacity(field_name); - assert(!gop.found_existing); + if (gop.found_existing) { + const msg = msg: { + const tree = try sema.getAstTree(&block_scope); + const field_src = enumFieldSrcLoc(decl, tree.*, union_obj.node_offset, field_i); + const msg = try sema.errMsg(&block_scope, field_src, "duplicate union field: '{s}'", .{field_name}); + errdefer msg.destroy(gpa); + + const prev_field_index = union_obj.fields.getIndex(field_name).?; + const prev_field_src = enumFieldSrcLoc(decl, tree.*, union_obj.node_offset, prev_field_index); + try sema.mod.errNoteNonLazy(prev_field_src.toSrcLoc(decl), msg, "other field here", .{}); + try sema.errNote(&block_scope, src, msg, "union declared here", .{}); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(&block_scope, msg); + } + gop.value_ptr.* = .{ .ty = try field_ty.copy(decl_arena_allocator), .abi_align = 0, diff --git a/test/compile_errors/stage2/union_duplicate_field_definition.zig b/test/compile_errors/stage2/union_duplicate_field_definition.zig new file mode 100644 index 0000000000..6ad2ae4f4e --- /dev/null +++ b/test/compile_errors/stage2/union_duplicate_field_definition.zig @@ -0,0 +1,15 @@ +const U = union { + foo: u32, + foo: u32, +}; + +export fn entry() void { + const u: U = .{ .foo = 100 }; + _ = u; +} + +// duplicate union field name +// +// :3:5: error: duplicate union field: 'foo' +// :2:5: note: other field here +// :1:11: note: union declared here