Skip to content

Introduce a #[diagnostic::on_unknown] attribute#152901

Merged
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
weiznich:feature/on_unknown_item
Apr 10, 2026
Merged

Introduce a #[diagnostic::on_unknown] attribute#152901
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
weiznich:feature/on_unknown_item

Conversation

@weiznich
Copy link
Copy Markdown
Contributor

@weiznich weiznich commented Feb 20, 2026

View all comments

This PR introduces a #[diagnostic::on_unknown] attribute that allows crate authors to customize the error messages emitted by unresolved imports. The main usecase for this is using this attribute as part of a proc macro that expects a certain external module structure to exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel, that expect to refer to a tabe module. That is done either implicitly (via the name of the type with the derive) or explicitly by the user. This attribute would allow us to improve the error message in both cases:

  • For the implicit case we could explicity call out our assumptions (turning the name into lower case, adding an s in the end)
  • point to the explicit variant as alternative
  • For the explicit variant we would add additional notes to tell the user why this is happening and what they should look for to fix the problem (be more explicit about certain diesel specific assumptions of the module structure)

I assume that similar use-cases exist for other proc-macros as well, therefore I decided to put in the work implementing this new attribute. I would also assume that this is likely not useful for std-lib internal usage.

related #152900 and #128674

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 20, 2026

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Feb 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 20, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
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 68 candidates
  • Random selection from 15 candidates

@rust-log-analyzer

This comment has been minimized.

@weiznich weiznich force-pushed the feature/on_unknown_item branch from 5c63f6f to a9dd689 Compare February 20, 2026 13:33
@rust-log-analyzer

This comment has been minimized.

@weiznich weiznich force-pushed the feature/on_unknown_item branch from a9dd689 to c87fc9e Compare February 20, 2026 14:37
@jdonszelmann
Copy link
Copy Markdown
Contributor

@weiznich maybe this should be rebased on #151558

@weiznich
Copy link
Copy Markdown
Contributor Author

I can try this, although I need to read up on how the new infrastructure works and check if it's possible to use this inside of the name resolution stage. Somehow certain things (like lints) act weirdly there.

@jdonszelmann
Copy link
Copy Markdown
Contributor

The attribute refactor is pretty much finished, which means all old style parsers at this point have been removed from the compiler. There are many examples of new-parsing-infrastructure attribute parsers that work at this stage of the compiler (and none more that work using the old system). I don't think I want to accept any new old-style attribute parsers into the compiler anymore for that reason.

r? me

@rustbot rustbot assigned jdonszelmann and unassigned nnethercote Feb 23, 2026
@weiznich
Copy link
Copy Markdown
Contributor Author

@jdonszelmann I can totally understand that you don't want to accept any attributes using the old style. Your comment as currently written is still not useful for me as person that contributes only from time to time to the compiler and doesn't keep up with all the internal changes all the time. I get that I need to change something, but it is really unclear for me:

  • What need to change exactly
  • How the new attribute parsing framework works
  • If the new attribute parsing framework would allow me to access the relevant attribute information during name resolution, which as far as I know is kind of a strange place as it is between compiler phases and doesn't have access to the full set of information yet

It's especially not helpful to write that "There are many examples of new-parsing-infrastructure attribute parsers" without even providing a link to one of them.

Do you have any documentation or other hints where to get these information from? Otherwise I fear it's impossible for me to satisfy these requests with the limited amount of time I'm able to spend on this change.

@jdonszelmann
Copy link
Copy Markdown
Contributor

Fair enough, take a look at how we handle MacroUse in rustc_resolve (grep for it). It should look similar to that. For that attribute we similarly have less information available, but it works using parse_limited.

@rust-bors

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

#151558 has merged now so you can rebase on that.

What need to change exactly

I have some review comments as well. Thanks for continuing this work by the way :)

View changes since this review

@jdonszelmann
Copy link
Copy Markdown
Contributor

@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 Feb 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 26, 2026

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

@weiznich weiznich force-pushed the feature/on_unknown_item branch from c87fc9e to fad08dd Compare March 6, 2026 10:15
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 6, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot

This comment has been minimized.

@weiznich weiznich force-pushed the feature/on_unknown_item branch from fad08dd to 6319918 Compare March 6, 2026 10:19
@rustbot

This comment has been minimized.

@weiznich
Copy link
Copy Markdown
Contributor Author

weiznich commented Mar 6, 2026

@rustbot ready

The new attribute infrastructure is really nice to work with as soon as you get your head around it. Thanks for all the effort that want into it.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 6, 2026
@rust-bors rust-bors bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 10, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 10, 2026

This pull request was unapproved due to being closed.

@weiznich weiznich reopened this Apr 10, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 10, 2026
@weiznich
Copy link
Copy Markdown
Contributor Author

I just would like to apologize for the chaos here. I acknowledge that my wording in the last few comments should have been better. I'm sorry for that, I should have done it better. I still stand to the point that it felt for me like the reviewers are continuously shifting the goal post.

@jdonszelmann
Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 10, 2026

📌 Commit 97da819 has been approved by jdonszelmann

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. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 10, 2026
…r=jdonszelmann

Introduce a `#[diagnostic::on_unknown]` attribute

This PR introduces a `#[diagnostic::on_unknown]` attribute that allows crate authors to customize the error messages emitted by unresolved imports. The main usecase for this is using this attribute as part of a proc macro that expects a certain external module structure to exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel, that expect to refer to a `tabe` module. That is done either implicitly (via the name of the type with the derive) or explicitly by the user. This attribute would allow us to improve the error message in both cases:

* For the implicit case we could explicity call out our assumptions (turning the name into lower case, adding an `s` in the end)
+ point to the explicit variant as alternative
* For the explicit variant we would add additional notes to tell the user why this is happening and what they should look for to fix the problem (be more explicit about certain diesel specific assumptions of the module structure)

I assume that similar use-cases exist for other proc-macros as well, therefore I decided to put in the work implementing this new attribute. I would also assume that this is likely not useful for std-lib internal usage.

related rust-lang#152900 and rust-lang#128674
rust-bors bot pushed a commit that referenced this pull request Apr 10, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #152901 (Introduce a `#[diagnostic::on_unknown]` attribute)
 - #155078 (Reject dangling attributes in where clauses)
 - #154449 (Invert dependency between `rustc_errors` and `rustc_abi`.)
 - #154646 (Add suggestion to `.to_owned()` used on `Cow` when borrowing)
 - #154993 (compiletest: pass -Zunstable-options for unpretty and no-codegen paths)
 - #155097 (Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span`)
@rust-bors rust-bors bot merged commit dbf9492 into rust-lang:main Apr 10, 2026
22 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Apr 10, 2026
rust-timer added a commit that referenced this pull request Apr 10, 2026
Rollup merge of #152901 - weiznich:feature/on_unknown_item, r=jdonszelmann

Introduce a `#[diagnostic::on_unknown]` attribute

This PR introduces a `#[diagnostic::on_unknown]` attribute that allows crate authors to customize the error messages emitted by unresolved imports. The main usecase for this is using this attribute as part of a proc macro that expects a certain external module structure to exist or certain dependencies to be there.

For me personally the motivating use-case are several derives in diesel, that expect to refer to a `tabe` module. That is done either implicitly (via the name of the type with the derive) or explicitly by the user. This attribute would allow us to improve the error message in both cases:

* For the implicit case we could explicity call out our assumptions (turning the name into lower case, adding an `s` in the end)
+ point to the explicit variant as alternative
* For the explicit variant we would add additional notes to tell the user why this is happening and what they should look for to fix the problem (be more explicit about certain diesel specific assumptions of the module structure)

I assume that similar use-cases exist for other proc-macros as well, therefore I decided to put in the work implementing this new attribute. I would also assume that this is likely not useful for std-lib internal usage.

related #152900 and #128674
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@rust-timer build f6ea057

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (f6ea057): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
0.3% [0.2%, 0.6%] 12
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.4%, 0.4%] 1

Max RSS (memory usage)

Results (primary 2.4%, secondary -3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
7.4% [7.4%, 7.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.5% [-2.5%, -2.5%] 1
Improvements ✅
(secondary)
-3.8% [-5.5%, -2.0%] 2
All ❌✅ (primary) 2.4% [-2.5%, 7.4%] 2

Cycles

Results (secondary -4.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.1% [-7.1%, -2.3%] 8
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 491.232s -> 490.616s (-0.13%)
Artifact size: 394.07 MiB -> 394.13 MiB (0.01%)

@rustbot rustbot added the perf-regression Performance regression. label Apr 11, 2026
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

JonathanBrouwer commented Apr 11, 2026

This causes the regression in #155115

I wonder what caused the regression? The regression is minor enough to probably just accept tho

jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 12, 2026
Reduce size of `ImportData`

Perhaps this will undo the regression caused by rust-lang#152901
rust-timer added a commit that referenced this pull request Apr 12, 2026
Rollup merge of #155167 - mejrs:box_directive, r=Kivooeo

Reduce size of `ImportData`

Perhaps this will undo the regression caused by #152901
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 13, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#152901 (Introduce a `#[diagnostic::on_unknown]` attribute)
 - rust-lang/rust#155078 (Reject dangling attributes in where clauses)
 - rust-lang/rust#154449 (Invert dependency between `rustc_errors` and `rustc_abi`.)
 - rust-lang/rust#154646 (Add suggestion to `.to_owned()` used on `Cow` when borrowing)
 - rust-lang/rust#154993 (compiletest: pass -Zunstable-options for unpretty and no-codegen paths)
 - rust-lang/rust#155097 (Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. 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.

10 participants