rust-lang / rust-lang/rust

Warn when using `std::panic::catch_unwind` with the panic strategy as `abort`

Open
#149,902 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

std::panic::catch_unwind() behaves unexpectedly when the panic strategy is abort, and although this is documented it can lead to unexpected behavior. This would be especially helpful when using certain targets (such as NVPTX) and codegen backends (such as Cranelift) that silently override the panic strategy to always abort when unwinding is not supported, which leads to confusing behavior when unwind is explicitly set.

Example code

src/main.rs:

fn main() {
    let unwind_result = std::panic::catch_unwind(|| {
        panic!("Oops");
    });

    if unwind_result.is_err() {
        println!("Caught panic!");
    }
}

Cargo.toml:

[package]
name = "catch_unwind"
edition = "2024"

[profile.cranelift]
inherits = "dev"
panic = "unwind"
codegen-backend = "cranelift"

[profile.llvm]
inherits = "dev"
panic = "unwind"
codegen-backend = "llvm"

[profile.llvm-abort]
inherits = "dev"
panic = "abort"
codegen-backend = "llvm"

Current output

cargo run --profile cranelift (unexpected result, should see Caught panic! at the end):

    Finished `cranelift` profile [unoptimized + debuginfo] target(s) in 0.18s
     Running `target/cranelift/catch_unwind`

thread 'main' (65964) panicked at src/main.rs:3:9:
Oops
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

cargo run --profile llvm (expected result):

    Finished `llvm` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/llvm/catch_unwind`

thread 'main' (60978) panicked at src/main.rs:3:9:
Oops
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Caught panic!

cargo run --profile llvm-abort (unexpected result, should see Caught panic! at the end):

    Finished `llvm-abort` profile [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/llvm-abort/catch_unwind`

thread 'main' (64934) panicked at src/main.rs:3:9:
Oops
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Desired output

cargo run --profile {cranelift, llvm-abort} produces a warning about catch_unwind not being available, ideally identifying why panics are set to abort e.g. the codegen options, target, codegen backend etc.

Rustc version

rustc 1.94.0-nightly (f52090008 2025-12-10)
binary: rustc
commit-hash: f5209000832c9d3bc29c91f4daef4ca9f28dc797
commit-date: 2025-12-10
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example in src/main.rs and its Cargo.toml profiles, running cargo run for the cranelift, llvm, and llvm-abort profiles to reproduce the differing behavior. Trace how the panic strategy reaches catch_unwind across the target and codegen backend cases described in the issue. Done means abort configurations warn that catch_unwind is unavailable and identify the relevant cause.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.