Skip to content

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

Closed

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

Activity

  1. added
    C-bugCategory: This is a bug.
    regression-untriagedUntriaged performance or correctness regression.
    on Apr 22, 2025
  2. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    and removed
    regression-untriagedUntriaged performance or correctness regression.
    on Apr 22, 2025
  3. added
    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
    on Apr 22, 2025
  4. apiraino commented on Apr 22, 2025

    @apiraino
    Contributor

    Bisecting by running rustdoc --test src/lib.rs (the file contains the reproducible code):

    Regression in rust-lang-ci@cdcf730
    #138104

    cc @GuillaumeGomez @fmease for authoring #138104

    bisected with cargo-bisect-rustc v0.6.9

    Host triple: x86_64-unknown-linux-gnu
    Reproduce with:

    cargo bisect-rustc --without-cargo --preserve --start 2024-02-01 --script test.sh 
  5. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 22, 2025
  6. GuillaumeGomez commented on Apr 22, 2025

    @GuillaumeGomez
    Member

    Gonna try to take a look as soon as possible.

  7. added
    A-doctestsArea: Documentation tests, run by rustdoc
    on Apr 22, 2025
  8. cyrgani commented on Apr 22, 2025

    @cyrgani
    Contributor

    duplicate of #139651?

  9. GuillaumeGomez commented on Apr 23, 2025

    @GuillaumeGomez
    Member

    That's an interesting case: your main function is never run. I'm not sure it's the desired output. However, to keep compatibility, this code should be allowed. So instead, I offer to emit a warning in case there is a main function alongside expressions that only belong to a function.

  10. GuillaumeGomez commented on Apr 23, 2025

    @GuillaumeGomez
    Member

    duplicate of #139651?

    Ah yes it is!

  11. 4 remaining items

  12. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Apr 24, 2025
  13. added a commit that references this issue on Apr 25, 2025
    a67fc12
  14. added a commit that references this issue on Apr 26, 2025
    38e59a8
  15. added 2 commits that reference this issue on Apr 26, 2025
    742814d
    abeffef
  16. added 3 commits that reference this issue on Apr 27, 2025
    e813782
    6900ea5
    dc57338
  17. added a commit that references this issue on Apr 27, 2025
    eee34b5
  18. added a commit that references this issue on Apr 28, 2025
    a782b54
  19. added a commit that references this issue on Apr 28, 2025
    4f8ada3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions