Zig: Global array passed as slice to function have unexpected address

Created on 30 Sep 2018  路  2Comments  路  Source: ziglang/zig

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
bug

Most helpful comment

Seems like this is not a problem anymore on master. Should I add a regression test and close this?

All 2 comments

Seems like this is not a problem anymore on master. Should I add a regression test and close this?

Sounds good to me

Was this page helpful?
0 / 5 - 0 ratings