#`undefined` pointer address (cause: loops have `else` branches)

1 messages · Page 1 of 1 (latest)

unreal egret
junior flower
#

instead of screenshots, paste the text between lines with ``` to format them
you can provide a language at the start to control the highlighting, since discord does not support zig we usually use rust or typescript, e.g ```rust

do that for both code and terminal output, it makes it easier to read and copy for our own testing

#

it is preferable if you paste the relevant code in addition to linking the repo

#

especially since rn, codeberg is having issues lol

unreal egret
#

Yes, I'm sorry.

Here's relevant code (edit: poorly higtlightable, so I resent it below without backticks):

#

And the console output:

error: 'iter.decltest.Chain' terminated with signal ABRT with stderr:
       iters is a tuple
       Maybe iter: .{ .item = i32@aaaaaaaaaaaaaaaa }
       Iter: .{ .item = i32@aaaaaaaaaaaaaaaa }
       Item: i32@aaaaaaaaaaaaaaaa
       General protection exception (no address available)
       /home/danik/.zvm/0.16.0/lib/std/testing.zig:798:52: 0x123a15d in expectEqualDeepInner__anon_43639 (std.zig)
                               else => try expectEqualDeep(expected.*, actual.*),
                                                          ^
       /home/danik/.zvm/0.16.0/lib/std/testing.zig:741:32: 0x1239650 in expectEqualDeep (std.zig)
           return expectEqualDeepInner(T, expected, actual);
                                      ^
       /home/danik/.zvm/0.16.0/lib/std/testing.zig:741:32: 0x124275b in expectEqualDeep (root.zig)
           return expectEqualDeepInner(T, expected, actual);
                                      ^
       /home/danik/.zvm/0.16.0/lib/compiler/test_runner.zig:140:68: 0x11ed5d7 in mainServer (test_runner.zig)
                       const status: TestResults.Status = if (test_fn.func()) |v| s: {
                                                                          ^
       /home/danik/.zvm/0.16.0/lib/compiler/test_runner.zig:71:26: 0x11ee3fd in main (test_runner.zig)
               return mainServer(init) catch |err| panic("internal test runner failure: {t}", .{err});
                                ^
       /home/danik/.zvm/0.16.0/lib/std/start.zig:699:88: 0x11eac65 in callMain (std.zig)
           if (fn_info.params[0].type.? == std.process.Init.Minimal) return wrapMain(root.main(.{
                                                                                              ^
       /home/danik/.zvm/0.16.0/lib/std/start.zig:190:5: 0x11ea641 in _start (std.zig)
           asm volatile (switch (native_arch) {
           ^
unreal egret
#

I found the bug, and it seems to be the compiler bug. The Chain's initialization loop did not run when being written as an expression:

pub fn init(iters: Iters) Self {
    var opt_iters: OptionalIters = undefined;
    if (@typeInfo(Iters) == .array) for (iters, &opt_iters) |i, *oi| {
        oi.* = i;
    } else inline for (iters, &opt_iters) |i, *oi| {
        oi.* = i;
    };
    return .{ .iters = opt_iters };
}

But this works:

pub fn init(iters: Iters) Self {
    var opt_iters: OptionalIters = undefined;
    if (@typeInfo(Iters) == .array) {
        for (iters, &opt_iters) |i, *oi| {
            oi.* = i;
        }
    } else {
        inline for (iters, &opt_iters) |i, *oi| {
            oi.* = i;
        }
    }
    return .{ .iters = opt_iters };
}
junior flower
#

not a compiler bug, the else was a part of the loop instead of the if.
so when the condition was false the loop did not run at all.

loops can have an else branch that is run when the loop ends, but if you break it wont run

unreal egret
#

And to be more precise --- the issue is with the inline loop only

#

ok, that's confusing

junior flower
#

its really nice though :3
you just have to be careful when combining constructs that have else
the easiest thing is to always use blocks outside of trivial expressions

#

you can also go the other direction and ommit blocks from the loops too hyperandrew

unreal egret
#

Yes, I like the idea of the else branch for loops as well, but in this particular case, I completely forgot about their existence in zig and for the life of me couldn't figure out what was going on

#

pretty uncommon feature, so yeah