rust-lang / rust-lang/rust

`should_panic` does not capture allocation-induced panics

Open
#130,024 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-libtest C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When writing tests for custom allocators, I ran into the problem that an allocation-induced panic is not captured properly by should_panic. This is in my opinion a bug, as the behaviour the test shows is still very much like a panic.

Example code to trigger the issue:

use core::alloc::Layout;
use alloc::alloc::handle_alloc_error

#[test]
#[should_panic]
fn sample_test() {
    let layout = Layout::new::<u64>();
    handle_alloc_error(layout);
}

this uses an explicit call to handle_alloc_error, but the actual test cases I am writing create Vectors with a custom allocator that limits the amount of memory that can be allocated, which is a more realistic use case.

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (9c01301c5 2024-09-05)
binary: rustc
commit-hash: 9c01301c52df5d2d7b6fe337707a74e011d68d6f
commit-date: 2024-09-05
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

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

Begin by reducing the provided Rust test using alloc::alloc::handle_alloc_error and the #[should_panic] attribute, then inspect the test-harness or compiler entry points involved in panic capture. Done means allocation-induced panics are captured consistently by should_panic, with regression coverage for both the direct example and the custom-allocator Vector scenario.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.