From 332a43858a6ba2128d3df9e6bca45a10547ed383 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 2 Jan 2023 17:06:06 +0200 Subject: [PATCH] Sema: `@intToEnum` on non-exhaustive enum at comptime should check int is in range Closes #14155 --- src/Sema.zig | 23 +++++++++++++++++-- ...n-exhaustive_enums_checks_int_in_range.zig | 11 +++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/cases/compile_errors/intToEnum_on_non-exhaustive_enums_checks_int_in_range.zig diff --git a/src/Sema.zig b/src/Sema.zig index 05ea86c387..1e12486bde 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2536,6 +2536,9 @@ fn coerceResultPtr( .wrap_errunion_payload => { new_ptr = try sema.analyzeErrUnionPayloadPtr(block, src, new_ptr, false, true); }, + .array_to_slice => { + return sema.fail(block, src, "TODO coerce_result_ptr array_to_slice", .{}); + }, else => { if (std.debug.runtime_safety) { std.debug.panic("unexpected AIR tag for coerce_result_ptr: {}", .{ @@ -7839,7 +7842,23 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A if (try sema.resolveMaybeUndefVal(operand)) |int_val| { if (dest_ty.isNonexhaustiveEnum()) { - return sema.addConstant(dest_ty, int_val); + var buffer: Type.Payload.Bits = undefined; + const int_tag_ty = dest_ty.intTagType(&buffer); + if (try sema.intFitsInType(int_val, int_tag_ty, null)) { + return sema.addConstant(dest_ty, int_val); + } + const msg = msg: { + const msg = try sema.errMsg( + block, + src, + "int value '{}' out of range of non-exhaustive enum '{}'", + .{ int_val.fmtValue(sema.typeOf(operand), sema.mod), dest_ty.fmt(sema.mod) }, + ); + errdefer msg.destroy(sema.gpa); + try sema.addDeclaredHereNote(msg, dest_ty); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(msg); } if (int_val.isUndef()) { return sema.failWithUseOfUndef(block, operand_src); @@ -32886,7 +32905,7 @@ fn enumHasInt( int: Value, ) CompileError!bool { switch (ty.tag()) { - .enum_nonexhaustive => return sema.intFitsInType(int, ty, null), + .enum_nonexhaustive => unreachable, .enum_full => { const enum_full = ty.castTag(.enum_full).?.data; const tag_ty = enum_full.tag_ty; diff --git a/test/cases/compile_errors/intToEnum_on_non-exhaustive_enums_checks_int_in_range.zig b/test/cases/compile_errors/intToEnum_on_non-exhaustive_enums_checks_int_in_range.zig new file mode 100644 index 0000000000..b05c9f35d9 --- /dev/null +++ b/test/cases/compile_errors/intToEnum_on_non-exhaustive_enums_checks_int_in_range.zig @@ -0,0 +1,11 @@ +pub export fn entry() void { + const E = enum(u3) { a, b, c, _ }; + @compileLog(@intToEnum(E, 100)); +} + +// error +// target=native +// backend=stage2 +// +// :3:17: error: int value '100' out of range of non-exhaustive enum 'tmp.entry.E' +// :2:15: note: enum declared here