Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent type ascription misdiagnosis #688

Merged
merged 3 commits into from Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/macros.rs
Expand Up @@ -224,6 +224,11 @@ macro_rules! json_internal {
json_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};

// Refuse to absorb colon token into key expression.
(@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
json_expect_expr_comma!($($unexpected)+);
};

// Munch a token into the current key.
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
Expand Down Expand Up @@ -290,3 +295,9 @@ macro_rules! json_internal_vec {
macro_rules! json_unexpected {
() => {};
}

#[macro_export]
#[doc(hidden)]
macro_rules! json_expect_expr_comma {
($e:expr , $($tt:tt)*) => {};
}
5 changes: 5 additions & 0 deletions tests/ui/missing_comma.rs
@@ -0,0 +1,5 @@
use serde_json::json;

fn main() {
json!({ "1": "" "2": "" });
}
7 changes: 7 additions & 0 deletions tests/ui/missing_comma.stderr
@@ -0,0 +1,7 @@
error: no rules expected the token `"2"`
--> $DIR/missing_comma.rs:4:21
|
4 | json!({ "1": "" "2": "" });
| -^^^ no rules expected this token in macro call
| |
| help: missing comma here
8 changes: 3 additions & 5 deletions tests/ui/parse_expr.stderr
@@ -1,7 +1,5 @@
error: unexpected end of macro invocation
--> $DIR/parse_expr.rs:4:5
error: no rules expected the token `~`
--> $DIR/parse_expr.rs:4:19
|
4 | json!({ "a" : ~ });
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
| ^ no rules expected this token in macro call