Improve error message of unexpected colon or comma in json macro

These rules regressed when the unimplemented macro began accepting
formatter arguments.
This commit is contained in:
David Tolnay 2018-06-24 15:16:17 -07:00
parent f12c983942
commit 25b5dfbb6c
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 12 additions and 6 deletions

View File

@ -216,13 +216,13 @@ macro_rules! json_internal {
// Misplaced colon. Trigger a reasonable error message. // Misplaced colon. Trigger a reasonable error message.
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => { (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `:`". // Takes no arguments so "no rules expected the token `:`".
unimplemented!($colon); json_unexpected!($colon);
}; };
// Found a comma inside a key. Trigger a reasonable error message. // Found a comma inside a key. Trigger a reasonable error message.
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => { (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `,`". // Takes no arguments so "no rules expected the token `,`".
unimplemented!($comma); json_unexpected!($comma);
}; };
// Key is fully parenthesized. This avoids clippy double_parens false // Key is fully parenthesized. This avoids clippy double_parens false
@ -280,3 +280,9 @@ macro_rules! json_internal {
$crate::to_value(&$other).unwrap() $crate::to_value(&$other).unwrap()
}; };
} }
#[macro_export]
#[doc(hidden)]
macro_rules! json_unexpected {
() => {};
}

View File

@ -1,8 +1,8 @@
error: expected expression, found `:` error: no rules expected the token `:`
--> $DIR/unexpected_colon.rs:13:13 --> $DIR/unexpected_colon.rs:13:13
| |
13 | json!({ : true }); 13 | json!({ : true });
| ^ expected expression | ^
error: aborting due to previous error error: aborting due to previous error

View File

@ -1,8 +1,8 @@
error: expected expression, found `,` error: no rules expected the token `,`
--> $DIR/unexpected_comma.rs:13:17 --> $DIR/unexpected_comma.rs:13:17
| |
13 | json!({ "a" , "b": true }); 13 | json!({ "a" , "b": true });
| ^ expected expression | ^
error: aborting due to previous error error: aborting due to previous error