rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.64k stars 12.49k forks source link

`doctest` line number is incorrect if used with #![doc = include_str!()] #127893

Open ProfFan opened 1 month ago

ProfFan commented 1 month ago

I tried this code:

#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

/// ```no_run << line 23
///  panic!("nooo");
///  ```
pub struct XXXXYYYY<A, B> {
...
}

I expected to see this happen: the included doctests show the line number of the include statement.

Instead, this happened:

running 2 tests
test src/lib.rs - XXXXYYYY (line 23) - compile ... ok
test src/lib.rs - (line 23) ... FAILED

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (032be6f7b 2024-07-16)
binary: rustc
commit-hash: 032be6f7bbe091c7dfa29f115e94b9cc9bae1758
commit-date: 2024-07-16
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
Backtrace

``` N/A ```

ProfFan commented 1 month ago

Strange, this runs even after I removed the doctest itself, target folder, as well as doing a cargo clean. Does cargo keep a cache of this test somewhere outside of the project?

workingjubilee commented 1 month ago

...weird.

can you provide a version that compiles and repros?

ProfFan commented 1 month ago

...weird.

can you provide a version that compiles and repros?

https://github.com/ProfFan/icm426xx on main

I don't know if this is my macOS being funky, this is the only system I have.

Just run cargo test and this should trigger?

ProfFan commented 1 month ago

nvm, it's this trick from stackoverflow that got me:

#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

This results in doctest trying to run the code snippet from README.

However this problem is still valid: the displayed line number is clearly incorrect. It shows the line number of my disabled test in the source code.

ProfFan commented 1 month ago

XREF: https://stackoverflow.com/questions/69117817/how-can-i-tell-cargo-to-run-code-test-in-documentation-in-my-readme-md

Same problem in this post.

tgross35 commented 1 month ago

Similar but not exact issue https://github.com/rust-lang/rust/issues/81070