Skip to content

Commit fcddb9a

Browse files
authored
Merge pull request #688 from dtolnay/missingcomma
Prevent type ascription misdiagnosis
2 parents 631ce80 + 9357569 commit fcddb9a

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ macro_rules! json_internal {
224224
json_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
225225
};
226226

227+
// Refuse to absorb colon token into key expression.
228+
(@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
229+
json_expect_expr_comma!($($unexpected)+);
230+
};
231+
227232
// Munch a token into the current key.
228233
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
229234
json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
@@ -290,3 +295,9 @@ macro_rules! json_internal_vec {
290295
macro_rules! json_unexpected {
291296
() => {};
292297
}
298+
299+
#[macro_export]
300+
#[doc(hidden)]
301+
macro_rules! json_expect_expr_comma {
302+
($e:expr , $($tt:tt)*) => {};
303+
}

tests/ui/missing_comma.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use serde_json::json;
2+
3+
fn main() {
4+
json!({ "1": "" "2": "" });
5+
}

tests/ui/missing_comma.stderr

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: no rules expected the token `"2"`
2+
--> $DIR/missing_comma.rs:4:21
3+
|
4+
4 | json!({ "1": "" "2": "" });
5+
| -^^^ no rules expected this token in macro call
6+
| |
7+
| help: missing comma here

tests/ui/parse_expr.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
error: unexpected end of macro invocation
2-
--> $DIR/parse_expr.rs:4:5
1+
error: no rules expected the token `~`
2+
--> $DIR/parse_expr.rs:4:19
33
|
44
4 | json!({ "a" : ~ });
5-
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
6-
|
7-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
5+
| ^ no rules expected this token in macro call

0 commit comments

Comments
 (0)