What is the correct behavior when 0xffffffff is casted to i32 with @intCast?
According to the document, an undefined behavior should be detected.
Converts an integer to another integer while keeping the same numerical value. Attempting to convert a number which is out of range of the destination type results in safety-protected Undefined Behavior.
But, the following code does not detect an undefined behavior, but successfully converts 0xffffffff
to -1.
const std = @import("std");
var xxx: u32 = 0xffffffff;
test "" {
std.debug.warn("\n{}\n", .{ @intCast(i32, xxx) });
}
const std = @import("std");
pub fn main() void {
var x: u32 = std.math.maxInt(u32);
var y = @intCast(i32, x);
std.debug.print("{}\n", .{y}); // -1 :(
}
I was able to reproduce this with the latest master release. Runtime safety should cause a panic when this occurs, instead of setting y to the value of -1. This is a bug.
I'm working on this.
Most helpful comment
I'm working on this.