Breaking Zig Build while trying to run a thread using a method of a struct

Hi,

I break the Zig build… to which it does not provide rhyme or reason (or a good one anyway).

❯ zig build
install
└─ install keep-alive
   └─ zig build-exe keep-alive Debug native failure
error: the following command terminated unexpectedly:
/opt/zig-linux-x86_64-0.15.0-dev.208+8acedfd5b/zig build-exe -ODebug --dep utils --dep microsmart -Mroot=/home/anonymous/Laboratory/Zig/pascal-string/src/cmd/keep-alive.zig -ODebug -Mutils=/home/anonymous/Laboratory/Zig/pascal-string/src/utils/Assemble.zig -ODebug --dep utils -Mmicrosmart=/home/anonymous/Laboratory/Zig/pascal-string/src/microsmart/Assemble.zig --cache-dir /home/anonymous/Laboratory/Zig/pascal-string/.zig-cache --global-cache-dir /home/anonymous/.cache/zig --name keep-alive --zig-lib-dir /opt/zig-linux-x86_64-0.15.0-dev.208+8acedfd5b/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install keep-alive transitive failure
   └─ zig build-exe keep-alive Debug native failure
error: the following build command failed with exit code 1:
/home/anonymous/Laboratory/Zig/pascal-string/.zig-cache/o/c877d0d0d6381fd3f8e963ccc2c00249/build /opt/zig-linux-x86_64-0.15.0-dev.208+8acedfd5b/zig /opt/zig-linux-x86_64-0.15.0-dev.208+8acedfd5b/lib /home/anonymous/Laboratory/Zig/pascal-string /home/anonymous/Laboratory/Zig/pascal-string/.zig-cache /home/anonymous/.cache/zig --seed 0x774524a3 -Za2d3715e6aee1600

I have boiled it down to when I:

    const args = ThreadArgs{ .service = &acq };
    const threadConfig = std.Thread.SpawnConfig{
        .stack_size = 1024 * 16,
    };

    var th = try std.Thread.spawn(
        threadConfig,
        runner,
        .{ .ta = args},     //  <<< this line breaks the compiler
    ){};

When trying

    var th = try std.Thread.spawn(
        threadConfig,
        runner,
        .{ args},
    ){};

I get

src/cmd/keep-alive.zig:35:10: error: unable to evaluate comptime expression
        .{ args},

My question being: How does one run a “method” on a struct, that takes *Self as the receiver from a Thread spawn.

My observation being: That this breaks the compiler to the point that this could potentially be very tough to find from the error… Not going to make a meal of it, hope you as the reader will see how the error does not correlate at all to the failure. I know Zig is pre-release; but if this is not a known bug then maybe somebody can take notice thereof.

I have tested this against 0.14 and 0.15(latest). Same issue.

Regards and Thanks,

using this as the example

const T = struct {
    //....
    pub fn foo(t: *T) void {
    //...
    }
};

you would do this

const t = T {
    //...
    };
std.Thread.spawn(config, T.foo, .{&t});

t.foo() is just sugar for T.foo(&t)

regarding the compile error, prociding the source of runner would help but my guess is:
Thread.spawn eventualy gets to @call which calls the function with the provided args, it expects a tuple struct, that being a struct with no field names.
I think thats the cause of your wierd error, however my tests have always gotten a clear expected tuple struct error.

also your code shouldnt compile with {} after spawn, im guessing thats supposed the be a catch block

@vulpesx, thank you for your reply and confirmation. I have tried, before posting, every combination (including your suggestion) without success.

But with your confirmation I have figured out what I was doing wrong!!

I was so tired and fixated on the parameters that I did not notice the trailing {} after the spawn. I think that {} is also what is causing the compiler to break without any good feedback.

Fixing that allows the other issues to be reported by the compiler, which I fixed and everything working now. Big thanks again.