Error: slice of pointer must include end value

Hi,
have anybody encountered such compiler error?

$ /opt/zig/zig build-exe server.zig 
server-sm/worker.zig:2:1: error: slice of pointer must include end value
const std = @import("std");
^~~~~
server-sm/worker.zig:2:1: error: slice of pointer must include end value
const std = @import("std");

There is no clear indication of what line of source files caused this error :frowning:
P.S. Total source is quite big (and unfinished), so I do not want to post it right now.

Minimal example

const AnObject = struct {
    f1: i32 = 0,
    f3: i32 = 0,
};

pub fn main() void {
    var o = AnObject{};
    var ptr = @ptrCast([*]u8, @alignCast(@alignOf([*]u8), &o));
    var slice = ptr[0..];
    _ = slice;
}
$ /opt/zig/zig build-exe a.zig 
a.zig:2:1: error: slice of pointer must include end value
const AnObject = struct {
^~~~~

filed bug report

I think one problem is that you’re getting incorrect line numbers in the compiler error. I’m using an older version of Zig (0.9.0) and the error is the same but on this line:

    var slice = ptr[0..];

The error makes sense on this line, because you can’t slice a raw multi-pointer without specifying the end of the range, because the length of the raw multi-pointer is unknown.

If I add the end value of the range ([0..3] for example) then it compiles.

2 Likes

AFAIK 0.9.0 uses stage1 compiler

$ /opt/zig/zig version
0.10.0-dev.4212+34835bbbc

$ /opt/zig/zig build-exe a.zig -fstage1
./a.zig:10:20: error: slice of pointer must include end value
    var slice = ptr[0..];

Right, so you need to add the end value of the range [0…SOMETHING_HERE]. The ptr is [*] which is not a slice so it doesn’t include the length. So you have to specify the length yourself when creating a slice from the ptr.