aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Schmidt <john.schmidt.h@gmail.com>2022-02-12 20:03:16 +0100
committerVeikka Tuominen <git@vexu.eu>2022-02-14 13:05:00 +0200
commitee69a4b45f67e929fe5780ab6bf44360f6511d26 (patch)
tree85599466d8bed51f04f210ed430940b248876fc9
parent3eb29f15f537ee79df8f2c4afa0db94ce6137d4c (diff)
downloadzig-ee69a4b45f67e929fe5780ab6bf44360f6511d26.tar.gz
zig-ee69a4b45f67e929fe5780ab6bf44360f6511d26.zip
stage2: improve compiler error message for bad union init
-rw-r--r--src/Sema.zig21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 7dbc36af37..480a6a1ca2 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -2631,9 +2631,24 @@ fn validateUnionInit(
union_ptr: Air.Inst.Ref,
) CompileError!void {
if (instrs.len != 1) {
- // TODO add note for other field
- // TODO add note for union declared here
- return sema.fail(block, init_src, "only one union field can be active at once", .{});
+ const msg = msg: {
+ const msg = try sema.errMsg(
+ block,
+ init_src,
+ "cannot initialize multiple union fields at once, unions can only have one active field",
+ .{},
+ );
+ errdefer msg.destroy(sema.gpa);
+
+ for (instrs[1..]) |inst| {
+ const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
+ const inst_src: LazySrcLoc = .{ .node_offset_back2tok = inst_data.src_node };
+ try sema.errNote(block, inst_src, msg, "additional initializer here", .{});
+ }
+ try sema.mod.errNoteNonLazy(union_obj.srcLoc(), msg, "union declared here", .{});
+ break :msg msg;
+ };
+ return sema.failWithOwnedErrorMsg(msg);
}
const field_ptr = instrs[0];