From 4a948df6f6bdefe561bed320b02ffa0a63a3e749 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 28 Jun 2020 22:24:56 -0700 Subject: [PATCH 1/3] Add ui test of missing comma between object entries --- tests/ui/missing_comma.rs | 5 +++++ tests/ui/missing_comma.stderr | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/ui/missing_comma.rs create mode 100644 tests/ui/missing_comma.stderr diff --git a/tests/ui/missing_comma.rs b/tests/ui/missing_comma.rs new file mode 100644 index 000000000..8818c3e60 --- /dev/null +++ b/tests/ui/missing_comma.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "1": "" "2": "" }); +} diff --git a/tests/ui/missing_comma.stderr b/tests/ui/missing_comma.stderr new file mode 100644 index 000000000..28bb0f827 --- /dev/null +++ b/tests/ui/missing_comma.stderr @@ -0,0 +1,10 @@ +error: expected type, found `""` + --> $DIR/missing_comma.rs:4:18 + | +4 | json!({ "1": "" "2": "" }); + | - ^^ expected type + | | + | tried to parse a type due to this type ascription + | + = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `: ` + = note: see issue #23416 for more information From 0443daa488427c09ad38f76618f45178df245bde Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 28 Jun 2020 22:38:59 -0700 Subject: [PATCH 2/3] Prevent type ascription misdiagnosis --- src/macros.rs | 11 +++++++++++ tests/ui/missing_comma.stderr | 11 +++-------- tests/ui/parse_expr.stderr | 8 +++----- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 9ab58b13f..c61f0e10d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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)*)); @@ -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 ,) => {}; +} diff --git a/tests/ui/missing_comma.stderr b/tests/ui/missing_comma.stderr index 28bb0f827..ba9f551f4 100644 --- a/tests/ui/missing_comma.stderr +++ b/tests/ui/missing_comma.stderr @@ -1,10 +1,5 @@ -error: expected type, found `""` - --> $DIR/missing_comma.rs:4:18 +error: no rules expected the token `"2"` + --> $DIR/missing_comma.rs:4:21 | 4 | json!({ "1": "" "2": "" }); - | - ^^ expected type - | | - | tried to parse a type due to this type ascription - | - = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `: ` - = note: see issue #23416 for more information + | ^^^ no rules expected this token in macro call diff --git a/tests/ui/parse_expr.stderr b/tests/ui/parse_expr.stderr index 107ec59e6..d3240df35 100644 --- a/tests/ui/parse_expr.stderr +++ b/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 From 9357569b1c56b12025c83f4840805bcbc678becd Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 28 Jun 2020 22:46:33 -0700 Subject: [PATCH 3/3] Trigger missing comma rustc suggestion --- src/macros.rs | 2 +- tests/ui/missing_comma.stderr | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index c61f0e10d..5287998b4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -299,5 +299,5 @@ macro_rules! json_unexpected { #[macro_export] #[doc(hidden)] macro_rules! json_expect_expr_comma { - ($e:expr ,) => {}; + ($e:expr , $($tt:tt)*) => {}; } diff --git a/tests/ui/missing_comma.stderr b/tests/ui/missing_comma.stderr index ba9f551f4..ab25b740e 100644 --- a/tests/ui/missing_comma.stderr +++ b/tests/ui/missing_comma.stderr @@ -2,4 +2,6 @@ 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 + | -^^^ no rules expected this token in macro call + | | + | help: missing comma here