Skip to content

Line numbers in diagnostics for doctests are off by number of preceding hidden lines that aren't use core; #122941

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

Open
zopsicle opened this issue Mar 23, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@zopsicle
Copy link
Contributor

zopsicle commented Mar 23, 2024

Code

/// ```
/// # use playground::f;
/// x
/// ```
pub fn f()
{
    todo!()
}

Note that the expression x occurs on line 3 in the source code.

Current output

error[E0425]: cannot find value `x` in this scope
 --> src/lib.rs:4:1
  |
6 | x
  | ^ help: a function with a similar name exists: `f`
  |
 ::: /playground/src/lib.rs:5:1
  |
5 | pub fn f()
  | ---------- similarly named function `f` defined here

Note that the diagnostic refers to line 4.

Desired output

error[E0425]: cannot find value `x` in this scope
 --> src/lib.rs:3:1
  |
6 | x
  | ^ help: a function with a similar name exists: `f`
  |
 ::: /playground/src/lib.rs:5:1
  |
5 | pub fn f()
  | ---------- similarly named function `f` defined here

Rationale and extra context

No response

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6

Anything else?

This doesn't always seem to happen, for example in the following example it does not:

/// ```
/// # use core;
/// x
/// ```
pub fn f()
{
}
error[E0425]: cannot find value `x` in this scope
 --> src/lib.rs:3:1
  |
4 | x
  | ^ not found in this scope

I'm not sure what makes the difference. The only difference in the code is that one imports playground::f and the other imports core.

@zopsicle zopsicle added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 23, 2024
@zopsicle zopsicle changed the title Line numbers in diagnostics for doctests are off by number of preceding hidden lines Line numbers in diagnostics for doctests are sometimes off by number of preceding hidden lines Mar 23, 2024
@zopsicle zopsicle changed the title Line numbers in diagnostics for doctests are sometimes off by number of preceding hidden lines Line numbers in diagnostics for doctests are off by number of preceding hidden lines that aren't use core; Mar 23, 2024
@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 29, 2024
@GuillaumeGomez
Copy link
Member

The problem is actually a bit more complicated than just an offset. For example:

// blabla

///
///
/// ```
/// # use playground::f;
/// x
/// ```
pub fn f()
{
    todo!()
}

It will give:

$ rustdoc --test foo2.rs 

running 1 test
test foo2.rs - f (line 5) ... FAILED

failures:

---- foo2.rs - f (line 5) stdout ----
error[E0432]: unresolved import `playground`
 --> foo2.rs:6:5
  |
3 | use playground::f;
  |     ^^^^^^^^^^ maybe a missing crate `playground`?
  |
  = help: consider adding `extern crate playground` to use the `playground` crate

error[E0425]: cannot find value `x` in this scope
 --> foo2.rs:7:1
  |
4 | x
  | ^ not found in this scope

error: aborting due to 2 previous errors

As you can see, even though I added a lot of lines before, the lines didn't change for the error messages. It's because we generate tests outside of this code and rustc only has information about this. I plan to rework doctests in the near future to improve such issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

2 participants