Zig Version
.17.0-dev.813+2153f8143
Steps to Reproduce, Observed Behavior, and Expected Behavior
This came up while I was drilling some random leetcode problems to stay sharp. I figured it'd be nice to have Zig generate a 1MiB array of i32, so reached for std.Random and found the .array method.
const std = @import("std");
const Io = std.Io;
pub fn main(init: std.process.Init) !void {
_ = init;
var source = std.Random.Xoroshiro128.init(12345);
const rng = source.random();
const data: [1024 * 1024]i32 = rng.array(i32, 1024 * 1024);
std.debug.print("This references it: {d}", .{data[0]});
}
Trying to compile this leads to a type-level error:
X lizardbox zig-stuff/two-sum-2 % zig-0.17 build run
run
└─ run exe two_sum_2
└─ install
└─ install two_sum_2
└─ compile exe two_sum_2 Debug native 1 errors
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: error: expected type '[]u8', found '*[1048576]i32'
bytes(r, &result);
^~~~~~~
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: note: pointer type child 'i32' cannot cast into pointer type child 'u8'
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: note: unsigned 8-bit int cannot represent all possible signed 32-bit values
/home/adam/.local/zig-dev/lib/std/Random.zig:72:30: note: parameter type declared here
pub fn bytes(r: Random, buf: []u8) void {
^~~~
referenced by:
main: src/main.zig:9:45
callMain [inlined]: /home/adam/.local/zig-dev/lib/std/start.zig:778:30
callMainWithArgs [inlined]: /home/adam/.local/zig-dev/lib/std/start.zig:680:20
posixCallMainAndExit: /home/adam/.local/zig-dev/lib/std/start.zig:632:38
2 reference(s) hidden; use '-freference-trace=6' to see all references
The issue seems to be with https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Random.zig#L78 in specific. InKryption on Discord thinks a @ptrCast can solve this.
### Zig Version
.17.0-dev.813+2153f8143
### Steps to Reproduce, Observed Behavior, and Expected Behavior
This came up while I was drilling some random leetcode problems to stay sharp. I figured it'd be nice to have Zig generate a 1MiB array of i32, so reached for `std.Random` and found the `.array` method.
```
const std = @import("std");
const Io = std.Io;
pub fn main(init: std.process.Init) !void {
_ = init;
var source = std.Random.Xoroshiro128.init(12345);
const rng = source.random();
const data: [1024 * 1024]i32 = rng.array(i32, 1024 * 1024);
std.debug.print("This references it: {d}", .{data[0]});
}
```
Trying to compile this leads to a type-level error:
```
X lizardbox zig-stuff/two-sum-2 % zig-0.17 build run
run
└─ run exe two_sum_2
└─ install
└─ install two_sum_2
└─ compile exe two_sum_2 Debug native 1 errors
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: error: expected type '[]u8', found '*[1048576]i32'
bytes(r, &result);
^~~~~~~
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: note: pointer type child 'i32' cannot cast into pointer type child 'u8'
/home/adam/.local/zig-dev/lib/std/Random.zig:78:14: note: unsigned 8-bit int cannot represent all possible signed 32-bit values
/home/adam/.local/zig-dev/lib/std/Random.zig:72:30: note: parameter type declared here
pub fn bytes(r: Random, buf: []u8) void {
^~~~
referenced by:
main: src/main.zig:9:45
callMain [inlined]: /home/adam/.local/zig-dev/lib/std/start.zig:778:30
callMainWithArgs [inlined]: /home/adam/.local/zig-dev/lib/std/start.zig:680:20
posixCallMainAndExit: /home/adam/.local/zig-dev/lib/std/start.zig:632:38
2 reference(s) hidden; use '-freference-trace=6' to see all references
```
The issue seems to be with https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Random.zig#L78 in specific. InKryption on Discord thinks a @ptrCast can solve this.