This code fails:
const std = @import("std");
const a = []u8{1,2,3};
fn b(s: []const u8) void {
for (s) |*i, j| {
std.debug.assert(i == &a[j]);
}
}
test "" {
b(a);
}
test2.zig:7:25: 0x205248 in ??? (test)
std.debug.assert(i == &a[j]);
But it really shouldn't right? The slice s should really point to a, but it seems, from this code, that their address is different.
If we look in compiler explorer we can see, that for some reason there are two arrays in the binary.
.LBB2_3:
add rsp, 16
pop rbp
ret
a:
.ascii "\001\002\003"
.byte 0
__unnamed_3:
.ascii "\001\002\003"
__unnamed_1:
.quad __unnamed_3
.quad 3
Edit: Looking closer, it seems that b is called with __unamed_1 which is the slice of __unnamed_3 and not the slice of a.
square:
push rbp
mov rbp, rsp
lea rdi, [rip + __unnamed_1]
call b
pop rbp
ret
Seems like this is not a problem anymore on master. Should I add a regression test and close this?
Sounds good to me
Most helpful comment
Seems like this is not a problem anymore on master. Should I add a regression test and close this?