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

Incorrect span used for doctests defined using #[doc(include)] or #[doc = include_str!("...")] #81070

Open
lambda-fairy opened this issue Jan 16, 2021 · 2 comments
Assignees
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

@lambda-fairy
Copy link
Contributor

Given these two files:

ducks.rs

#![feature(extended_key_value_attributes)]
#[doc = include_str!("ducks.md")]
fn ducks() {}

ducks.md

test
test
test
test
test
test
test
test
test
test

```rust
invalid syntax
```

When I run rustdoc --test ducks.rs, I get an error:

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `syntax`
 --> ducks.rs:14:9
  |
3 | invalid syntax
  |         ^^^^^^ expected one of 8 possible tokens

error: aborting due to previous error

The error message is correct. However, the position (ducks.rs:14:9) is invalid. I would expect it to reference either the original include_str! call, or (preferred) the snippet in the Markdown file.

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (bc39d4d9c 2021-01-15)
binary: rustc
commit-hash: bc39d4d9c514e5fdb40a5782e6ca08924f979c35
commit-date: 2021-01-15
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0

cc #78835

@lambda-fairy lambda-fairy added the C-bug Category: This is a bug. label Jan 16, 2021
@lambda-fairy lambda-fairy changed the title Incorrect span used for doctests defined using #[doc = include_str("...")] Incorrect span used for doctests defined using #[doc = include_str!("...")] Jan 16, 2021
@camelid camelid added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Mar 6, 2021
@camelid
Copy link
Member

camelid commented Mar 6, 2021

Hmm, this is especially interesting given that the error message has 3 | for the line number gutter but 14:9 for the location...

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `syntax`
 --> ducks.rs:14:9
  |
3 | invalid syntax
  |         ^^^^^^ expected one of 8 possible tokens

error: aborting due to previous error

However, this bug also occurs with old-school doc(include):

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `syntax`
 --> ducks.rs:13:9
  |
3 | invalid syntax
  |         ^^^^^^ expected one of 8 possible tokens

error: aborting due to previous error

I wonder if the reason the doc(include) one shows 13:9 as the location while the include_str! shows 14:9 is because include_str! uses macro expansion whereas doc(include) is dealt with in rustdoc itself? Not sure though.

@camelid camelid added the A-doctests Area: Documentation tests, run by rustdoc label Mar 6, 2021
@camelid camelid changed the title Incorrect span used for doctests defined using #[doc = include_str!("...")] Incorrect span used for doctests defined using #[doc(include)] or #[doc = include_str!("...")] Mar 6, 2021
@camelid camelid added I-nominated T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed I-nominated T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 6, 2021
@jyn514 jyn514 removed the T-rustdoc label May 7, 2021
@camelid camelid self-assigned this Aug 30, 2021
@camelid
Copy link
Member

camelid commented Jan 5, 2022

Ok, I've looked into this and found the underlying issue. Rustdoc actually uses environment variables as a side channel to tell rustc, but the code is really messy and the filename and line number seem to be obtained separately:

compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", path);
compiler.env("UNSTABLE_RUSTDOC_TEST_LINE", format!("{}", line as isize - line_offset as isize));

I'm still looking into ways to get the filename based on the span of the docs themselves rather than the attribute they're in.

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 7, 2024
…eGomez

rustdoc: Refactor doctest collection and running code

This code previously had a quite confusing structure, mixing the collection,
processing, and running of doctests with multiple layers of indirection. There
are also many cases where tons of parameters are passed to functions with little
typing information (e.g., booleans or strings are often used).

As a result, the source of bugs is obfuscated (e.g. rust-lang#81070) and large changes
(e.g.  rust-lang#123974) become unnecessarily complicated. This PR is a first step to try
to simplify the code and make it easier to follow and less bug-prone.

r? `@GuillaumeGomez`
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
Status: No status
Development

No branches or pull requests

5 participants