On Zig 0.16.0 the following does not compile:
pub const A = struct {
x: [4]u8 = @import("std").mem.zeroes([4]u8),
};
pub fn main() !void {
var a: A = .{};
const b: [*c]A = &a;
b.*.x[0] = 10; // error: expected type '[4]u8', found 'comptime_int'
b[0].x[0] = 10; // This works.
b.*.x[0][0] = 10; // This works? And the first index is into an array of length 4...
}
This was fixed since then and I was wondering if someone could help find the commit where it was fixed, as I am interested. I have been looking for a while but am not sure exactly what to look for.
Edit:
I’ve found Sema: fix incorrect element pointer type of c pointer array by timholzhey · Pull Request #25556 · ziglang/zig · GitHub which demonstrates similar errors.
Welcome to Ziggit!
It was fixed in this PR:
https://codeberg.org/ziglang/zig/pulls/35749
and in this follow-up PR:
https://codeberg.org/ziglang/zig/pulls/35851
The fix was basically to guarantee for references to dereferenced memory locations (i.e. the array access, which takes a reference to the array and applies an offset to it, and the .* dereference) to always be single-item pointers.
Before these kinds of references were c pointers if the dereferenced pointer was also a c pointer (the dereferenced pointer was just passed through) which was not accounted for in the compiler logic that analyzed the array access. That logic then wrongly treated the resulting [*c][n]T pointer like a many-item pointer [*][n]T rather than a single-item pointer *[n]T (as c pointers can act as both) and indexed into the many-item pointer rather than into the pointed-to array.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.