Skip to content

Comments

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

Open
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:feature/on_unknown_item
Open

Introduce a #[diagnostic::on_unknown_item] attribute#152901
weiznich wants to merge 1 commit intorust-lang:mainfrom
weiznich:feature/on_unknown_item

Conversation

@weiznich
Copy link
Contributor

@weiznich weiznich commented Feb 20, 2026

This PR introduces a #[diagnostic::on_unknown_item] 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
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
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.

This PR introduces a `#[diagnostic::on_unknown_item]` 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.
@weiznich weiznich force-pushed the feature/on_unknown_item branch from a9dd689 to c87fc9e Compare February 20, 2026 14:37
@jdonszelmann
Copy link
Contributor

@weiznich maybe this should be rebased on #151558

@weiznich
Copy link
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.

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants