Skip to content

Commit 1e20d34

Browse files
committed
Change map-capacity-counting part of json_internal! macro to not report errors.
Since these errors are already reported by the part of the macro that actually parses the map, it is unnecessary to report them twice, so the capacity-counting part can just return 0 if it encounters an error.
1 parent ab5f90f commit 1e20d34

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

src/macros.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ macro_rules! json_internal {
254254
1 + json_internal!(@object_capacity () ($($rest)*) ($($rest)*))
255255
};
256256

257-
// Current entry followed by unexpected token.
257+
// Current entry followed by unexpected token. The part that parses the values
258+
// will trigger a reasonable error message; here, we just return 0
259+
// so that there is not a duplicated error message.
258260
(@object_capacity entry $unexpected:tt $($rest:tt)*) => {
259-
json_unexpected!($unexpected)
261+
0
260262
};
261263

262264
// Insert the last entry without trailing comma.
@@ -299,29 +301,32 @@ macro_rules! json_internal {
299301
json_internal!(@object_capacity entry)
300302
};
301303

302-
// Missing value for last entry. Trigger a reasonable error message.
304+
// Missing value for last entry. The part that parses the values
305+
// will trigger a reasonable error message; here, we just return 0
306+
// so that there is not a duplicated error message.
303307
(@object_capacity ($($key:tt)+) (:) $copy:tt) => {
304-
// "unexpected end of macro invocation"
305-
json_internal!()
308+
0
306309
};
307310

308-
// Missing colon and value for last entry. Trigger a reasonable error
309-
// message.
311+
// Missing colon and value for last entry. The part that parses the values
312+
// will trigger a reasonable error message; here, we just return 0
313+
// so that there is not a duplicated error message.
310314
(@object_capacity ($($key:tt)+) () $copy:tt) => {
311-
// "unexpected end of macro invocation"
312-
json_internal!()
315+
0
313316
};
314317

315-
// Misplaced colon. Trigger a reasonable error message.
318+
// Misplaced colon. The part that parses the values
319+
// will trigger a reasonable error message; here, we just return 0
320+
// so that there is not a duplicated error message.
316321
(@object_capacity () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
317-
// Takes no arguments so "no rules expected the token `:`".
318-
json_unexpected!($colon)
322+
0
319323
};
320324

321-
// Found a comma inside a key. Trigger a reasonable error message.
325+
// Found a comma inside a key. The part that parses the values
326+
// will trigger a reasonable error message; here, we just return 0
327+
// so that there is not a duplicated error message.
322328
(@object_capacity ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
323-
// Takes no arguments so "no rules expected the token `,`".
324-
json_unexpected!($comma)
329+
0
325330
};
326331

327332
// Key is fully parenthesized. This is not necessary for counting capacity
@@ -331,8 +336,10 @@ macro_rules! json_internal {
331336
// };
332337

333338
// Refuse to absorb colon token into key expression.
339+
// The part that parses the values will trigger a reasonable error message;
340+
// here, we just return 0 so that there is not a duplicated error message.
334341
(@object_capacity ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
335-
json_expect_expr_comma!($($unexpected)+)
342+
0
336343
};
337344

338345
// Munch a token into the current key.

tests/ui/missing_colon.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,3 @@ error: unexpected end of macro invocation
55
| ^^^^^^^^^^^^^^ missing tokens in macro arguments
66
|
77
= note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
8-
9-
error: unexpected end of macro invocation
10-
--> tests/ui/missing_colon.rs:4:5
11-
|
12-
4 | json!({ "a" });
13-
| ^^^^^^^^^^^^^^ missing tokens in macro arguments
14-
|
15-
= note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/missing_value.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,3 @@ error: unexpected end of macro invocation
55
| ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
66
|
77
= note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
8-
9-
error: unexpected end of macro invocation
10-
--> tests/ui/missing_value.rs:4:5
11-
|
12-
4 | json!({ "a" : });
13-
| ^^^^^^^^^^^^^^^^ missing tokens in macro arguments
14-
|
15-
= note: this error originates in the macro `json_internal` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)