rust-lang / rust-lang/rust

should_panic in doctests accepts crashes, aborts, std::process::exit

Open
#143,009 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

/// ```should_panic
/// std::process::exit(1);
/// ```
fn f() {}

cargo test

I expected to see this happen: test fails.

Instead, this happened: test passes.

This also applies to explicit aborts, the process being killed by a signal, Miri aborting execution due to UB, etc. I've just found UB in my doctest that was accidentally hidden by this, and I only stumbled upon it because

WARNING: No rustdoc doctest environment variable provided so doctests will be run in the same process

happened when running under cross and apparently that affects how should_panic works.

This applies to both separate doctest binaries and edition 2024-style merged doctests.

Meta

rustc --version --verbose:

rustc 1.90.0-nightly (28f1c8079 2025-06-24)
binary: rustc
commit-hash: 28f1c807911c63f08d98e7b468cfcf15a441e34b
commit-date: 2025-06-24
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

@rustbot label +A-doctests +T-rustdoc

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

Use the reproducer in the issue as the entry point: run cargo test with the should_panic doctest containing std::process::exit(1), then compare the separate and merged doctest cases described in the report. Trace rustdoc's doctest handling; done means crashes, aborts, signals, and Miri UB are not accepted as should_panic successes while ordinary panics retain the expected behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.