rust-lang / rust-lang/rust

rustdoc: doctest-xcompile passes when it shouldn't

Open
#139,924 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In the following example, both doctests pass, even though they are identical except for the annotation (one of them is no_run, the other compile_fail).

Command to run: cargo test --doc -Zdoctest-xcompile -Zbuild-std=core,panic_abort --target riscv32imac-unknown-none-elf

[package]
name = "rustdoc-issue"
version = "0.1.0"
edition = "2024"

[dependencies]
esp-hal = { version = "1.0.0-beta.0", features = ["esp32c6", "unstable"] }
#![no_std]

#[macro_export]
macro_rules! pre {
    () => {
        r#"
        # #![no_std]
        # #![no_main]
        #
        # use esp_hal as _;
        #
        # #[panic_handler]
        # fn panic(_ : &core::panic::PanicInfo) -> ! {
        #     loop {}
        # }

        # fn example() {
        "#
    };
}

/// ```rust,compile_fail
#[doc = crate::pre!()]
/// let mut x = 5;
/// x += 2;
/// }
/// ```
fn _foo() {}

/// ```rust,no_run
#[doc = crate::pre!()]
/// let mut x = 5;
/// x += 2;
/// }
/// ```
fn _foo2() {}

Since the no_run test case compiles, I would expect the compile_fail example to fail. I can only trigger this behaviour if I reference esp_hal - I'm not sure if other third party dependencies would trigger it. Without the external dependency, the annotation works as expected.

rustc --version
rustc 1.88.0-nightly (092a284ba 2025-04-13)

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 rustdoc doctest handling and the cargo test --doc -Zdoctest-xcompile -Zbuild-std=core,panic_abort --target riscv32imac-unknown-none-elf reproduction. Compare the identical compile_fail and no_run examples with and without the esp_hal dependency. Done means the external-dependency case no longer lets the compile_fail doctest pass when the corresponding no_run example compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing
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.