authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-30 16:10:15+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-01 09:29:13+03:00
log6d24c40b6e79176b05ec735adcecfd346b03f059
tree26c5240abbadf123a98c0077b4521e9c61a4c2fc
parent902dc8c721c762bc5d1b9786bad47b21da45042c

Sema: improve bitcast to enum error


3 files changed, 28 insertions(+), 14 deletions(-)

src/Sema.zig+16-4
...@@ -7617,11 +7617,11 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -7617,11 +7617,11 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
7617 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;7617 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
76187618
7619 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);7619 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
7620 const operand = try sema.resolveInst(extra.rhs);
7620 switch (dest_ty.zigTypeTag()) {7621 switch (dest_ty.zigTypeTag()) {
7621 .AnyFrame,7622 .AnyFrame,
7622 .ComptimeFloat,7623 .ComptimeFloat,
7623 .ComptimeInt,7624 .ComptimeInt,
7624 .Enum,
7625 .EnumLiteral,7625 .EnumLiteral,
7626 .ErrorSet,7626 .ErrorSet,
7627 .ErrorUnion,7627 .ErrorUnion,
...@@ -7634,7 +7634,21 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -7634,7 +7634,21 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
7634 .Type,7634 .Type,
7635 .Undefined,7635 .Undefined,
7636 .Void,7636 .Void,
7637 => return sema.fail(block, dest_ty_src, "invalid type '{}' for @bitCast", .{dest_ty.fmt(sema.mod)}),7637 => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}'", .{dest_ty.fmt(sema.mod)}),
7638
7639 .Enum => {
7640 const msg = msg: {
7641 const msg = try sema.errMsg(block, dest_ty_src, "cannot @bitCast to '{}'", .{dest_ty.fmt(sema.mod)});
7642 errdefer msg.destroy(sema.gpa);
7643 switch (sema.typeOf(operand).zigTypeTag()) {
7644 .Int, .ComptimeInt => try sema.errNote(block, dest_ty_src, msg, "use @intToEnum for type coercion", .{}),
7645 else => {},
7646 }
7647
7648 break :msg msg;
7649 };
7650 return sema.failWithOwnedErrorMsg(block, msg);
7651 },
76387652
7639 .Pointer => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', use @ptrCast to cast to a pointer", .{7653 .Pointer => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', use @ptrCast to cast to a pointer", .{
7640 dest_ty.fmt(sema.mod),7654 dest_ty.fmt(sema.mod),
...@@ -7658,8 +7672,6 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -7658,8 +7672,6 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
7658 .Vector,7672 .Vector,
7659 => {},7673 => {},
7660 }7674 }
7661
7662 const operand = try sema.resolveInst(extra.rhs);
7663 return sema.bitCast(block, dest_ty, operand, operand_src);7675 return sema.bitCast(block, dest_ty, operand, operand_src);
7664}7676}
76657677
test/cases/compile_errors/bitCast_to_enum_type.zig created+12
...@@ -0,0 +1,12 @@
1export fn entry() void {
2 const E = enum(u32) { a, b };
3 const y = @bitCast(E, @as(u32, 3));
4 _ = y;
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :3:24: error: cannot @bitCast to 'tmp.entry.E'
12// :3:24: note: use @intToEnum for type coercion
test/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig deleted-10
...@@ -1,10 +0,0 @@
1export fn entry() void {
2 const y = @bitCast(enum(u32) { a, b }, @as(u32, 3));
3 _ = y;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:24: error: cannot cast a value of type 'y'