Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Unexpected semilocon makes rustdoc to produce false negatives #91014

Closed
xDarksome opened this issue Nov 18, 2021 · 0 comments · Fixed by #91026
Closed

Unexpected semilocon makes rustdoc to produce false negatives #91014

xDarksome opened this issue Nov 18, 2021 · 0 comments · Fixed by #91026
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@xDarksome
Copy link

I tried this code:

/// ```rust
/// struct S {}; // unexpected semicolon after struct def
///
/// fn main() {
///    assert_eq!(0, 1);
/// }
/// ```
mod m {}
cargo test

I expected to see this happen:

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


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

failures:

---- src/lib.rs - m (line 1) stdout ----
error: expected item, found `;`
 --> src/lib.rs:6:12
  |
2 | struct S {};
  |            ^ help: remove this semicolon
  |
  = help: braced struct declarations are not followed by a semicolon

error: aborting due to previous error

Couldn't compile the test.

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

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

Instead, this happened:

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


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

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

Meta

rustc --version --verbose:

binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1

Also, reproduces on playground (1.56.1 / 1.57.0-beta.3 / 1.58.0-nightly)

Putting struct def after the main function makes rustdoc to behave as expected:

/// ```rust
/// fn main() {
///    assert_eq!(0, 1);
/// }
///
/// struct S {}; // unexpected semicolon after struct def
/// ```
mod m {}

Implicit main function does the assertion correctly.

/// ```rust
/// struct S {}; // semicolon is OK here
/// assert_eq!(0, 1);
/// ```
mod m {}
@xDarksome xDarksome added the C-bug Category: This is a bug. label Nov 18, 2021
@jyn514 jyn514 added A-doctests Area: Documentation tests, run by rustdoc T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Nov 18, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 19, 2021
…-semicolon, r=jyn514

rustdoc doctest: detect `fn main` after an unexpected semicolon

Fixes rust-lang#91014

The basic problem with this is that rustdoc, when hunting for `fn main`, will stop parsing after it reaches a fatal error. This unexpected semicolon was a fatal error, so in `src/test/rustdoc-ui/failed-doctest-extra-semicolon-on-item.rs`, it would wrap the doctest in an implied main function, turning it into this:

    fn main() {
        struct S {};
        fn main() {
            assert_eq!(0, 1);
        }
    }

This, as it turns out, is totally valid, and it executes no assertions, so *it passes,* even though the user wanted it to execute the assertion.

The Rust parser already has the ability to recover from these unexpected semicolons, but to do so, it needs to use the `parse_mod` function, so this PR changes it to do that.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 19, 2021
…-semicolon, r=jyn514

rustdoc doctest: detect `fn main` after an unexpected semicolon

Fixes rust-lang#91014

The basic problem with this is that rustdoc, when hunting for `fn main`, will stop parsing after it reaches a fatal error. This unexpected semicolon was a fatal error, so in `src/test/rustdoc-ui/failed-doctest-extra-semicolon-on-item.rs`, it would wrap the doctest in an implied main function, turning it into this:

    fn main() {
        struct S {};
        fn main() {
            assert_eq!(0, 1);
        }
    }

This, as it turns out, is totally valid, and it executes no assertions, so *it passes,* even though the user wanted it to execute the assertion.

The Rust parser already has the ability to recover from these unexpected semicolons, but to do so, it needs to use the `parse_mod` function, so this PR changes it to do that.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 19, 2021
…-semicolon, r=jyn514

rustdoc doctest: detect `fn main` after an unexpected semicolon

Fixes rust-lang#91014

The basic problem with this is that rustdoc, when hunting for `fn main`, will stop parsing after it reaches a fatal error. This unexpected semicolon was a fatal error, so in `src/test/rustdoc-ui/failed-doctest-extra-semicolon-on-item.rs`, it would wrap the doctest in an implied main function, turning it into this:

    fn main() {
        struct S {};
        fn main() {
            assert_eq!(0, 1);
        }
    }

This, as it turns out, is totally valid, and it executes no assertions, so *it passes,* even though the user wanted it to execute the assertion.

The Rust parser already has the ability to recover from these unexpected semicolons, but to do so, it needs to use the `parse_mod` function, so this PR changes it to do that.
@bors bors closed this as completed in a9858ce Nov 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants