Skip to content

Suggest installing rust-src when standard library source is unavailable - #158191

Open
sladyn98 wants to merge 1 commit into
rust-lang:mainfrom
sladyn98:issue-156402-rust-src-hint
Open

Suggest installing rust-src when standard library source is unavailable#158191
sladyn98 wants to merge 1 commit into
rust-lang:mainfrom
sladyn98:issue-156402-rust-src-hint

Conversation

@sladyn98

@sladyn98 sladyn98 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Why

A lot of diagnostics point at a span inside the standard library e.g. required by a bound in std::iter::Iterator::sum``, or JoinHandle::join takes ownership of the receiver. For toolchains installed via `rustup` std is shipped with its paths remapped to `/rustc//library/...` and the source itself is not included unless the `rust-src` component is installed.

So when an error references std, rustc tries to render that span, can't find the source on disk, and falls back to printing just the bare remapped path with no code:

  error[E0277]: a value of type String cannot be made by summing an iterator ...
   --> src/main.rs:2:20
    ...
  note: required by a bound in std::iter::Iterator::sum
   --> /rustc/<hash>/library/core/src/iter/traits/iterator.rs:3669:4

For the user this is a dead end: the path doesn't exist locally, there's no surrounding code. It is fixable: rustup component add rust-src makes the source available, after which the same diagnostic renders the real std snippet. But that capability is
invisible. This PR closes that discoverability gap by suggesting it at exactly the moment it would help.

Closes #156402

@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

rustc_errors::annotate_snippet_emitter_writer was changed

cc @Muscraft

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

r? @tiif

rustbot has assigned @tiif.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@sladyn98

Copy link
Copy Markdown
Contributor Author

r? @estebank

@rustbot rustbot assigned estebank and unassigned tiif Jun 20, 2026
@sladyn98 sladyn98 changed the title Suggest installing rust-src when standard library source is unavail… Suggest installing rust-src when standard library source is unavailable Jun 20, 2026

@estebank estebank left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Would you have time to address the nitpicks? If not, we can likely land this as is.

View changes since this review

Comment thread compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs Outdated
Comment thread compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs Outdated
…able

When a diagnostic points into the standard library but its source isn't
available locally (e.g. a rustup toolchain without the `rust-src`
component), the emitter can only print the bare remapped path with no
snippet. Emit a one-time `help` suggesting `rustup component add rust-src`
so the next build can show the source.

The hint is shown at most once per compilation (tracked with an
`AtomicBool` on the emitter) and only for remapped std paths. It is added
to both the primary and child-note fallbacks, since std spans usually
appear in child "required by a bound in `core::...`" notes.
@sladyn98
sladyn98 force-pushed the issue-156402-rust-src-hint branch from 487bec6 to 55b1daf Compare June 21, 2026 20:29
@sladyn98
sladyn98 requested a review from estebank June 21, 2026 20:32
@sladyn98

Copy link
Copy Markdown
Contributor Author

@estebank I think we are good to merge this ?

@sladyn98

Copy link
Copy Markdown
Contributor Author

@estebank Any updates on this ?

@estebank

estebank commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 55b1daf has been approved by estebank

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

Reason for tree closure: error: could not lock gitconfig

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 9, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 10, 2026
…t, r=estebank

Suggest installing `rust-src` when standard library source is unavailable

### Why

  A lot of diagnostics point at a span *inside* the standard library e.g. `required by a bound in `std::iter::Iterator::sum``, or `JoinHandle::join takes ownership of the receiver`. For toolchains installed via `rustup` std is shipped with its paths remapped to `/rustc/<hash>/library/...` and the source itself is **not** included unless the `rust-src` component is installed.

So when an error references std, rustc tries to render that span, can't find the source on disk, and falls back to printing just the bare remapped path with no code:

```
  error[E0277]: a value of type String cannot be made by summing an iterator ...
   --> src/main.rs:2:20
    ...
  note: required by a bound in std::iter::Iterator::sum
   --> /rustc/<hash>/library/core/src/iter/traits/iterator.rs:3669:4
```

For the user this is a dead end: the path doesn't exist locally, there's no surrounding code. It *is* fixable: `rustup component add rust-src` makes the source available, after which the same diagnostic renders the real std snippet. But that capability is
invisible. This PR closes that discoverability gap by suggesting it at exactly  the moment it would help.

Closes rust-lang#156402
rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
Rollup of 5 pull requests

Successful merges:

 - #158191 (Suggest installing `rust-src` when standard library source is unavailable)
 - #162122 (Put manual calls of Enzyme intrinsics behind a feature gate)
 - #162553 (Revert "Skip rustc book on i686")
 - #162562 (Add regression test for higher-ranked fn pointer trait impl mismatch errors)
 - #162518 (Add a revision for previously fixed solver issue)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 10, 2026
Suggest installing `rust-src` when standard library source is unavailable

### Why

  A lot of diagnostics point at a span *inside* the standard library e.g. `required by a bound in `std::iter::Iterator::sum``, or `JoinHandle::join takes ownership of the receiver`. For toolchains installed via `rustup` std is shipped with its paths remapped to `/rustc/<hash>/library/...` and the source itself is **not** included unless the `rust-src` component is installed.

So when an error references std, rustc tries to render that span, can't find the source on disk, and falls back to printing just the bare remapped path with no code:

```
  error[E0277]: a value of type String cannot be made by summing an iterator ...
   --> src/main.rs:2:20
    ...
  note: required by a bound in std::iter::Iterator::sum
   --> /rustc/<hash>/library/core/src/iter/traits/iterator.rs:3669:4
```

For the user this is a dead end: the path doesn't exist locally, there's no surrounding code. It *is* fixable: `rustup component add rust-src` makes the source available, after which the same diagnostic renders the real std snippet. But that capability is
invisible. This PR closes that discoverability gap by suggesting it at exactly  the moment it would help.

Closes #156402
@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 10, 2026
@rust-bors

rust-bors Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 8a723e4 failed: CI. Failed job:

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-x86_64-gnu-llvm-22-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
help: if you don't care about this missing field, you can explicitly ignore it
   |
67 -             theme,
68 -         } = self;
67 +             theme, rust_src_hint_shown: _ } = self;
   |
help: or always ignore missing fields here
   |
67 -             theme,
68 -         } = self;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest installing rust-std component when spans pointing at std can't find the sources

5 participants