Commit 6bbc2cd59a

John Schmidt <john.schmidt.h@gmail.com>
2022-04-03 14:07:13
sema: add compile error for duplicate struct field
1 parent 174a889
Changed files (2)
src
test
compile_errors
src/Sema.zig
@@ -21945,7 +21945,21 @@ fn semaStructFields(
         }
 
         const gop = struct_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.*, struct_obj.node_offset, field_i);
+                const msg = try sema.errMsg(&block_scope, field_src, "duplicate struct field: '{s}'", .{field_name});
+                errdefer msg.destroy(gpa);
+
+                const prev_field_index = struct_obj.fields.getIndex(field_name).?;
+                const prev_field_src = enumFieldSrcLoc(decl, tree.*, struct_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, "struct 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,
test/compile_errors/stage2/struct_duplicate_field_name.zig
@@ -0,0 +1,15 @@
+const S = struct {
+    foo: u32,
+    foo: u32,
+};
+
+export fn entry() void {
+    const s: S = .{ .foo = 100 };
+    _ = s;
+}
+
+// duplicate struct field name
+//
+// :3:5: error: duplicate struct field: 'foo'
+// :2:5: note: other field here
+// :1:11: note: struct declared here