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:

pub fn main() !void {
    try foo();
    return error.FailTest;
}

fn failure() error{ Fatal, NonFatal }!void {
    return error.NonFatal;
}

fn foo() error{Fatal}!void {
    return failure() catch |err| switch (err) {
        error.Fatal => return error.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:

pub fn main() !void {
    const std = @import("std");
    var tree = try std.zig.Ast.parse(std.heap.page_allocator, "const foo = ", .zig);
    defer tree.deinit(std.heap.page_allocator);

    return error.FailTest;
}

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

zig/lib/std/zig/Parse.zig

Lines 670 to 678 in c724cc6

fn expectTopLevelDeclRecoverable(p: *Parse) error{OutOfMemory}!Node.Index {
return p.expectTopLevelDecl() catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.ParseError => {
p.findNextContainerMember();
return null_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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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