Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.builtin: allow user code to set specific panic handlers #15392

Closed
wants to merge 3 commits into from

Conversation

wrongnull
Copy link
Contributor

This pr will allow users to set custom specific panic handlers. In current implementation it looks like follows:

pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
    if (std.mem.eql(u8, message, "access of inactive union field")) {
        //handle access of inactive union field
    }
    //handle other cases in the same manner
}

With proposed approach it will be possible to write something like this

pub fn panicInactiveUnionField(active: anytype, wanted: @TypeOf(active)) noreturn {
   //handle this panic
}

right in root source file.

This pr will allow users to set custom specific panic handlers. In current implementation it looks like follows: 
```zig

pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
    if (std.mem.eql(u8, message, "access of inactive union field")) {
        //handle access of inactive union field
    }
    //handle other cases in the same manner
}
```
With proposed approach it will be possible to write something like this
```zig
pub fn panicInactiveUnionField(active: anytype, wanted: @typeof(active)) noreturn {
   //handle this panic
}
```
right in root source file.
@Vexu
Copy link
Member

Vexu commented Apr 22, 2023

Do you have a use case for overriding these? IMO they're more implementation details than language features.

@wrongnull
Copy link
Contributor Author

@Vexu
easy way to split some certain cases of panic without piling up ifs could be useful for user.
For instance one can write specific information about panic into log file. Or do (almost) anything you want with such data.

@Vexu
Copy link
Member

Vexu commented May 1, 2023

This can be reconsidered if someone has an actual use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants