Skip to content

Commit 0443daa

Browse files
committed
Prevent type ascription misdiagnosis
1 parent 4a948df commit 0443daa

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/macros.rs

Lines changed: 11 additions & 0 deletions
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 ,) => {};
303+
}

tests/ui/missing_comma.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
error: expected type, found `""`
2-
--> $DIR/missing_comma.rs:4:18
1+
error: no rules expected the token `"2"`
2+
--> $DIR/missing_comma.rs:4:21
33
|
44
4 | json!({ "1": "" "2": "" });
5-
| - ^^ expected type
6-
| |
7-
| tried to parse a type due to this type ascription
8-
|
9-
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
10-
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
5+
| ^^^ no rules expected this token in macro call

tests/ui/parse_expr.stderr

Lines changed: 3 additions & 5 deletions
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)