Skip to content

Detect bad number of generics caused by bad derive - #160695

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
estebank:issue-160463
Sep 26, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
estebank:issue-160463

Conversation

@estebank

@estebank estebank commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

When a derive macro expands the annotated item's name directly using quote!, it keep the item's Span context (instead of having a new context). This means that the generic Span context machinery which provides feedback that an error happened due to a derive doesn't kick in.

If a derive macro isn't written to take into account the existence of type parameters, an error for "mismatched number of type parameters" will be emitted. We now detect the case when this happens due to the derive macro, and customize the output to point that out, as well as avoid giving suggestions that will always be wrong.

Partially address #160463 (this doesn't detect a nameres error caused by referencing type parameter within a derive).

error[E0107]: missing generics for enum `A`
 --> bar.rs:8:6
  |
7 | #[derive(A)]
  |          - it looks like this derive macro might not support items with generic parameters
8 | enum A<T> {
  |      ^
  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

r? @petrochenkov

@rustbot

rustbot commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Aug 7, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_hir_analysis/src/diagnostics/wrong_number_of_generic_args.rs Outdated
Comment thread tests/ui/derives/auxiliary/derive-no-generics.rs
Comment thread tests/run-make/derive-macro-unsupported-type-params/bar.stderr Outdated
Comment thread tests/run-make/derive-macro-unsupported-type-params/rmake.rs Outdated
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 18, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@estebank estebank added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 21, 2026
…chenkov

Do not continue past `rustc_resolve` when encountering duplicated items

Duplicated items cause *lots* of confusing knock down errors. This change side-steps some known ICEs, and reduces the verbosity of crates with duplicated items at the cost of not emitting every error that we could.

Noticed just how problematic these can be while looking at rust-lang#160695, as `#[derive]`s are particularly prone to the kind of confusion these duplicates cause.

Fix rust-lang#120873, fix rust-lang#123690.

r? @petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
…chenkov

Do not continue past `rustc_resolve` when encountering duplicated items

Duplicated items cause *lots* of confusing knock down errors. This change side-steps some known ICEs, and reduces the verbosity of crates with duplicated items at the cost of not emitting every error that we could.

Noticed just how problematic these can be while looking at rust-lang#160695, as `#[derive]`s are particularly prone to the kind of confusion these duplicates cause.

Fix rust-lang#120873, fix rust-lang#123690.

r? @petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
…chenkov

Do not continue past `rustc_resolve` when encountering duplicated items

Duplicated items cause *lots* of confusing knock down errors. This change side-steps some known ICEs, and reduces the verbosity of crates with duplicated items at the cost of not emitting every error that we could.

Noticed just how problematic these can be while looking at rust-lang#160695, as `#[derive]`s are particularly prone to the kind of confusion these duplicates cause.

Fix rust-lang#120873, fix rust-lang#123690.

r? @petrochenkov
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
Rollup merge of #162831 - estebank:duplicated-items, r=petrochenkov

Do not continue past `rustc_resolve` when encountering duplicated items

Duplicated items cause *lots* of confusing knock down errors. This change side-steps some known ICEs, and reduces the verbosity of crates with duplicated items at the cost of not emitting every error that we could.

Noticed just how problematic these can be while looking at #160695, as `#[derive]`s are particularly prone to the kind of confusion these duplicates cause.

Fix #120873, fix #123690.

r? @petrochenkov
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Sep 22, 2026
Do not continue past `rustc_resolve` when encountering duplicated items

Duplicated items cause *lots* of confusing knock down errors. This change side-steps some known ICEs, and reduces the verbosity of crates with duplicated items at the cost of not emitting every error that we could.

Noticed just how problematic these can be while looking at rust-lang/rust#160695, as `#[derive]`s are particularly prone to the kind of confusion these duplicates cause.

Fix rust-lang/rust#120873, fix rust-lang/rust#123690.

r? @petrochenkov
Comment thread compiler/rustc_hir_analysis/src/diagnostics/wrong_number_of_generic_args.rs Outdated
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2026
@rustbot

rustbot commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank

Copy link
Copy Markdown
Contributor Author

@petrochenkov addressed your comments in the last commit (and shortened the label).

@estebank estebank added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 25, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after squashing commits.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2026
@rustbot

rustbot commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

When a derive macro expands the annotated item's name directly using
`quote!`, it keep the item's Span context (instead of having a new
context). This means that the generic Span context machinery which
provides feedback that an error happened due to a derive doesn't kick
in.

If a derive macro isn't written to take into account the existence of
type parameters, an error for "mismatched number of type parameters"
will be emitted. We now detect the case when this happens due to the
derive macro, and customize the output to point that out, as well as
avoid giving suggestions that will always be wrong.
@estebank

Copy link
Copy Markdown
Contributor Author

@bors r=petrochenkov

@rust-bors

rust-bors Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 49276b0 has been approved by petrochenkov

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. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 25, 2026
@rust-bors

rust-bors Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

⚠️ A new commit 565857be19fa709280bf461ae658b5df33863f8a was pushed.

This pull request was unapproved.

@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Sep 26, 2026
@estebank

Copy link
Copy Markdown
Contributor Author

@bors r=petrochenkov

@rust-bors

rust-bors Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 49276b0 has been approved by petrochenkov

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
Rollup of 13 pull requests

Successful merges:

 - #160695 (Detect bad number of generics caused by bad derive)
 - #162883 (No longer auto-inline `f16` and `f128` on well-supported platforms)
 - #163133 ([rustdoc] Fix invalid jump to def link when `#[rustc_allow_incoherent_impl]` is involved)
 - #163215 (Fix suggestion for Option to bool with proper precedence handling)
 - #163266 (More deferred liveness cleanups)
 - #163274 (Support -Z merge-functions with gcc and add stack-protector asm tests)
 - #163290 (Merge `ForceWarning`/`Allow`/`Expect` into `Warning`)
 - #163298 (Don't emit sugg for pub-reexport extern std)
 - #163312 (Add rustdoc regression test for glob import of a crate that re-exports)
 - #163357 (Borrowck cleanups part trois)
 - #161710 (Stabilize `mem::conjure_zst`)
 - #163332 (Add some docs to `Global`)
 - #163362 (simplify ndk compiler test)
@rust-bors
rust-bors Bot merged commit 06568dc into rust-lang:main Sep 26, 2026
26 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
Rollup merge of #160695 - estebank:issue-160463, r=petrochenkov

Detect bad number of generics caused by bad derive

When a derive macro expands the annotated item's name directly using `quote!`, it keep the item's Span context (instead of having a new context). This means that the generic Span context machinery which provides feedback that an error happened due to a derive doesn't kick in.

If a derive macro isn't written to take into account the existence of type parameters, an error for "mismatched number of type parameters" will be emitted. We now detect the case when this happens due to the derive macro, and customize the output to point that out, as well as avoid giving suggestions that will always be wrong.

Partially address #160463 (this doesn't detect a nameres error caused by referencing type parameter within a derive).

```
error[E0107]: missing generics for enum `A`
 --> bar.rs:8:6
  |
7 | #[derive(A)]
  |          - it looks like this derive macro might not support items with generic parameters
8 | enum A<T> {
  |      ^
```

r? @petrochenkov
@rustbot rustbot added this to the 1.100.0 milestone Sep 26, 2026
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 27, 2026
Rollup of 13 pull requests

Successful merges:

 - rust-lang/rust#160695 (Detect bad number of generics caused by bad derive)
 - rust-lang/rust#162883 (No longer auto-inline `f16` and `f128` on well-supported platforms)
 - rust-lang/rust#163133 ([rustdoc] Fix invalid jump to def link when `#[rustc_allow_incoherent_impl]` is involved)
 - rust-lang/rust#163215 (Fix suggestion for Option to bool with proper precedence handling)
 - rust-lang/rust#163266 (More deferred liveness cleanups)
 - rust-lang/rust#163274 (Support -Z merge-functions with gcc and add stack-protector asm tests)
 - rust-lang/rust#163290 (Merge `ForceWarning`/`Allow`/`Expect` into `Warning`)
 - rust-lang/rust#163298 (Don't emit sugg for pub-reexport extern std)
 - rust-lang/rust#163312 (Add rustdoc regression test for glob import of a crate that re-exports)
 - rust-lang/rust#163357 (Borrowck cleanups part trois)
 - rust-lang/rust#161710 (Stabilize `mem::conjure_zst`)
 - rust-lang/rust#163332 (Add some docs to `Global`)
 - rust-lang/rust#163362 (simplify ndk compiler test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs 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.

5 participants