rust-lang / rust-lang/rust

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

Open
#122,941 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-rustdoc
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the doctest example from the issue and compare diagnostic line mapping when the hidden line imports playground::f versus core. Trace the doctest source transformation and line-offset handling, then add a regression test that expects the diagnostic for x to point to source line 3.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.