Skip to content

Doc test containing code before main function fails to compile on beta #139651

Closed
@andrewhickman

Description

@andrewhickman

I tried this code:

//! ```
//! # std::fs::write("foo.txt", "Hello, world!").unwrap();
//! fn main() {
//!   println!("{}", std::fs::read_to_string("foo.txt").unwrap())
//! }
//! ```

With Rust 1.86.0, running cargo test compiles and runs the doctest successfully.

On the beta channel, the doctest fails to compile:

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

failures:

---- src/lib.rs - (line 1) stdout ----
error: expected one of `!` or `::`, found `(`
 --> src/lib.rs:2:15
  |
2 | std::fs::write("foo.txt", "Hello, world!").unwrap();
  |               ^ expected one of `!` or `::`

error: aborting due to 1 previous error

Couldn't compile the test.

failures:
    src/lib.rs - (line 1)

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

Meta

rustc +beta --version --verbose:

rustc 1.87.0-beta.3 (a22ecb51f 2025-04-07)
binary: rustc
commit-hash: a22ecb51f85284abc5b18a32b27696b0e3e9cf85
commit-date: 2025-04-07
host: x86_64-pc-windows-msvc
release: 1.87.0-beta.3
LLVM version: 20.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-bugCategory: This is a bug.E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcP-mediumMedium priorityT-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