zig

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

commit 332a43858a6ba2128d3df9e6bca45a10547ed383 (tree)
parent aadd1b252e1d912e45cc924a15872d7c3d1f9080
Author: Veikka Tuominen <git@vexu.eu>
Date:   Mon,  2 Jan 2023 17:06:06 +0200

Sema: `@intToEnum` on non-exhaustive enum at comptime should check int is in range

Closes #14155

Diffstat:
Msrc/Sema.zig | 23+++++++++++++++++++++--
Atest/cases/compile_errors/intToEnum_on_non-exhaustive_enums_checks_int_in_range.zig | 11+++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)

diff --git 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 @@ -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