ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.63k stars 2.46k forks source link

inline function missing from stack/error trace #19407

Open Inve1951 opened 5 months ago

Inve1951 commented 5 months ago

Zig Version

0.12.0-dev.3381+7057bffc1

Steps to Reproduce and Observed Behavior

Scenario 1:

inline fn repro() void {
    @panic("");
}

pub fn main() void {
    repro();
}

Output:

$ zig run repro.zig 
thread 147441 panic: 
.../repro.zig:2:5: 0x1033fef in main (repro)
    @panic("");
    ^
.../lib/zig/std/start.zig:501:22: 0x1033899 in posixCallMainAndExit (repro)
            root.main();
                     ^
.../lib/zig/std/start.zig:253:5: 0x1033401 in _start (repro)
    asm volatile (switch (native_arch) {
    ^

Scenario 2:

inline fn repro() !void {
    try other();
}

fn other() !void {
    return error.Fail;
}

pub fn main() !void {
    try repro();
}

Output:

$ zig run repro.zig 
error: Fail
.../repro.zig:6:5: 0x1033c98 in other (repro)
    return error.Fail;
    ^
.../repro.zig:10:5: 0x1033dc7 in main (repro)
    try repro();
    ^

Expected Behavior

Expected output is as if the function wasn't inline.

Expected 1:

$ zig run repro.zig 
thread 147991 panic: 
.../repro.zig:2:5: 0x1035fdf in repro (repro)
    @panic("");
    ^
.../repro.zig:6:10: 0x1034008 in main (repro)
    repro();
         ^
.../lib/zig/std/start.zig:501:22: 0x10338c9 in posixCallMainAndExit (repro)
            root.main();
                     ^
.../zig/std/start.zig:253:5: 0x1033431 in _start (repro)
    asm volatile (switch (native_arch) {
    ^

Expected 2:

$ zig run repro.zig 
error: Fail
.../repro.zig:6:5: 0x1033cc8 in other (repro)
    return error.Fail;
    ^
.../repro.zig:2:5: 0x1033dd3 in repro (repro)
    try other();
    ^
.../repro.zig:10:5: 0x1033e13 in main (repro)
    try repro();
    ^
nektro commented 5 months ago

I thought this was intentional behavior?

barathrm commented 5 months ago

Possibly related/the same, but with the inline function being the inner function and zig not showing which line the error occurred at.

inline fn inner() !void {
    return error.InvalidVal;
}

fn outer() !void {
    try inner();
}

pub fn main() !void {
    try outer();
}
$ zig build-exe -O Debug main.zig
$ ./main
error: InvalidVal
<snip>/main.zig:6:5: 0x1033b86 in outer (main
)
    try inner();
    ^
<snip>/main.zig:10:5: 0x1033ca3 in main (main
)
    try outer();
    ^

Without marking inner() as inline:

error: InvalidVal
<snip>/main.zig:2:5: 0x1033ba8 in inner (main
)
    return error.InvalidVal;
    ^
<snip>/main.zig:6:5: 0x1033cb3 in outer (main
)
    try inner();
    ^
<snip>/main.zig:10:5: 0x1033cf3 in main (main
)
    try outer();
    ^

When I do something similiar (AFAICT) in C, compiling with clang, gdb can tell me just fine where the fault occurs.

This makes debugging failing tests where a big inlined function is involved unnecessarily cumbersome (imo), as zig build won't print the error which was returned. zig build only prints that the test failed and which inlined function triggers the error. zig test will print the error name at least, but I guess that's a separate issue ticket/matter:

$ zig-nightly build test -Doptimize=Debug -freference-trace
test
└─ run test 0/1 passed, 1 failed
error: 'main.test_0' failed: <snip>/zig-linux-x86_64-0.12.0-dev.3439+31a7f 22b8/lib/compiler/main.zig:6:5: 0x10396e6 in outer (test)
<snip>/zig-linux-x86_64-0.12.0-dev.3439+31a7f22b8/lib/compiler/main.zig:14:5: 0x1039803 in test_0 (test)
error: while executing test 'main.test_0', the following test command failed:
<snip>/zig-cache/o/3b3fafdd763ff5442eb3edb769
9bc0f6/test --listen=- 
Build Summary: 1/3 steps succeeded; 1 failed; 0/1 tests passed; 1 failed (disable with --summary none)
test transitive failure
└─ run test 0/1 passed, 1 failed
error: the following build command failed with exit code 1:
<snip>/zig-cache/o/d15bdc5fa3ae95c2118b5028b1
7ee5f2/build <snip>/zig-linux-x86_64-0.12.0-dev.3439+31a7f22b8/zig <snip>/.cache/zig --seed 0x2551e4f8 -Zf7a43c2259b1da74 test -Doptimize=Debug -freference-trace

Possibly related https://github.com/ziglang/zig/issues/18237