Skip to content

Commit dde50f5

Browse files
committed
extend on_missing_args to no-match
1 parent 80b2743 commit dde50f5

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(super) fn failed_to_match_macro(
7676
let span = token.span.substitute_dummy(sp);
7777
let CustomDiagnostic {
7878
message: custom_message, label: custom_label, notes: custom_notes, ..
79-
} = if matches!(token.kind, token::Eof) {
79+
} = {
8080
let macro_name = name.to_string();
8181
on_missing_args
8282
.map(|directive| {
@@ -91,8 +91,6 @@ pub(super) fn failed_to_match_macro(
9191
)
9292
})
9393
.unwrap_or_default()
94-
} else {
95-
CustomDiagnostic::default()
9694
};
9795

9896
let mut err = match custom_message {

src/doc/unstable-book/src/language-features/diagnostic-on-missing-args.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ The tracking issue for this feature is: [#152494]
88

99
The `diagnostic_on_missing_args` feature adds the
1010
`#[diagnostic::on_missing_args(...)]` attribute for declarative macros.
11-
It lets a macro definition customize the diagnostic that is emitted when an invocation ends before
12-
all required arguments were provided.
11+
It lets a macro definition customize diagnostics for matcher failures after all arms have been
12+
tried, such as incomplete invocations or trailing extra arguments.
1313

1414
This attribute currently applies to declarative macros such as `macro_rules!` and `pub macro`.
15-
It only affects diagnostics for incomplete invocations; other matcher failures continue to use the
16-
usual macro diagnostics.
15+
It is currently used for errors emitted by declarative macro matching itself; fragment parser
16+
errors still use their existing diagnostics.
1717

1818
```rust,compile_fail
1919
#![feature(diagnostic_on_missing_args)]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(diagnostic_on_missing_args)]
2+
3+
#[diagnostic::on_missing_args(
4+
message = "{This}! expects exactly two arguments",
5+
label = "unexpected extra input starts here",
6+
note = "this macro expects a type and a value, like `pair!(u8, 0)`",
7+
note = "make sure to pass both arguments",
8+
)]
9+
macro_rules! pair {
10+
//~^ NOTE when calling this macro
11+
($ty:ty, $value:expr) => {};
12+
//~^ NOTE while trying to match meta-variable `$value:expr`
13+
}
14+
15+
fn main() {
16+
pair!(u8, 0, 42);
17+
//~^ ERROR pair! expects exactly two arguments
18+
//~| NOTE unexpected extra input starts here
19+
//~| NOTE this macro expects a type and a value, like `pair!(u8, 0)`
20+
//~| NOTE make sure to pass both arguments
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: pair! expects exactly two arguments
2+
--> $DIR/notes_on_extra_args.rs:16:16
3+
|
4+
LL | macro_rules! pair {
5+
| ----------------- when calling this macro
6+
...
7+
LL | pair!(u8, 0, 42);
8+
| ^ unexpected extra input starts here
9+
|
10+
note: while trying to match meta-variable `$value:expr`
11+
--> $DIR/notes_on_extra_args.rs:11:14
12+
|
13+
LL | ($ty:ty, $value:expr) => {};
14+
| ^^^^^^^^^^^
15+
= note: this macro expects a type and a value, like `pair!(u8, 0)`
16+
= note: make sure to pass both arguments
17+
18+
error: aborting due to 1 previous error
19+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(diagnostic_on_missing_args)]
2+
3+
#[diagnostic::on_missing_args(
4+
message = "invalid route method",
5+
note = "this macro expects a action, like `{This}!(get \"/hello\")`"
6+
)]
7+
macro_rules! route {
8+
(get $path:literal) => {};
9+
}
10+
11+
fn main() {
12+
route!(post "/");
13+
//~^ ERROR invalid route method
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: invalid route method
2+
--> $DIR/other_match_macro_error.rs:12:12
3+
|
4+
LL | macro_rules! route {
5+
| ------------------ when calling this macro
6+
...
7+
LL | route!(post "/");
8+
| ^^^^ no rules expected this token in macro call
9+
|
10+
note: while trying to match `get`
11+
--> $DIR/other_match_macro_error.rs:8:6
12+
|
13+
LL | (get $path:literal) => {};
14+
| ^^^
15+
= note: this macro expects a action, like `route!(get "/hello")`
16+
17+
error: aborting due to 1 previous error
18+

0 commit comments

Comments
 (0)