Skip to content

Commit 611b2a4

Browse files
authored
Merge pull request #1165 from serde-rs/jsonmac
Eliminate local_inner_macros in favor of non-ident macro paths
2 parents eca2658 + 7633cb7 commit 611b2a4

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

src/macros.rs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
/// "comma -->",
5151
/// ]);
5252
/// ```
53-
#[macro_export(local_inner_macros)]
53+
#[macro_export]
5454
macro_rules! json {
5555
// Hide distracting implementation details from the generated rustdoc.
5656
($($json:tt)+) => {
57-
json_internal!($($json)+)
57+
$crate::json_internal!($($json)+)
5858
};
5959
}
6060

@@ -65,7 +65,7 @@ macro_rules! json {
6565
//
6666
// Changes are fine as long as `json_internal!` does not call any new helper
6767
// macros and can still be invoked as `json_internal!($($json)+)`.
68-
#[macro_export(local_inner_macros)]
68+
#[macro_export]
6969
#[doc(hidden)]
7070
macro_rules! json_internal {
7171
//////////////////////////////////////////////////////////////////////////
@@ -77,57 +77,57 @@ macro_rules! json_internal {
7777

7878
// Done with trailing comma.
7979
(@array [$($elems:expr,)*]) => {
80-
json_internal_vec![$($elems,)*]
80+
vec![$($elems,)*]
8181
};
8282

8383
// Done without trailing comma.
8484
(@array [$($elems:expr),*]) => {
85-
json_internal_vec![$($elems),*]
85+
vec![$($elems),*]
8686
};
8787

8888
// Next element is `null`.
8989
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
90-
json_internal!(@array [$($elems,)* json_internal!(null)] $($rest)*)
90+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!(null)] $($rest)*)
9191
};
9292

9393
// Next element is `true`.
9494
(@array [$($elems:expr,)*] true $($rest:tt)*) => {
95-
json_internal!(@array [$($elems,)* json_internal!(true)] $($rest)*)
95+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!(true)] $($rest)*)
9696
};
9797

9898
// Next element is `false`.
9999
(@array [$($elems:expr,)*] false $($rest:tt)*) => {
100-
json_internal!(@array [$($elems,)* json_internal!(false)] $($rest)*)
100+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!(false)] $($rest)*)
101101
};
102102

103103
// Next element is an array.
104104
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
105-
json_internal!(@array [$($elems,)* json_internal!([$($array)*])] $($rest)*)
105+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!([$($array)*])] $($rest)*)
106106
};
107107

108108
// Next element is a map.
109109
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
110-
json_internal!(@array [$($elems,)* json_internal!({$($map)*})] $($rest)*)
110+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!({$($map)*})] $($rest)*)
111111
};
112112

113113
// Next element is an expression followed by comma.
114114
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
115-
json_internal!(@array [$($elems,)* json_internal!($next),] $($rest)*)
115+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!($next),] $($rest)*)
116116
};
117117

118118
// Last element is an expression with no trailing comma.
119119
(@array [$($elems:expr,)*] $last:expr) => {
120-
json_internal!(@array [$($elems,)* json_internal!($last)])
120+
$crate::json_internal!(@array [$($elems,)* $crate::json_internal!($last)])
121121
};
122122

123123
// Comma after the most recent element.
124124
(@array [$($elems:expr),*] , $($rest:tt)*) => {
125-
json_internal!(@array [$($elems,)*] $($rest)*)
125+
$crate::json_internal!(@array [$($elems,)*] $($rest)*)
126126
};
127127

128128
// Unexpected token after most recent element.
129129
(@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
130-
json_unexpected!($unexpected)
130+
$crate::json_unexpected!($unexpected)
131131
};
132132

133133
//////////////////////////////////////////////////////////////////////////
@@ -146,12 +146,12 @@ macro_rules! json_internal {
146146
// Insert the current entry followed by trailing comma.
147147
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
148148
let _ = $object.insert(($($key)+).into(), $value);
149-
json_internal!(@object $object () ($($rest)*) ($($rest)*));
149+
$crate::json_internal!(@object $object () ($($rest)*) ($($rest)*));
150150
};
151151

152152
// Current entry followed by unexpected token.
153153
(@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
154-
json_unexpected!($unexpected);
154+
$crate::json_unexpected!($unexpected);
155155
};
156156

157157
// Insert the last entry without trailing comma.
@@ -161,78 +161,78 @@ macro_rules! json_internal {
161161

162162
// Next value is `null`.
163163
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
164-
json_internal!(@object $object [$($key)+] (json_internal!(null)) $($rest)*);
164+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!(null)) $($rest)*);
165165
};
166166

167167
// Next value is `true`.
168168
(@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
169-
json_internal!(@object $object [$($key)+] (json_internal!(true)) $($rest)*);
169+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!(true)) $($rest)*);
170170
};
171171

172172
// Next value is `false`.
173173
(@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
174-
json_internal!(@object $object [$($key)+] (json_internal!(false)) $($rest)*);
174+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!(false)) $($rest)*);
175175
};
176176

177177
// Next value is an array.
178178
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
179-
json_internal!(@object $object [$($key)+] (json_internal!([$($array)*])) $($rest)*);
179+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!([$($array)*])) $($rest)*);
180180
};
181181

182182
// Next value is a map.
183183
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
184-
json_internal!(@object $object [$($key)+] (json_internal!({$($map)*})) $($rest)*);
184+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!({$($map)*})) $($rest)*);
185185
};
186186

187187
// Next value is an expression followed by comma.
188188
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
189-
json_internal!(@object $object [$($key)+] (json_internal!($value)) , $($rest)*);
189+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!($value)) , $($rest)*);
190190
};
191191

192192
// Last value is an expression with no trailing comma.
193193
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
194-
json_internal!(@object $object [$($key)+] (json_internal!($value)));
194+
$crate::json_internal!(@object $object [$($key)+] ($crate::json_internal!($value)));
195195
};
196196

197197
// Missing value for last entry. Trigger a reasonable error message.
198198
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
199199
// "unexpected end of macro invocation"
200-
json_internal!();
200+
$crate::json_internal!();
201201
};
202202

203203
// Missing colon and value for last entry. Trigger a reasonable error
204204
// message.
205205
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
206206
// "unexpected end of macro invocation"
207-
json_internal!();
207+
$crate::json_internal!();
208208
};
209209

210210
// Misplaced colon. Trigger a reasonable error message.
211211
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
212212
// Takes no arguments so "no rules expected the token `:`".
213-
json_unexpected!($colon);
213+
$crate::json_unexpected!($colon);
214214
};
215215

216216
// Found a comma inside a key. Trigger a reasonable error message.
217217
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
218218
// Takes no arguments so "no rules expected the token `,`".
219-
json_unexpected!($comma);
219+
$crate::json_unexpected!($comma);
220220
};
221221

222222
// Key is fully parenthesized. This avoids clippy double_parens false
223223
// positives because the parenthesization may be necessary here.
224224
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
225-
json_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
225+
$crate::json_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
226226
};
227227

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

233233
// Munch a token into the current key.
234234
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
235-
json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
235+
$crate::json_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
236236
};
237237

238238
//////////////////////////////////////////////////////////////////////////
@@ -254,11 +254,11 @@ macro_rules! json_internal {
254254
};
255255

256256
([]) => {
257-
$crate::Value::Array(json_internal_vec![])
257+
$crate::Value::Array(vec![])
258258
};
259259

260260
([ $($tt:tt)+ ]) => {
261-
$crate::Value::Array(json_internal!(@array [] $($tt)+))
261+
$crate::Value::Array($crate::json_internal!(@array [] $($tt)+))
262262
};
263263

264264
({}) => {
@@ -268,7 +268,7 @@ macro_rules! json_internal {
268268
({ $($tt:tt)+ }) => {
269269
$crate::Value::Object({
270270
let mut object = $crate::Map::new();
271-
json_internal!(@object object () ($($tt)+) ($($tt)+));
271+
$crate::json_internal!(@object object () ($($tt)+) ($($tt)+));
272272
object
273273
})
274274
};
@@ -280,9 +280,8 @@ macro_rules! json_internal {
280280
};
281281
}
282282

283-
// The json_internal macro above cannot invoke vec directly because it uses
284-
// local_inner_macros. A vec invocation there would resolve to $crate::vec.
285-
// Instead invoke vec here outside of local_inner_macros.
283+
// Used by old versions of Rocket.
284+
// Unused since https://github.com/rwf2/Rocket/commit/c74bcfd40a47b35330db6cafb88e4f3da83e0d17
286285
#[macro_export]
287286
#[doc(hidden)]
288287
macro_rules! json_internal_vec {

tests/ui/missing_colon.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ note: while trying to match `@`
99
|
1010
| (@array [$($elems:expr,)*]) => {
1111
| ^
12-
= note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the macro `$crate::json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/missing_value.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ note: while trying to match `@`
99
|
1010
| (@array [$($elems:expr,)*]) => {
1111
| ^
12-
= note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the macro `$crate::json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)