Skip to content

rustc_resolve: point the label span at the segment that could not be resolved#157623

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
mejrs:not_found_in
Jun 9, 2026
Merged

rustc_resolve: point the label span at the segment that could not be resolved#157623
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
mejrs:not_found_in

Conversation

@mejrs

@mejrs mejrs commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

This is part of work that @weiznich and I are doing for #[diagnostic::on_unknown].

This means that you can put the attribute in macro_rules macros and have the label message point at the input the user gave. It's imo also the right thing to point the span at in general.

mod things {}

macro_rules! mac {
    ($thing: ident) => {{
        const _x: u32 = {
            #[diagnostic::on_unknown(label = "you did the bad thing")]
            use things::$thing;
            //~^ERROR unresolved import `things::what` [E0432]
            //~|ERROR unresolved import `things::what2` [E0432]
            $thing
        };
    }};
}
LL | |         what2
   | |         ----- you did the bad thing

See also the first and last commit for what these messages look(ed) like.

cc @estebank

@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 8, 2026
@rustbot

rustbot commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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 19 candidates

@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev 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.

I'm not really sure that the diagnostic is clearer? It just got larger, and visually highlights the part that actually can be resolved?

Am I reading this wrong? At first glance I'd think the errors is in the bar part, but it might equally well be in the foo part (when it has a typo, for instance).

LL |     use bar::foo;
   |         ^^^^^---
   |              |
   |              no `foo` in `bar`

View changes since this review

Comment on lines 4 to +7
LL | invoke_with_crate!{input proc_macro_item}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `proc_macro_item` in the root
| ^^^^^^^^^^^^^^^^^^^^^^^^^---------------^
| |
| no `proc_macro_item` in the root

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.

idk this just kind of looks weird now?

Comment thread tests/ui/imports/point_macro_input.rs Outdated
macro_rules! mac1 {
($thing: ident) => {{
const _x: u32 = things::$thing;
//~^NOTE due to this macro variable

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.

Suggested change
//~^NOTE due to this macro variable
//~^ NOTE due to this macro variable

very nitpicky but I believe this is how we do it everywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(usually) yes. I'll edit this.

@mejrs

mejrs commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

I'm not really sure that the diagnostic is clearer? It just got larger, and visually highlights the part that actually can be resolved?

Am I reading this wrong? At first glance I'd think the errors is in the bar part, but it might equally well be in the foo part (when it has a typo, for instance).

LL |     use bar::foo;
   |         ^^^^^---
   |              |
   |              no `foo` in `bar`

The main error message points at the entire path. The label points to the segment that could not be resolved. You get ^^^^--- when these two overlap.

This is not unique to this error or this PR:

#![deny(forgetting_references)]

fn main(){
    std::mem::forget(&());
}
error: calls to `std::mem::forget` with a reference instead of an owned value does nothing
 --> src/main.rs:4:5
  |
4 |     std::mem::forget(&());
  |     ^^^^^^^^^^^^^^^^^---^
  |                      |
  |                      argument has type `&()`

Here, the main error span is the entire function path, function call syntax and the arguments. The label is just of the function argument.

@folkertdev

Copy link
Copy Markdown
Contributor

Hmm allright then

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c8730ed has been approved by folkertdev

It is now in the queue for this repository.

@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 Jun 8, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 9, 2026
rustc_resolve: point the label span at the segment that could not be resolved

This is part of work that @weiznich and I are doing for `#[diagnostic::on_unknown]`.

This means that you can put the attribute in macro_rules macros and have the label message point at the input the user gave. It's imo also the right thing to point the span at in general.
```rust
mod things {}

macro_rules! mac {
    ($thing: ident) => {{
        const _x: u32 = {
            #[diagnostic::on_unknown(label = "you did the bad thing")]
            use things::$thing;
            //~^ERROR unresolved import `things::what` [E0432]
            //~|ERROR unresolved import `things::what2` [E0432]
            $thing
        };
    }};
}
```
```
LL | |         what2
   | |         ----- you did the bad thing
```
See also the first and last commit for what these messages look(ed) like.

cc @estebank
rust-bors Bot pushed a commit that referenced this pull request Jun 9, 2026
Rollup of 4 pull requests

Successful merges:

 - #157547 (Reorganize `tests/ui/issues` [6/N])
 - #157549 (Remove comments already covered by rustdoc)
 - #157591 (Simplify the HIR ty lowering of trait object lifetime bounds)
 - #157623 (rustc_resolve: point the label span at the segment that could not be resolved)
@rust-bors rust-bors Bot merged commit bc0f005 into rust-lang:main Jun 9, 2026
12 checks passed
rust-timer added a commit that referenced this pull request Jun 9, 2026
Rollup merge of #157623 - mejrs:not_found_in, r=folkertdev

rustc_resolve: point the label span at the segment that could not be resolved

This is part of work that @weiznich and I are doing for `#[diagnostic::on_unknown]`.

This means that you can put the attribute in macro_rules macros and have the label message point at the input the user gave. It's imo also the right thing to point the span at in general.
```rust
mod things {}

macro_rules! mac {
    ($thing: ident) => {{
        const _x: u32 = {
            #[diagnostic::on_unknown(label = "you did the bad thing")]
            use things::$thing;
            //~^ERROR unresolved import `things::what` [E0432]
            //~|ERROR unresolved import `things::what2` [E0432]
            $thing
        };
    }};
}
```
```
LL | |         what2
   | |         ----- you did the bad thing
```
See also the first and last commit for what these messages look(ed) like.

cc @estebank
@rustbot rustbot added this to the 1.98.0 milestone Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants