Skip to content

Commit d496220

Browse files
ubnt-intrepidLegNeato
authored andcommitted
Use local_inner_macros to all helper macros (#233)
This enables Rust2018-style macro imports: ```rust use juniper::graphql_object; graphql_object!(User: () |&self| { ... }); ``` In the future when dropping compatibility with pre-2018 compilers, this can be replaced with the explicit `$crate` prefixes instead of `local_inner_macros`. In `macro_rules!` with the annotation `local_inner_macros`, all of macro calls are transformed into `$crate::builtin!(...)` (See [1] for details). Therefore, name resolutions of built-in macros are not perfomed correctly. To avoid this, some helper macros are introduced to make built-in macros be interpreted as crate-local macros. [1]: rust-lang/rust#53464 (comment)
1 parent 273edc1 commit d496220

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

juniper/src/macros/args.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[doc(hidden)]
2-
#[macro_export]
2+
#[macro_export(local_inner_macros)]
33
macro_rules! __graphql__args {
44
// Internal type conversion
55
( @as_expr, $e:expr) => { $e };
@@ -27,7 +27,7 @@ macro_rules! __graphql__args {
2727
$name:ident $(= $default:tt)* : $ty:ty $(as $desc:tt)*, $($rest:tt)*
2828
) => {
2929
let $name: $ty = $args
30-
.get(&$crate::to_camel_case(stringify!($name)))
30+
.get(&$crate::to_camel_case(__graphql__stringify!($name)))
3131
.expect("Argument missing - validation must have failed");
3232
__graphql__args!(@assign_arg_vars, $args, $executorvar, $($rest)*);
3333
};
@@ -38,7 +38,7 @@ macro_rules! __graphql__args {
3838
$name:ident $(= $default:tt)* : $ty:ty $(as $desc:expr)*
3939
) => {
4040
let $name: $ty = $args
41-
.get(&$crate::to_camel_case(stringify!($name)))
41+
.get(&$crate::to_camel_case(__graphql__stringify!($name)))
4242
.expect("Argument missing - validation must have failed");
4343
};
4444

@@ -75,7 +75,7 @@ macro_rules! __graphql__args {
7575
$reg:expr, $base:expr, $info:expr, ( $name:ident = $default:tt : $t:ty )
7676
) => {
7777
$base.argument($reg.arg_with_default::<$t>(
78-
&$crate::to_camel_case(stringify!($name)),
78+
&$crate::to_camel_case(__graphql__stringify!($name)),
7979
&__graphql__args!(@as_expr, $default), $info))
8080
};
8181

@@ -87,7 +87,7 @@ macro_rules! __graphql__args {
8787
@apply_args,
8888
$reg,
8989
$base.argument($reg.arg_with_default::<$t>(
90-
&$crate::to_camel_case(stringify!($name)),
90+
&$crate::to_camel_case(__graphql__stringify!($name)),
9191
&__graphql__args!(@as_expr, $default), $info)),
9292
$info,
9393
( $($rest)* ))
@@ -102,7 +102,7 @@ macro_rules! __graphql__args {
102102
@apply_args,
103103
$reg,
104104
$base.argument($reg.arg_with_default::<$t>(
105-
&$crate::to_camel_case(stringify!($name)),
105+
&$crate::to_camel_case(__graphql__stringify!($name)),
106106
&__graphql__args!(@as_expr, $default), $info)
107107
.description($desc)),
108108
$info,
@@ -114,7 +114,7 @@ macro_rules! __graphql__args {
114114
$reg:expr, $base:expr, $info:expr, ( $name:ident : $t:ty )
115115
) => {
116116
$base.argument($reg.arg::<$t>(
117-
&$crate::to_camel_case(stringify!($name)), $info))
117+
&$crate::to_camel_case(__graphql__stringify!($name)), $info))
118118
};
119119

120120
(
@@ -125,7 +125,7 @@ macro_rules! __graphql__args {
125125
@apply_args,
126126
$reg,
127127
$base.argument($reg.arg::<$t>(
128-
&$crate::to_camel_case(stringify!($name)), $info)),
128+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)),
129129
$info,
130130
( $($rest)* ))
131131
};
@@ -139,7 +139,7 @@ macro_rules! __graphql__args {
139139
$reg,
140140
$base.argument(
141141
$reg.arg::<$t>(
142-
&$crate::to_camel_case(stringify!($name)), $info)
142+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
143143
.description($desc)),
144144
$info,
145145
( $($rest)* ))

juniper/src/macros/field.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[doc(hidden)]
2-
#[macro_export]
2+
#[macro_export(local_inner_macros)]
33
macro_rules! __graphql__build_field_matches {
44
// field deprecated <reason> <name>(...) -> <type> as <description> { ... }
55
(
@@ -81,7 +81,7 @@ macro_rules! __graphql__build_field_matches {
8181
( $( ( $name:ident; ( $($args:tt)* ); $t:ty; $body:block ) )* ),
8282
) => {
8383
$(
84-
if $fieldvar == &$crate::to_camel_case(stringify!($name)) {
84+
if $fieldvar == &$crate::to_camel_case(__graphql__stringify!($name)) {
8585
let result: $t = (||{
8686
__graphql__args!(
8787
@assign_arg_vars,
@@ -98,6 +98,6 @@ macro_rules! __graphql__build_field_matches {
9898
})
9999
}
100100
)*
101-
panic!("Field {} not found on type {}", $fieldvar, $outname);
101+
__graphql__panic!("Field {} not found on type {}", $fieldvar, $outname);
102102
};
103103
}

juniper/src/macros/interface.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
8585
[1]: macro.graphql_object!.html
8686
8787
*/
88-
#[macro_export]
88+
#[macro_export(local_inner_macros)]
8989
macro_rules! graphql_interface {
9090
( @as_item, $i:item) => { $i };
9191
( @as_expr, $e:expr) => { $e };
@@ -104,7 +104,7 @@ macro_rules! graphql_interface {
104104
@apply_args,
105105
$reg,
106106
$reg.field_convert::<$t, _, Self::Context>(
107-
&$crate::to_camel_case(stringify!($name)), $info)
107+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
108108
.description($desc)
109109
.deprecated($reason),
110110
$info,
@@ -123,7 +123,7 @@ macro_rules! graphql_interface {
123123
@apply_args,
124124
$reg,
125125
$reg.field_convert::<$t, _, Self::Context>(
126-
&$crate::to_camel_case(stringify!($name)), $info)
126+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
127127
.deprecated($reason),
128128
$info,
129129
$args));
@@ -141,7 +141,7 @@ macro_rules! graphql_interface {
141141
@apply_args,
142142
$reg,
143143
$reg.field_convert::<$t, _, Self::Context>(
144-
&$crate::to_camel_case(stringify!($name)), $info)
144+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
145145
.description($desc),
146146
$info,
147147
$args));
@@ -159,7 +159,7 @@ macro_rules! graphql_interface {
159159
@apply_args,
160160
$reg,
161161
$reg.field_convert::<$t, _, Self::Context>(
162-
&$crate::to_camel_case(stringify!($name)), $info),
162+
&$crate::to_camel_case(__graphql__stringify!($name)), $info),
163163
$info,
164164
$args));
165165

@@ -206,7 +206,7 @@ macro_rules! graphql_interface {
206206
}
207207
)*
208208

209-
panic!("Concrete type not handled by instance resolvers on {}", $outname);
209+
__graphql__panic!("Concrete type not handled by instance resolvers on {}", $outname);
210210
};
211211

212212
// instance_resolvers: | <ctxtvar> |
@@ -224,7 +224,7 @@ macro_rules! graphql_interface {
224224
}
225225
)*
226226

227-
panic!("Concrete type not handled by instance resolvers on {}", $outname);
227+
__graphql__panic!("Concrete type not handled by instance resolvers on {}", $outname);
228228
};
229229

230230
( @ $mfn:ident, $args:tt, $first:tt $($rest:tt)* ) => {
@@ -327,6 +327,6 @@ macro_rules! graphql_interface {
327327
$( $items:tt )*
328328
}
329329
) => {
330-
graphql_interface!(() $name : $ctxt as (stringify!($name)) | &$mainself | { $( $items )* });
330+
graphql_interface!(() $name : $ctxt as (__graphql__stringify!($name)) | &$mainself | { $( $items )* });
331331
};
332332
}

juniper/src/macros/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
// Wrapper macros which allows built-in macros to be recognized as "crate-local".
2+
3+
#[doc(hidden)]
4+
#[macro_export]
5+
macro_rules! __graphql__panic {
6+
($($t:tt)*) => ( panic!($($t)*) );
7+
}
8+
9+
#[doc(hidden)]
10+
#[macro_export]
11+
macro_rules! __graphql__stringify {
12+
($($t:tt)*) => ( stringify!($($t)*) );
13+
}
14+
15+
#[doc(hidden)]
16+
#[macro_export]
17+
macro_rules! __graphql__vec {
18+
($($t:tt)*) => ( vec!($($t)*) );
19+
}
20+
121
#[macro_use]
222
mod object;
323
#[macro_use]

juniper/src/macros/object.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ arg_name = ("default".to_owned()): String
237237
[1]: struct.Executor.html
238238
239239
*/
240-
#[macro_export]
240+
#[macro_export(local_inner_macros)]
241241
macro_rules! graphql_object {
242242
( @as_item, $i:item) => { $i };
243243
( @as_expr, $e:expr) => { $e };
@@ -258,7 +258,7 @@ macro_rules! graphql_object {
258258
@apply_args,
259259
$reg,
260260
$reg.field_convert::<$t, _, Self::Context>(
261-
&$crate::to_camel_case(stringify!($name)), $info)
261+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
262262
.description($desc)
263263
.deprecated($reason),
264264
$info,
@@ -277,7 +277,7 @@ macro_rules! graphql_object {
277277
@apply_args,
278278
$reg,
279279
$reg.field_convert::<$t, _, Self::Context>(
280-
&$crate::to_camel_case(stringify!($name)), $info)
280+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
281281
.deprecated($reason),
282282
$info,
283283
$args));
@@ -295,7 +295,7 @@ macro_rules! graphql_object {
295295
@apply_args,
296296
$reg,
297297
$reg.field_convert::<$t, _, Self::Context>(
298-
&$crate::to_camel_case(stringify!($name)), $info)
298+
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
299299
.description($desc),
300300
$info,
301301
$args));
@@ -313,7 +313,7 @@ macro_rules! graphql_object {
313313
@apply_args,
314314
$reg,
315315
$reg.field_convert::<$t, _, Self::Context>(
316-
&$crate::to_camel_case(stringify!($name)), $info),
316+
&$crate::to_camel_case(__graphql__stringify!($name)), $info),
317317
$info,
318318
$args));
319319

@@ -357,13 +357,13 @@ macro_rules! graphql_object {
357357
) => {};
358358

359359
( @assign_interfaces, $reg:expr, $tgt:expr, [ $($t:ty,)* ] ) => {
360-
$tgt = Some(vec![
360+
$tgt = Some(__graphql__vec![
361361
$($reg.get_type::<$t>(&())),*
362362
]);
363363
};
364364

365365
( @assign_interfaces, $reg:expr, $tgt:expr, [ $($t:ty),* ] ) => {
366-
$tgt = Some(vec![
366+
$tgt = Some(__graphql__vec![
367367
$($reg.get_type::<$t>(&())),*
368368
]);
369369
};
@@ -453,6 +453,6 @@ macro_rules! graphql_object {
453453
}
454454
) => {
455455
graphql_object!(
456-
( ); $name; $ctxt; (stringify!($name)); $mainself; $( $items )*);
456+
( ); $name; $ctxt; (__graphql__stringify!($name)); $mainself; $( $items )*);
457457
};
458458
}

juniper/src/macros/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ In addition to implementing `GraphQLType` for the type in question,
3636
usable as arguments and default values.
3737
3838
*/
39-
#[macro_export]
39+
#[macro_export(local_inner_macros)]
4040
macro_rules! graphql_scalar {
4141
( @as_expr, $e:expr) => { $e };
4242

@@ -151,6 +151,6 @@ macro_rules! graphql_scalar {
151151
// Entry point
152152
// RustName { ... }
153153
( $name:ty { $( $items:tt )* }) => {
154-
graphql_scalar!( @parse, ( $name, stringify!($name), None ), ( None, None ), $($items)* );
154+
graphql_scalar!( @parse, ( $name, __graphql__stringify!($name), None ), ( None, None ), $($items)* );
155155
};
156156
}

juniper/src/macros/union.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ resolvers.
1717
[1]: macro.graphql_object!.html
1818
[2]: macro.graphql_interface!.html
1919
*/
20-
#[macro_export]
20+
#[macro_export(local_inner_macros)]
2121
macro_rules! graphql_union {
2222
( @as_item, $i:item) => { $i };
2323
( @as_expr, $e:expr) => { $e };
@@ -43,7 +43,7 @@ macro_rules! graphql_union {
4343
instance_resolvers: | $ctxtvar:pat
4444
| { $( $srctype:ty => $resolver:expr ),* $(,)* } $( $rest:tt )*
4545
) => {
46-
$acc = vec![
46+
$acc = __graphql__vec![
4747
$(
4848
$reg.get_type::<$srctype>(&())
4949
),*
@@ -68,7 +68,7 @@ macro_rules! graphql_union {
6868
}
6969
)*
7070

71-
panic!("Concrete type not handled by instance resolvers on {}", $outname);
71+
__graphql__panic!("Concrete type not handled by instance resolvers on {}", $outname);
7272
};
7373

7474
// To generate the "resolve into type" resolver, syntax case:
@@ -87,7 +87,7 @@ macro_rules! graphql_union {
8787
}
8888
)*
8989

90-
panic!("Concrete type not handled by instance resolvers on {}", $outname);
90+
__graphql__panic!("Concrete type not handled by instance resolvers on {}", $outname);
9191
};
9292

9393
// eat commas
@@ -177,6 +177,6 @@ macro_rules! graphql_union {
177177
$( $items:tt )*
178178
}
179179
) => {
180-
graphql_union!(() $name : $ctxt as (stringify!($name)) | &$mainself | { $( $items )* });
180+
graphql_union!(() $name : $ctxt as (__graphql__stringify!($name)) | &$mainself | { $( $items )* });
181181
};
182182
}

0 commit comments

Comments
 (0)