authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-02-12 20:03:16+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-14 13:05:00+02:00
logee69a4b45f67e929fe5780ab6bf44360f6511d26
tree85599466d8bed51f04f210ed430940b248876fc9
parent3eb29f15f537ee79df8f2c4afa0db94ce6137d4c

stage2: improve compiler error message for bad union init


1 files changed, 18 insertions(+), 3 deletions(-)

src/Sema.zig+18-3
...@@ -2631,9 +2631,24 @@ fn validateUnionInit(...@@ -2631,9 +2631,24 @@ fn validateUnionInit(
2631 union_ptr: Air.Inst.Ref,2631 union_ptr: Air.Inst.Ref,
2632) CompileError!void {2632) CompileError!void {
2633 if (instrs.len != 1) {2633 if (instrs.len != 1) {
2634 // TODO add note for other field2634 const msg = msg: {
2635 // TODO add note for union declared here2635 const msg = try sema.errMsg(
2636 return sema.fail(block, init_src, "only one union field can be active at once", .{});2636 block,
2637 init_src,
2638 "cannot initialize multiple union fields at once, unions can only have one active field",
2639 .{},
2640 );
2641 errdefer msg.destroy(sema.gpa);
2642
2643 for (instrs[1..]) |inst| {
2644 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
2645 const inst_src: LazySrcLoc = .{ .node_offset_back2tok = inst_data.src_node };
2646 try sema.errNote(block, inst_src, msg, "additional initializer here", .{});
2647 }
2648 try sema.mod.errNoteNonLazy(union_obj.srcLoc(), msg, "union declared here", .{});
2649 break :msg msg;
2650 };
2651 return sema.failWithOwnedErrorMsg(msg);
2637 }2652 }
26382653
2639 const field_ptr = instrs[0];2654 const field_ptr = instrs[0];