Skip to content

Tweak "name not found" resolution error when it happens from within a derive expansion - #163380

Open
estebank wants to merge 4 commits into
rust-lang:mainfrom
estebank:issue-160463-2
Open

estebank wants to merge 4 commits into
rust-lang:mainfrom
estebank:issue-160463-2

Conversation

@estebank

@estebank estebank commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Point at the derive macro attribute:

error[E0425]: cannot find type `T` in this scope
  --> $DIR/derive-macro-unsupported-type-params.rs:11:13
   |
LL | #[derive(A)]
   |          - in this derive macro
...
LL |     Variant(T),
   |             ^ not found in this scope

Do not provide suggestions to add type parameters which would be nonsensical when coming from a proc-macro (because they get suggested on the annontated item):

error[E0425]: cannot find type `NonExistent` in this scope
  --> $DIR/issue-119493-type-error-ice.rs:5:16
   |
LL |     #[derive(Copy, Clone)]
   |                    ----- in this derive macro
LL |     struct Foo(NonExistent);
   |                ^^^^^^^^^^^ not found in this scope

I believe this is enough to close #160463.

Follow up to #160695.

```
error[E0425]: cannot find type `T` in this scope
  --> $DIR/derive-macro-unsupported-type-params.rs:11:13
   |
LL | #[derive(A)]
   |          - in this derive macro
...
LL |     Variant(T),
   |             ^ not found in this scope
```
@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 Sep 26, 2026
@rustbot

rustbot commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
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 77 candidates
  • Random selection from 19 candidates

Comment on lines +1896 to +1907
fn detect_resolution_error_in_derive(&self, err: &mut Diag<'_>, span: Span) {
if let Some(item) = self.diag_metadata.current_item
&& !item.span.eq_ctxt(span)
&& item.span.in_derive_expansion()
{
// `item` comes from a `#[derive()]`, but the error `span` doesn't, which means that the
// derive is referencing a name coming from the annotated item. If the item exists, then
// the derive macro itself is buggy. If the item exists, then an error will have already
// been emitted while evaluating the annotated item itself.
err.span_label(item.span, "in this derive macro");
}
}

@estebank estebank Sep 26, 2026 •

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.

I'd originally made this delay the error as bug, so that the resolve error would not be present, under the assumption that other errors would already have been emitted (if it was a non-existing item, we'd have an error when evaluating the item itself, if a proc-macro error, we have the other errors)...

View changes since the review

}
};
tokens
}

@estebank estebank Sep 26, 2026 •

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.

...but this macro is written such that if the name resolution error isn't emitted...

View changes since the review

Comment on lines +23 to +28
#[derive(C)]
enum C<T> {
Variant(T),
//~^ ERROR: cannot find value `T` in this scope
//~| ERROR: cannot find type `T` in this scope
}

@estebank estebank Sep 26, 2026 •

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.

...and this is the only code in the crate being built, we end up with an ICE.

This is why I'm adding context instead of silencing. I'd love it if we could come up with a strategy where we silence in every other case, but not this one.

View changes since the review

@estebank

Copy link
Copy Markdown
Contributor Author

Cc @petrochenkov

This branch has not been deployed

No deployments
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.

Rustc incorrectly suggests adding generic arguments to an enum that already has them when using enum-map-derive =1.0.0

3 participants