diff options
| author | kkHAIKE <kkhaike@gmail.com> | 2022-09-27 18:23:51 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-27 13:23:51 +0300 |
| commit | ba5cbea0c3c6ee2e22606365edde18342fcf0579 (patch) | |
| tree | 1c3f9c6581391abd525ac54bf55b40ccc8fcfe1a /src/Sema.zig | |
| parent | f7f15e99c418ab0efac268e9087ce45d55744edc (diff) | |
| download | zig-ba5cbea0c3c6ee2e22606365edde18342fcf0579.tar.gz zig-ba5cbea0c3c6ee2e22606365edde18342fcf0579.zip | |
Sema: fix segfault when union init with empty field
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 72a5cb518f..ad3bd355eb 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -15885,6 +15885,7 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE .Struct => return sema.structInitEmpty(block, obj_ty, src, src), .Array, .Vector => return sema.arrayInitEmpty(block, src, obj_ty), .Void => return sema.addConstant(obj_ty, Value.void), + .Union => return sema.fail(block, src, "union initializer must initialize one field", .{}), else => return sema.failWithArrayInitNotSupported(block, src, obj_ty), } } @@ -25854,14 +25855,19 @@ fn coerceAnonStructToUnion( inst_src: LazySrcLoc, ) !Air.Inst.Ref { const inst_ty = sema.typeOf(inst); - const anon_struct = inst_ty.castTag(.anon_struct).?.data; - if (anon_struct.types.len != 1) { + const field_count = inst_ty.structFieldCount(); + if (field_count != 1) { const msg = msg: { - const msg = try sema.errMsg( + const msg = if (field_count > 1) try sema.errMsg( block, inst_src, "cannot initialize multiple union fields at once, unions can only have one active field", .{}, + ) else try sema.errMsg( + block, + inst_src, + "union initializer must initialize one field", + .{}, ); errdefer msg.destroy(sema.gpa); @@ -25874,6 +25880,7 @@ fn coerceAnonStructToUnion( return sema.failWithOwnedErrorMsg(msg); } + const anon_struct = inst_ty.castTag(.anon_struct).?.data; const field_name = anon_struct.names[0]; const init = try sema.structFieldVal(block, inst_src, inst, field_name, inst_src, inst_ty); return sema.unionInit(block, init, inst_src, union_ty, union_ty_src, field_name, inst_src); |
