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

rustdoc 2015 regression: doc test fails (unresolved import) if it contains a doc comment #56727

Closed
japaric opened this issue Dec 11, 2018 · 2 comments
Labels
A-doctests Area: Documentation tests, run by rustdoc T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@japaric
Copy link
Member

japaric commented Dec 11, 2018

STR

Cargo.toml

[package]
name = "foo"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]

[dependencies]

[dev-dependencies]
stm32f30x = "=0.8.0"

src/lib.rs

//! ```
//! // crate: stm32f30x-hal
//! //! An implementation of the `embedded-hal` traits for STM32F30x microcontrollers
//!
//! // device crate
//! extern crate stm32f30x;
//!
//! use stm32f30x::USART1;
//!
//! # fn main() {}
//! ```
$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/foo-794f65deaf39282e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests foo

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

failures:

---- src/lib.rs -  (line 1) stdout ----
error[E0432]: unresolved import `stm32f30x`
 --> src/lib.rs:8:5
  |
9 | use stm32f30x::USART1;
  |     ^^^^^^^^^ maybe a missing `extern crate stm32f30x;`?

thread 'src/lib.rs -  (line 1)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:327:13
note: Run with `RUST_BACKTRACE=1` for a backtrace.


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

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--doc'
$ rustc -V
rustc 1.32.0-nightly (4a45578bc 2018-12-07)

Ways to "fix" the error:

  • Add edition = "2018" to Cargo.toml, OR
  • Remove the doc comment (//!), OR
  • Switch to an older nightly (e.g. nightly-2018-11-01)
japaric added a commit to rust-embedded/embedded-hal that referenced this issue Dec 11, 2018
fixes nightly builds
bors bot added a commit to rust-embedded/embedded-hal that referenced this issue Dec 11, 2018
116: work around rust-lang/rust#56727 r=ryankurte a=japaric

fixes nightly builds

Co-authored-by: Jorge Aparicio <jorge@japaric.io>
@japaric japaric changed the title rustdoc 2015: doc test fails (unresolved import) if it contains a doc comment rustdoc 2015 regression: doc test fails (unresolved import) if it contains a doc comment Dec 11, 2018
@estebank estebank added the A-doctests Area: Documentation tests, run by rustdoc label Dec 11, 2018
@QuietMisdreavus QuietMisdreavus added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Dec 13, 2018
@QuietMisdreavus
Copy link
Member

QuietMisdreavus commented Dec 13, 2018

I almost have a PR ready for this.

Here's the basic structure of what's happening:

  1. Rustdoc scans the doctest to split out crate attributes and crate statements from the doctest. However, the current scan doesn't expect comments in the doctest, so upon encountering the comment at the start of the doctest, it dumps the whole thing into what it calls "everything else".
  2. Rustdoc scans the doctest again to look for fn main and extern crate embedded-hal. This is the libsyntax-based scan. However, there is now a //! doc comment at the beginning of this sample, which causes an error. The parsing loop here is expecting to pull out individual items - it doesn't treat the whole sample as a potential block, so the inner attribute causes a parse error. This error is silenced, and the loop exits, claiming to have not found the fn main later on.
  3. Because the loop did not find fn main, Rustdoc proceeds to wrap the sample in another one.
  4. Upon attempting to compile the completed doctest, the compiler doesn't register the extern crate statement inside the generated main function, so when it tries to resolve the use statement, the resolution fails.
    • However! If the doctest is being compiled under 2018, then the crate is available in the extern prelude! In this situation, the compiler can ultimately ignore the extern crate statement (which is valid despite being unaddressable) and load the path from there.

EDIT: (submitted too early, oops)

So, the solution is to make sure rustdoc can handle comments when splitting up doctests. Like i said, i almost have this ready.

@QuietMisdreavus
Copy link
Member

That PR is now live: #56793

Centril added a commit to Centril/rust that referenced this issue Dec 16, 2018
…GuillaumeGomez

rustdoc: look for comments when scraping attributes/crates from doctests

Fixes rust-lang#56727

When scraping out crate-level attributes and `extern crate` statements, we wouldn't look for comments, so any presence of comments would shunt it and everything after it into "everything else". This could cause parsing issues when looking for `fn main` and `extern crate my_crate` later on, which would in turn cause rustdoc to incorrectly wrap a test with `fn main` when it already had one declared.

I took the opportunity to clean up the logic a little bit, but it would still benefit from a libsyntax-based loop like the `fn main` detection.
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 T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants