Skip to content

switch on error preserves error return trace #18591

Description

@Techatrix

Zig Version

0.12.0-dev.2139+e025ad7b4

Steps to Reproduce and Observed Behavior

Run the following code with zig run file.zig:

pubfnmain() !void {
tryfoo();
returnerror.FailTest;
}
fnfailure() error{ Fatal, NonFatal }!void {
returnerror.NonFatal;
}
fnfoo() error{Fatal}!void {
returnfailure() catch|err|switch (err) {
error.Fatal=>returnerror.Fatal,
error.NonFatal=>return,
};
}

This will yield the following error return trace:

error: FailTest
/run/media/techatrix/UserFiles/zls/sample.zig:7:5: 0x1025af8 in failure (sample)
return error.NonFatal;
^
/run/media/techatrix/UserFiles/zls/sample.zig:3:5: 0x10234a6 in main (sample)
return error.FailTest;

I have reduced this example from usage of std.zig.Ast.parse:

pubfnmain() !void {
conststd=@import("std");
vartree=trystd.zig.Ast.parse(std.heap.page_allocator, "const foo = ", .zig);
defertree.deinit(std.heap.page_allocator);
returnerror.FailTest;
}

The equivalent of the foo function in the reduced example would be expectTopLevelDeclRecoverable.

fnexpectTopLevelDeclRecoverable(p: *Parse) error{OutOfMemory}!Node.Index {
returnp.expectTopLevelDecl() catch|err|switch (err) {
error.OutOfMemory=>returnerror.OutOfMemory,
error.ParseError=> {
p.findNextContainerMember();
returnnull_node;
},
};
}

Expected Behavior

The error return trace should not include the failure function because the NonFatal error is being ignored in the foo function.

error: FailTest
/run/media/techatrix/UserFiles/zls/sample.zig:11:5: 0x21e2f6 in main (sample)
return error.FailTest;

This is also what Zig 0.11.0 would produce.

I suspect that this has been caused by #18173.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.regressionIt worked in a previous version of Zig, but stopped working.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions