Skip to content

Regression on doc-tests with statements and a main function #140162

Closed
@Darksonn

Description

@Darksonn

Code

I tried this code:

/// ```
/// # if cfg!(miri) { return; }
/// use playground::my_func;
///
/// fn main() {
///     println!("Hi!");
///     my_func();
/// }
/// ```
pub fn my_func() {
}

On stable, running this doctest succeeds. On beta, it triggers this error:

running 1 test
test src/lib.rs - my_func (line 2) ... FAILED

failures:

---- src/lib.rs - my_func (line 2) stdout ----
error: expected item, found keyword `if`
 --> src/lib.rs:4:1
  |
4 | if cfg!(miri) { return; }
  | ^^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: aborting due to 1 previous error

Couldn't compile the test.

failures:
    src/lib.rs - my_func (line 2)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

Version it worked on

It most recently worked on: Latest stable (i.e. rustc 1.86.0)

Version with regression

rustc --version --verbose:

rustc 1.87.0-beta.5 (386abeb93 2025-04-19)
binary: rustc
commit-hash: 386abeb93ab8e683357f54ffe76ae53861b2d984
commit-date: 2025-04-19
host: x86_64-unknown-linux-gnu
release: 1.87.0-beta.5
LLVM version: 20.1.1

@rustbot modify labels: +regression-from-stable-to-beta -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions