Compile and run example program with zig run ptrcast_opt_slice_to_manyptr_error.zigptrcast_opt_slice_to_manyptr_error.zig:
pub fn main() void {
var x: ?[]u8 = &.{};
var y: [*]u8 = undefined;
x = null;
y = @ptrCast(x);
if (@intFromPtr(y) == 0) {
@import("std").debug.print("value is zero\n", .{});
}
}
Output:
zig run ptrcast_opt_slice_to_manyptr_error.zig
value is zero
Expected Behavior
The program above should panic at runtime with panic ID cast_to_null.
Running an equivalent program at compile time will yield the expected outcome.
Compile example program with zig build-obj ptrcast_opt_slice_to_manyptr_comptime.zigptrcast_opt_slice_to_manyptr_comptime.zig:
comptime {
var x: ?[]u8 = &.{};
var y: [*]u8 = undefined;
x = null;
y = @ptrCast(x);
}
Output:
zig build-obj ptrcast_opt_slice_to_manyptr_comptime.zig
ptrcast_opt_slice_to_manyptr_comptime.zig:6:18: error: null pointer casted to type '[*]u8'
y = @ptrCast(x);
^
Zig Version
0.14.0-dev.1411+a670f5519
Steps to Reproduce and Observed Behavior
Compile and run example program with
zig run ptrcast_opt_slice_to_manyptr_error.zig
ptrcast_opt_slice_to_manyptr_error.zig
:Output:
Expected Behavior
The program above should panic at runtime with panic ID
cast_to_null
.Running an equivalent program at compile time will yield the expected outcome.
Compile example program with
zig build-obj ptrcast_opt_slice_to_manyptr_comptime.zig
ptrcast_opt_slice_to_manyptr_comptime.zig
:Output: