Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
andrewhickman opened this issue Apr 10, 2025 · 6 comments · Fixed by #140220
Closed

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

andrewhickman opened this issue Apr 10, 2025 · 6 comments · Fixed by #140220
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc P-medium Medium priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@andrewhickman
Copy link
Contributor

andrewhickman commented Apr 10, 2025

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
@andrewhickman andrewhickman added the C-bug Category: This is a bug. label Apr 10, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 10, 2025
andrewhickman added a commit to andrewhickman/protox that referenced this issue Apr 10, 2025
@fmease fmease added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. A-doctests Area: Documentation tests, run by rustdoc labels Apr 10, 2025
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Apr 10, 2025
@fmease fmease added P-high High priority E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Apr 10, 2025
@fmease
Copy link
Member

fmease commented Apr 10, 2025

Likely #138104 but I haven't run a bisection yet.

@fmease
Copy link
Member

fmease commented Apr 10, 2025

I'm surprised that this code used to work actually

@jyn514
Copy link
Member

jyn514 commented Apr 10, 2025

@fmease it used to be that if the code didn't compile at the top level, rustdoc would shove it in a fn doctest_NNNN() { CODE } function. so the main function would never be executed, but it would still compile successfully.

@fmease
Copy link
Member

fmease commented Apr 10, 2025

Oof, oh no. Okay :/

@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 11, 2025
@fmease fmease added P-medium Medium priority and removed P-high High priority labels Apr 15, 2025
@GuillaumeGomez
Copy link
Member

Arf, I wish I saw the comment about the duplicate issue before investigating. Well anyway, I said the same as what @jyn514 just said and offered to accept this code but emit a warning if such a case occur.

@GuillaumeGomez
Copy link
Member

Opened #140220.

@Darksonn Darksonn marked this as a duplicate of #140162 Apr 24, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 25, 2025
…ng, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`
jhpratt added a commit to jhpratt/rust that referenced this issue Apr 26, 2025
…ng, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? ``@fmease``
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 26, 2025
…, r=<try>

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`

try-job: x86_64-gnu-aux
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 26, 2025
…, r=<try>

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`

try-job: x86_64-gnu-aux
tgross35 added a commit to tgross35/rust that referenced this issue Apr 27, 2025
…ng, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`

try-job: x86_64-gnu-aux
tgross35 added a commit to tgross35/rust that referenced this issue Apr 27, 2025
…ng, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? ``@fmease``

try-job: x86_64-gnu-aux
tgross35 added a commit to tgross35/rust that referenced this issue Apr 27, 2025
…ng, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? ```@fmease```

try-job: x86_64-gnu-aux
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 27, 2025
…, r=<try>

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`

try-job: x86_64-mingw-1
@bors bors closed this as completed in a782b54 Apr 28, 2025
@bors bors marked this as not a duplicate of #140162 Apr 28, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 28, 2025
Rollup merge of rust-lang#140220 - GuillaumeGomez:doctest-main-wrapping, r=fmease

Fix detection of main function if there are expressions around it

Fixes rust-lang#140162.
Fixes rust-lang#139651.

Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run).

r? `@fmease`

try-job: x86_64-mingw-1
cuviper pushed a commit to cuviper/rust that referenced this issue May 2, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc P-medium Medium priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
Development

Successfully merging a pull request may close this issue.

6 participants