From 6d24c40b6e79176b05ec735adcecfd346b03f059 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 30 Jun 2022 16:10:15 +0300 Subject: [PATCH] Sema: improve bitcast to enum error --- src/Sema.zig | 20 +++++++++++++++---- .../compile_errors/bitCast_to_enum_type.zig | 12 +++++++++++ .../stage1/obj/bitCast_to_enum_type.zig | 10 ---------- 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 test/cases/compile_errors/bitCast_to_enum_type.zig delete mode 100644 test/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig diff --git a/src/Sema.zig b/src/Sema.zig index 862d5cbf48..08372bf7b3 100644 --- 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 new file mode 100644 index 0000000000..add531627f --- /dev/null +++ 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 deleted file mode 100644 index 4d63ab9e01..0000000000 --- a/test/cases/compile_errors/stage1/obj/bitCast_to_enum_type.zig +++ /dev/null @@ -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'