zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6d24c40b6e79176b05ec735adcecfd346b03f059 (tree)
parent 902dc8c721c762bc5d1b9786bad47b21da45042c
Author: Veikka Tuominen <git@vexu.eu>
Date:   Thu, 30 Jun 2022 16:10:15 +0300

Sema: improve bitcast to enum error

Diffstat:
Msrc/Sema.zig | 20++++++++++++++++----
Atest/cases/compile_errors/bitCast_to_enum_type.zig | 12++++++++++++
Dtest/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig | 10----------
3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -7617,11 +7617,11 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs); + const operand = try sema.resolveInst(extra.rhs); switch (dest_ty.zigTypeTag()) { .AnyFrame, .ComptimeFloat, .ComptimeInt, - .Enum, .EnumLiteral, .ErrorSet, .ErrorUnion, @@ -7634,7 +7634,21 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air .Type, .Undefined, .Void, - => return sema.fail(block, dest_ty_src, "invalid type '{}' for @bitCast", .{dest_ty.fmt(sema.mod)}), + => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}'", .{dest_ty.fmt(sema.mod)}), + + .Enum => { + const msg = msg: { + const msg = try sema.errMsg(block, dest_ty_src, "cannot @bitCast to '{}'", .{dest_ty.fmt(sema.mod)}); + errdefer msg.destroy(sema.gpa); + switch (sema.typeOf(operand).zigTypeTag()) { + .Int, .ComptimeInt => try sema.errNote(block, dest_ty_src, msg, "use @intToEnum for type coercion", .{}), + else => {}, + } + + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); + }, .Pointer => return sema.fail(block, dest_ty_src, "cannot @bitCast to '{}', use @ptrCast to cast to a pointer", .{ dest_ty.fmt(sema.mod), @@ -7658,8 +7672,6 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air .Vector, => {}, } - - const operand = try sema.resolveInst(extra.rhs); return sema.bitCast(block, dest_ty, operand, operand_src); } diff --git a/test/cases/compile_errors/bitCast_to_enum_type.zig b/test/cases/compile_errors/bitCast_to_enum_type.zig @@ -0,0 +1,12 @@ +export fn entry() void { + const E = enum(u32) { a, b }; + const y = @bitCast(E, @as(u32, 3)); + _ = y; +} + +// error +// backend=stage2 +// target=native +// +// :3:24: error: cannot @bitCast to 'tmp.entry.E' +// :3:24: note: use @intToEnum for type coercion diff --git a/test/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig b/test/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig @@ -1,10 +0,0 @@ -export fn entry() void { - const y = @bitCast(enum(u32) { a, b }, @as(u32, 3)); - _ = y; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:24: error: cannot cast a value of type 'y'