Skip to content

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

Open
weiznich wants to merge 3 commits intorust-lang:mainfrom
weiznich:feature/on_unknown_item
Open

Introduce a #[diagnostic::on_unknown] attribute#152901
weiznich wants to merge 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
@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 Apr 5, 2026
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.

I made some changes to the Directive api (see #154858). When you rebase please drop the FormatString::format and args -> Some(args) changes...

View changes since this review

let msg = format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),);

let mut diag = struct_span_code_err!(self.dcx(), span, E0432, "{msg}");
let default_message =
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.

...and here you'll need to do something like this:

let args = FormatArgs {
    this: paths.join(", ").collect(),
    // Unused
    this_sugared: String::new(),
    // Unused
    item_context: "",
    // Unused
    generic_args: Vec::new(),
};
let CustomDiagnostic { message, label, notes, parent_label: _ }  = directive.eval(None, &args);

@weiznich weiznich force-pushed the feature/on_unknown_item branch from c0fcdda to d65526f Compare April 8, 2026 06:37
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 2026

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.

@rust-log-analyzer

This comment has been minimized.

@weiznich weiznich force-pushed the feature/on_unknown_item branch from d65526f to a23ba50 Compare April 8, 2026 07:48
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.

Thanks for the rename. Can you update the PR title and description as well?

View changes since this review

pub enum FormatWarning {
PositionalArgument { span: Span, help: String },
InvalidSpecifier { name: String, span: Span },
DisallowedPlaceholder { span: Span },
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.

This enum variant is never constructed.

One of your previous commits had

+                if matches!(mode, Mode::DiagnosticOnUnknownItem) {
+                    warnings.push(FormatWarning::DisallowedPlaceholder { span });
+                }

but I cannot find it anymore; you may have accidentally dropped it.

Please put this in the match in fn parse_arg and make it return FormatArg::AsIs. And add a test for it (idk if you had one, maybe it was also dropped?).

@weiznich
Copy link
Copy Markdown
Contributor Author

weiznich commented Apr 8, 2026

I do not have much capacity or motivation left to do yet another set of changes on top of this.
Honestly it have been quite a frustrating experience to try getting this accepted over the past month. I can understand that you have other priorities but this PR got now rewritten three times to satisfy yet another round of feedback on topics that didn't change from the initial version. Additionally it was stuck on feedback from the reviewer for quite a lot of time, while in the meanwhile the same persons pushed changes that required several rather fundamental changes to the PR. This is then communicated with another tense comment that essentially says: Now change this again. Again I can understand that you might have other priorities and also I see that's your changes make things easier. It still is super frustrating for me to work in that environment.

@weiznich weiznich closed this Apr 8, 2026
@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 Apr 8, 2026
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented Apr 8, 2026

I'm sorry to hear that. I definitely understand the frustration as this part of the codebase is also very merge conflict prone. I still really want to see this happen, would you mind if I continue your work in the future?

@weiznich
Copy link
Copy Markdown
Contributor Author

weiznich commented Apr 8, 2026

To clarify that again: The main point of frustration is not resolving the merge conflicts. The main point of frustration is that it feels you you as reviewer continuously shift the goal post on this by bringing up yet another thing that was there from the beginning and now suddenly needs to be changed. That in combination with out of my perspective delayed reviews and resolutions to open questions + merge conflicts from changes by exactly these reviews that delay the review in favor of the their changes make it very painful to continue working on this. It just feels disrespectful and especially not welcome to me. And to be extra clear: The question about the naming was open for more than 4 weeks with noone on the reviewers side even seem to care about it more than "that name doesn't sound perfect". You certainly can do such a thing, but please respect that at some point people just walk away and stop trying to contribute at all.

I'm still open to consider finishing that at some point, but at this point that would require a honest, final list of what still needs to be done that doesn't contain yet another rewrite. Also at least acknowledging that you (not as person but as reviewers) could have done better might help with the motivation.

weiznich added 3 commits April 9, 2026 15:21
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 changed the title Introduce a #[diagnostic::on_unknown_item] attribute Introduce a #[diagnostic::on_unknown] attribute Apr 9, 2026
@weiznich weiznich reopened this Apr 9, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2026
@weiznich weiznich force-pushed the feature/on_unknown_item branch from a23ba50 to 93f9aec Compare April 9, 2026 13:35
@jdonszelmann
Copy link
Copy Markdown
Contributor

jdonszelmann commented Apr 9, 2026

This is quite a large change, which means review takes a bit and on re-review, new things can be spotted. In addition, both of us were reviewing, so not our opinions may align. I think that can be good actually: we can discuss how we want things to work. I think this is simply how review works in a large project with a lot of people involved.

Anyway, looks good to me as is.

@bors r=jdonszelmann,mejrs

Thanks for all the review work @mejrs and thanks for the implementation @weiznich

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 9, 2026

📌 Commit 93f9aec has been approved by jdonszelmann,mejrs

It is now in the queue for this repository.

⚠️ The following reviewer(s) could not be found: mejrs

@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 Apr 9, 2026
@weiznich
Copy link
Copy Markdown
Contributor Author

weiznich commented Apr 9, 2026

@jdonszelmann I can understand why it looks like that from your point of view, but let me assure that this hasn't been the case in the past. This kind of delay and bringing up of new remarks again and again is something that did not happen in the past. I write that as a person that contributes various larger PR's over the last years. I'm currently honestly considering if I want to continue that in the future, given that experience here.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 9, 2026
…r=jdonszelmann,mejrs

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
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 9, 2026
…r=jdonszelmann,mejrs

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

6 participants