zig/test/cases/safety/@enumFromInt truncated bits - exhaustive.zig
mlugg b01d6b156c compiler: add intcast_safe AIR instruction
This instruction is like `intcast`, but includes two safety checks:

* Checks that the int is in range of the destination type
* If the destination type is an exhaustive enum, checks that the int
  is a named enum value

This instruction is locked behind the `safety_checked_instructions`
backend feature; if unsupported, Sema will emit a fallback, as with
other safety-checked instructions.

This instruction is used to add a missing safety check for `@enumFromInt`
truncating bits. This check also has a fallback for backends which do
not yet support `safety_checked_instructions`.

Resolves: #21946
2025-01-30 14:47:59 +00:00

24 lines
466 B
Zig

const std = @import("std");
pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
if (std.mem.eql(u8, message, "invalid enum value")) {
std.process.exit(0);
}
std.process.exit(1);
}
pub fn main() u8 {
var num: i5 = undefined;
num = 14;
const E = enum(u3) { a, b, c, d, e, f, g, h };
const invalid: E = @enumFromInt(num);
_ = invalid;
return 1;
}
// run
// backend=llvm
// target=native