authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2023-07-12 08:37:42+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-11 23:37:42-07:00
log711b4e93e2b98233a5e225611891a0c25c6c12d9
tree8372859a4f721124b3725e5a46ac6b20819f5ac0
parentff0e2ab3983f0be241e02c1f37db795e5d02ed56
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fixes crash when a struct is given as the first parameter to the unionInit builtin (#16385)


2 files changed, 23 insertions(+), 0 deletions(-)

src/Sema.zig+9
...@@ -18727,6 +18727,15 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -18727,6 +18727,15 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
18727 const init_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };18727 const init_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node };
18728 const extra = sema.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data;18728 const extra = sema.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data;
18729 const union_ty = try sema.resolveType(block, ty_src, extra.union_type);18729 const union_ty = try sema.resolveType(block, ty_src, extra.union_type);
18730 if (union_ty.zigTypeTag(sema.mod) != .Union) {
18731 const msg = msg: {
18732 const msg = try sema.errMsg(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(sema.mod)});
18733 errdefer msg.destroy(sema.gpa);
18734 try sema.addDeclaredHereNote(msg, union_ty);
18735 break :msg msg;
18736 };
18737 return sema.failWithOwnedErrorMsg(msg);
18738 }
18730 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, "name of field being initialized must be comptime-known");18739 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, "name of field being initialized must be comptime-known");
18731 const init = try sema.resolveInst(extra.init);18740 const init = try sema.resolveInst(extra.init);
18732 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);18741 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);
test/cases/compile_errors/union_init_with_struct_as_first_param.zig created+14
...@@ -0,0 +1,14 @@
1const S = struct {
2 a: u8,
3};
4
5export fn u() void {
6 _ = @unionInit(S, "a", 5);
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :6:20: error: expected union type, found 'tmp.S'
14// :1:11: note: struct declared here