Skip to content

Add on_missing_args#154794

Draft
chenyukang wants to merge 8 commits intorust-lang:mainfrom
chenyukang:yukang-fix-152494-incomplete-macro-args
Draft

Add on_missing_args#154794
chenyukang wants to merge 8 commits intorust-lang:mainfrom
chenyukang:yukang-fix-152494-incomplete-macro-args

Conversation

@chenyukang
Copy link
Copy Markdown
Member

@chenyukang chenyukang commented Apr 4, 2026

Fixes #152494
r? @estebank

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

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

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Apr 4, 2026
@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 4, 2026
@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from 3eb0c39 to e3e5e61 Compare April 4, 2026 02:29
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, great idea. I love this.

A few points

  • you need more tests, for invalid args, on invalid items, cross crate use, format parameters etc.
  • format parameters; do these make sense? Currently they are not handled at all, just silently ignored. In check_attr.rs you can call directive_visit_params and issue diagnostics for them. Would it be useful to have indexed parameters like {1}, {2} to reference arguments? Something to think about, perhaps.
  • 2c on the naming; I don't have a problem with the name perse, I just intensely dislike names that are really long. Also you don't have to use the exact name estebank suggested :) Something shorter would be lovely. Also consider whether this is something we could extend to functions etc?
  • Can you please add some docs about this in the unstable book: https://doc.rust-lang.org/nightly/unstable-book/

View changes since this review

@chenyukang
Copy link
Copy Markdown
Member Author

for naming, maybe on_missing_args maybe better, incase we can extend it in future?

@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from e3e5e61 to 75df17e Compare April 6, 2026 10:48
@chenyukang chenyukang changed the title Add on_incomplete_macro_args Add on_missing_args Apr 6, 2026
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from 75df17e to 2fed49b Compare April 6, 2026 11:57
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 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.

@chenyukang chenyukang marked this pull request as draft April 6, 2026 13:26
@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 6, 2026
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from 595180d to 4898501 Compare April 6, 2026 13:43
@rust-log-analyzer

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.

Thanks for addressing my earlier comments (I cannot click resolve myself 😮‍💨).

Finally, I just remembered that declarative macro derives and attributes exist - can you add tests (and perhaps support for them) as well? See #145208 and #144579.

View changes since this review

Comment on lines +14 to +16
This attribute currently applies to declarative macros such as `macro_rules!` and `pub macro`.
It only affects diagnostics for incomplete invocations; other matcher failures continue to use the
usual macro diagnostics.
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 is not quite what I understood from #152494.

I think @estebank's idea is that it should trigger when no macro arm matches, not just when it is incomplete. For example the current implementation does not trigger with the following invocations:

#[diagnostic::on_missing_args(
    note = "this macro expects a type and a value, like `pair!(u8, 0)`",
    note = "make sure to pass both arguments",
)]
macro_rules! pair {
    //~^ NOTE when calling this macro
    ($ty:ty, $value:expr) => {};
    //~^ NOTE while trying to match `,`
}

pair!(u8, <);
pair!(u8, 0, 42);

I think triggering on no match makes more sense. Maybe then the name should be on_no_match? on_failed_match? on_fallthrough? (please do wait for consensus before renaming everything 😅 ).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, seems 'on_unmatch_args` is a proper name, if we extend this diagnostic to more scenarios, such as like:

#![feature(diagnostic_on_missing_args)]

#[diagnostic::on_missing_args(
    message = "invalid route method",
    note = "this macro expects a action, like `{This}!(get \"/hello\")`"
)]
macro_rules! route {
    (get $path:literal) => {};
}

fn main() {
    route!(post "/");
}

we also can report:

error: invalid route method
  --> tests/ui/diagnostic_namespace/on_missing_args/other_match_macro_error.rs:12:12
   |
 7 | macro_rules! route {
   | ------------------ when calling this macro
...
12 |     route!(post "/");
   |            ^^^^ no rules expected this token in macro call
   |
note: while trying to match `get`
  --> tests/ui/diagnostic_namespace/on_missing_args/other_match_macro_error.rs:8:6
   |
 8 |     (get $path:literal) => {};
   |      ^^^
   = note: this macro expects a action, like `route!(get "/hello")`

error: aborting due to 1 previous error

@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from dc940ba to dde50f5 Compare April 7, 2026 02:22
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-152494-incomplete-macro-args branch from dde50f5 to 2e0c595 Compare April 7, 2026 04:04
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 7, 2026
…JonathanBrouwer

Stabilize check-cfg suggestions for symbol

When I was working on rust-lang#154794, I found a weird CI fail rust-lang#154794 (comment), I finally found this caused by unstable cfg suggestions by using `FxHashSet`(from PR rust-lang#154777). Because my PR by luck insert a new symbol, which makes the final diagnostic order changed.

It's a standalone issue, so it's better to fix in a separate PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 7, 2026
…JonathanBrouwer

Stabilize check-cfg suggestions for symbol

When I was working on rust-lang#154794, I found a weird CI fail rust-lang#154794 (comment), I finally found this caused by unstable cfg suggestions by using `FxHashSet`(from PR rust-lang#154777). Because my PR by luck insert a new symbol, which makes the final diagnostic order changed.

It's a standalone issue, so it's better to fix in a separate PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 7, 2026
…JonathanBrouwer

Stabilize check-cfg suggestions for symbol

When I was working on rust-lang#154794, I found a weird CI fail rust-lang#154794 (comment), I finally found this caused by unstable cfg suggestions by using `FxHashSet`(from PR rust-lang#154777). Because my PR by luck insert a new symbol, which makes the final diagnostic order changed.

It's a standalone issue, so it's better to fix in a separate PR.
rust-timer added a commit that referenced this pull request Apr 7, 2026
Rollup merge of #154886 - chenyukang:yukang-fix-cfg-sugg, r=JonathanBrouwer

Stabilize check-cfg suggestions for symbol

When I was working on #154794, I found a weird CI fail #154794 (comment), I finally found this caused by unstable cfg suggestions by using `FxHashSet`(from PR #154777). Because my PR by luck insert a new symbol, which makes the final diagnostic order changed.

It's a standalone issue, so it's better to fix in a separate PR.
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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide #[diagnostic::on_incomplete_macro_args] to customize macro call errors

5 participants