Skip to content

Commit f64097a

Browse files
committed
generalize on_missing_args diagnostics
1 parent 80b2743 commit f64097a

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 8 additions & 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| {
@@ -90,9 +90,14 @@ 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+
})
93100
.unwrap_or_default()
94-
} else {
95-
CustomDiagnostic::default()
96101
};
97102

98103
let mut err = match custom_message {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ It lets a macro definition customize the diagnostic that is emitted when an invo
1212
all required arguments were provided.
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+
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.
1718

1819
```rust,compile_fail
1920
#![feature(diagnostic_on_missing_args)]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(diagnostic_on_missing_args)]
2+
3+
#[diagnostic::on_missing_args(
4+
note = "this macro expects a type and a value, like `pair!(u8, 0)`",
5+
note = "make sure to pass both arguments",
6+
)]
7+
macro_rules! pair {
8+
//~^ NOTE when calling this macro
9+
($ty:ty, $value:expr) => {};
10+
//~^ NOTE while trying to match meta-variable `$value:expr`
11+
}
12+
13+
fn main() {
14+
pair!(u8, 0, 42);
15+
//~^ ERROR no rules expected `,`
16+
//~| NOTE no rules expected this token in macro call
17+
//~| NOTE this macro expects a type and a value, like `pair!(u8, 0)`
18+
//~| NOTE make sure to pass both arguments
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: no rules expected `,`
2+
--> $DIR/notes_on_extra_args.rs:14:16
3+
|
4+
LL | macro_rules! pair {
5+
| ----------------- when calling this macro
6+
...
7+
LL | pair!(u8, 0, 42);
8+
| ^ no rules expected this token in macro call
9+
|
10+
note: while trying to match meta-variable `$value:expr`
11+
--> $DIR/notes_on_extra_args.rs:9: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+

0 commit comments

Comments
 (0)