zig
zig copied to clipboard
Inconsistant error message passing array literal to slice.
Zig Version
0.13.0-dev.44+9d64332a5
Steps to Reproduce and Observed Output
Assuming something like:
const an_array: [4]u32 = .{1, 2, 3, 4};
fn someFunc([]const u32) void { ... }
The calling
someFunc(.{1, 2, 3, 4}); // note missing "&".
gives error message "error: type '[]const u32' does not support array initialization syntax", but
someFunc(an_array); // also missing "&"
gives error message "error: array literal requires address-of operator (&) to coerce to slice type '[]const u32'".
Expected Output
Both should give the same message about the missing '&'.
I think the error messages being distinct makes sense, since .{1, 2, 3, 4}
is an anonymous aggregate initializer, not an array itself.
It can initialize arrays, but also vectors and tuples, however not slices, as the error message points out.
Usually Zig tries not to give users recommendations about how to fix their code, since those might be against the original author's intentions.
In this case the option of address-of &
would be uncontroversial, however as soon as you mix in runtime values
the backing lifetime would have to be bound to the current scope and the user might want/need to allocate the memory instead.
(This detail may belong into a separate discussion though, since the compiler could disallow this usage.)
As for the second error message, I think it would make more sense to talk about an "array value" rather than an "array literal". The only literal is the one initializing the constant, which seems no longer at this point.
I just like the second because it tells me "you forgot the & again!" Actually, from a purely semantic point of view, I don't like the address-of operator in either case. The function parameter is not a pointer, so having to pass a pointer in doesn't really make sense to me. A slice is a location and a length and an array has a location and a length. But, I'm new to Zig, so don't fully understand the Zen of it yet. Perhaps that will come in time.
This is incorrect; the function parameter is slice, which is a pointer. Please read the langref a bit more.
I did read that. It says that slices are related to pointers, not that they are pointers.
It also says “Use &s to obtain a single item pointer”, not to obtain a slice; and “we prefer slices to pointers” which also indicates that slices aren’t pointers.
I do understand that the implementation of a slice contains a pointer; but, as I see it, a pointer indicate a specific point in memory where a slice is a span of memory, not the same things at all.
On 25 Apr 24, at 19:10, David Rubin @.***> wrote:
This is incorrect, the function parameter is slice, which is a pointer. Please read the langref a bit more https://ziglang.org/documentation/master/#Pointers.
— Reply to this email directly, view it on GitHub https://github.com/ziglang/zig/issues/19769#issuecomment-2078315235, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOAJTIVIKFXEPSDKDAY6EQLY7GEO7AVCNFSM6AAAAABGZWGSXWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZYGMYTKMRTGU. You are receiving this because you authored the thread.