Skip to content

Error return trace incorrect when the function returning the error is called multiple times #21707

@quid256

Description

@quid256

Zig Version

0.14.0-dev.1820+ea527f7a8

Steps to Reproduce and Observed Behavior

$ touch main.zig

Then edit main.zig to contain

pub fn check_number(num: u8) !void {
    switch (num) {
        0 => return error.IsZero,
        10 => return error.IsTen,
        else => {},
    }
}

pub fn main() !void {
    const num1 = check_number(0);
    _ = num1 catch {} // discard error.IsZero

    const num2 = check_number(10);
    try num2;
}

Then

$ zig run main.zig
error: IsTen
.../main.zig:3:14: 0x10381e4 in check_number (main)
        0 => return error.IsZero,
             ^
.../main.zig:4:15: 0x10381f7 in check_number (main)
        10 => return error.IsTen,
              ^
.../main.zig:14:5: 0x10382d0 in main (main)
    try num2;
    ^

Expected Behavior

The first entry in the error return trace should have pointed me to test.zig:4:15, the line that generated the error that was returned from main. Instead, it pointed to test.zig:3:15, even though that error was generated by the first call to check_number and discarded.

(Sorry if I'm missing something obvious here -- I'm new to Zig)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions