Skip to content

Commit dc940ba

Browse files
committed
extend on_missing_args to no-match
1 parent f64097a commit dc940ba

6 files changed

Lines changed: 44 additions & 18 deletions

File tree

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ pub(super) fn failed_to_match_macro(
9090
},
9191
)
9292
})
93-
.map(|diag| {
94-
if matches!(token.kind, token::Eof) {
95-
diag
96-
} else {
97-
CustomDiagnostic { message: None, label: None, ..diag }
98-
}
99-
})
10093
.unwrap_or_default()
10194
};
10295

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +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-
Custom `message` and `label` are only used for incomplete invocations.
16-
Custom `note`s are also emitted for other matcher failures where no macro arm matches, such as
17-
trailing extra arguments.
15+
It is currently used for errors emitted by declarative macro matching itself; fragment parser
16+
errors still use their existing diagnostics.
1817

1918
```rust,compile_fail
2019
#![feature(diagnostic_on_missing_args)]

tests/ui/diagnostic_namespace/on_missing_args/notes_on_extra_args.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![feature(diagnostic_on_missing_args)]
22

33
#[diagnostic::on_missing_args(
4+
message = "{This}! expects exactly two arguments",
5+
label = "unexpected extra input starts here",
46
note = "this macro expects a type and a value, like `pair!(u8, 0)`",
57
note = "make sure to pass both arguments",
68
)]
@@ -12,8 +14,8 @@ macro_rules! pair {
1214

1315
fn main() {
1416
pair!(u8, 0, 42);
15-
//~^ ERROR no rules expected `,`
16-
//~| NOTE no rules expected this token in macro call
17+
//~^ ERROR pair! expects exactly two arguments
18+
//~| NOTE unexpected extra input starts here
1719
//~| NOTE this macro expects a type and a value, like `pair!(u8, 0)`
1820
//~| NOTE make sure to pass both arguments
1921
}

tests/ui/diagnostic_namespace/on_missing_args/notes_on_extra_args.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: no rules expected `,`
2-
--> $DIR/notes_on_extra_args.rs:14:16
1+
error: pair! expects exactly two arguments
2+
--> $DIR/notes_on_extra_args.rs:16:16
33
|
44
LL | macro_rules! pair {
55
| ----------------- when calling this macro
66
...
77
LL | pair!(u8, 0, 42);
8-
| ^ no rules expected this token in macro call
8+
| ^ unexpected extra input starts here
99
|
1010
note: while trying to match meta-variable `$value:expr`
11-
--> $DIR/notes_on_extra_args.rs:9:14
11+
--> $DIR/notes_on_extra_args.rs:11:14
1212
|
1313
LL | ($ty:ty, $value:expr) => {};
1414
| ^^^^^^^^^^^
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)