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

Panic in compiler when user code has function named panic #22465

Open
widlarizer opened this issue Jan 11, 2025 · 4 comments · May be fixed by #22478
Open

Panic in compiler when user code has function named panic #22465

widlarizer opened this issue Jan 11, 2025 · 4 comments · May be fixed by #22478
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@widlarizer
Copy link

widlarizer commented Jan 11, 2025

Zig Version

0.14.0-dev.2634+b36ea592b

Steps to Reproduce and Observed Behavior

const std = @import("std");

fn maybeError(x: u32) !u32 {
    return if (x % 2 == 0) error.Error else x;
}

export fn main() noreturn {
    const x = maybeError(5) catch panic();
    std.debug.print("{}\n", .{x});

    while (true) {}
}

pub fn panic() noreturn {
    while (true) {}
}

zig build-obj main.zig has the following output

thread 19206 panic: parameter count mismatch calling builtin fn, expected 0, found 3
Unable to dump stack trace: debug info stripped
fish: Job 1, 'nix run 'github:mitchellh/zig-o…' terminated by signal SIGABRT (Abort)

Expected Behavior

0.13.0 prints an error and doesn't crash:

/nix/store/s3nq31mhm8gxkq691p5w6q61ficw1hvr-zig-0.13.0/lib/std/builtin.zig:751:9: error: expected type 'fn ([]const u8, ?*builtin.StackTrace, ?usize) noreturn', found 'fn () noreturn'
    root.panic
    ~~~~^~~~~~
/nix/store/s3nq31mhm8gxkq691p5w6q61ficw1hvr-zig-0.13.0/lib/std/builtin.zig:751:9: note: function with 0 parameters cannot cast into a function with 3 parameters
@widlarizer widlarizer added the bug Observed behavior contradicts documented or intended behavior label Jan 11, 2025
@mark-rushakoff
Copy link

I think this is intended behavior: https://ziglang.org/documentation/master/#panic

By default the panic handler function calls the public panic function exposed in the root source file...

Presumably the parameter count mismatch error is because your panic function doesn't accept the single []const u8 parameter.

@Vexu
Copy link
Member

Vexu commented Jan 12, 2025

It should show the same error message that it used to show in 0.13.0.

@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Jan 12, 2025
@Vexu Vexu added this to the 0.14.0 milestone Jan 12, 2025
@mlugg
Copy link
Member

mlugg commented Jan 12, 2025

This happens because Sema fails to validate the type of the panic function. Regressed by #21520.

@widlarizer
Copy link
Author

@mark-rushakoff I think we have a misunderstanding. I'm pretty sure using the @panic builtin should cause a panic in the program when it is run, not in the compiler when it is compiled :D

mlugg added a commit to mlugg/zig that referenced this issue Jan 12, 2025
Also improve the source locations when this validation fails.

Resolves: ziglang#22465
@mlugg mlugg linked a pull request Jan 12, 2025 that will close this issue
mlugg added a commit to mlugg/zig that referenced this issue Jan 12, 2025
Also improve the source locations when this validation fails.

Resolves: ziglang#22465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants