Skip to content

Commit

Permalink
Use local_inner_macros to all helper macros (#233)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ubnt-intrepid authored and LegNeato committed Aug 26, 2018
1 parent 273edc1 commit d496220
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 35 deletions.
18 changes: 9 additions & 9 deletions juniper/src/macros/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! __graphql__args {
// Internal type conversion
( @as_expr, $e:expr) => { $e };
Expand Down Expand Up @@ -27,7 +27,7 @@ macro_rules! __graphql__args {
$name:ident $(= $default:tt)* : $ty:ty $(as $desc:tt)*, $($rest:tt)*
) => {
let $name: $ty = $args
.get(&$crate::to_camel_case(stringify!($name)))
.get(&$crate::to_camel_case(__graphql__stringify!($name)))
.expect("Argument missing - validation must have failed");
__graphql__args!(@assign_arg_vars, $args, $executorvar, $($rest)*);
};
Expand All @@ -38,7 +38,7 @@ macro_rules! __graphql__args {
$name:ident $(= $default:tt)* : $ty:ty $(as $desc:expr)*
) => {
let $name: $ty = $args
.get(&$crate::to_camel_case(stringify!($name)))
.get(&$crate::to_camel_case(__graphql__stringify!($name)))
.expect("Argument missing - validation must have failed");
};

Expand Down Expand Up @@ -75,7 +75,7 @@ macro_rules! __graphql__args {
$reg:expr, $base:expr, $info:expr, ( $name:ident = $default:tt : $t:ty )
) => {
$base.argument($reg.arg_with_default::<$t>(
&$crate::to_camel_case(stringify!($name)),
&$crate::to_camel_case(__graphql__stringify!($name)),
&__graphql__args!(@as_expr, $default), $info))
};

Expand All @@ -87,7 +87,7 @@ macro_rules! __graphql__args {
@apply_args,
$reg,
$base.argument($reg.arg_with_default::<$t>(
&$crate::to_camel_case(stringify!($name)),
&$crate::to_camel_case(__graphql__stringify!($name)),
&__graphql__args!(@as_expr, $default), $info)),
$info,
( $($rest)* ))
Expand All @@ -102,7 +102,7 @@ macro_rules! __graphql__args {
@apply_args,
$reg,
$base.argument($reg.arg_with_default::<$t>(
&$crate::to_camel_case(stringify!($name)),
&$crate::to_camel_case(__graphql__stringify!($name)),
&__graphql__args!(@as_expr, $default), $info)
.description($desc)),
$info,
Expand All @@ -114,7 +114,7 @@ macro_rules! __graphql__args {
$reg:expr, $base:expr, $info:expr, ( $name:ident : $t:ty )
) => {
$base.argument($reg.arg::<$t>(
&$crate::to_camel_case(stringify!($name)), $info))
&$crate::to_camel_case(__graphql__stringify!($name)), $info))
};

(
Expand All @@ -125,7 +125,7 @@ macro_rules! __graphql__args {
@apply_args,
$reg,
$base.argument($reg.arg::<$t>(
&$crate::to_camel_case(stringify!($name)), $info)),
&$crate::to_camel_case(__graphql__stringify!($name)), $info)),
$info,
( $($rest)* ))
};
Expand All @@ -139,7 +139,7 @@ macro_rules! __graphql__args {
$reg,
$base.argument(
$reg.arg::<$t>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.description($desc)),
$info,
( $($rest)* ))
Expand Down
6 changes: 3 additions & 3 deletions juniper/src/macros/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[doc(hidden)]
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! __graphql__build_field_matches {
// field deprecated <reason> <name>(...) -> <type> as <description> { ... }
(
Expand Down Expand Up @@ -81,7 +81,7 @@ macro_rules! __graphql__build_field_matches {
( $( ( $name:ident; ( $($args:tt)* ); $t:ty; $body:block ) )* ),
) => {
$(
if $fieldvar == &$crate::to_camel_case(stringify!($name)) {
if $fieldvar == &$crate::to_camel_case(__graphql__stringify!($name)) {
let result: $t = (||{
__graphql__args!(
@assign_arg_vars,
Expand All @@ -98,6 +98,6 @@ macro_rules! __graphql__build_field_matches {
})
}
)*
panic!("Field {} not found on type {}", $fieldvar, $outname);
__graphql__panic!("Field {} not found on type {}", $fieldvar, $outname);
};
}
16 changes: 8 additions & 8 deletions juniper/src/macros/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
[1]: macro.graphql_object!.html
*/
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! graphql_interface {
( @as_item, $i:item) => { $i };
( @as_expr, $e:expr) => { $e };
Expand All @@ -104,7 +104,7 @@ macro_rules! graphql_interface {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.description($desc)
.deprecated($reason),
$info,
Expand All @@ -123,7 +123,7 @@ macro_rules! graphql_interface {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.deprecated($reason),
$info,
$args));
Expand All @@ -141,7 +141,7 @@ macro_rules! graphql_interface {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.description($desc),
$info,
$args));
Expand All @@ -159,7 +159,7 @@ macro_rules! graphql_interface {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info),
&$crate::to_camel_case(__graphql__stringify!($name)), $info),
$info,
$args));

Expand Down Expand Up @@ -206,7 +206,7 @@ macro_rules! graphql_interface {
}
)*

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

// instance_resolvers: | <ctxtvar> |
Expand All @@ -224,7 +224,7 @@ macro_rules! graphql_interface {
}
)*

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

( @ $mfn:ident, $args:tt, $first:tt $($rest:tt)* ) => {
Expand Down Expand Up @@ -327,6 +327,6 @@ macro_rules! graphql_interface {
$( $items:tt )*
}
) => {
graphql_interface!(() $name : $ctxt as (stringify!($name)) | &$mainself | { $( $items )* });
graphql_interface!(() $name : $ctxt as (__graphql__stringify!($name)) | &$mainself | { $( $items )* });
};
}
20 changes: 20 additions & 0 deletions juniper/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Wrapper macros which allows built-in macros to be recognized as "crate-local".

#[doc(hidden)]
#[macro_export]
macro_rules! __graphql__panic {
($($t:tt)*) => ( panic!($($t)*) );
}

#[doc(hidden)]
#[macro_export]
macro_rules! __graphql__stringify {
($($t:tt)*) => ( stringify!($($t)*) );
}

#[doc(hidden)]
#[macro_export]
macro_rules! __graphql__vec {
($($t:tt)*) => ( vec!($($t)*) );
}

#[macro_use]
mod object;
#[macro_use]
Expand Down
16 changes: 8 additions & 8 deletions juniper/src/macros/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ arg_name = ("default".to_owned()): String
[1]: struct.Executor.html
*/
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! graphql_object {
( @as_item, $i:item) => { $i };
( @as_expr, $e:expr) => { $e };
Expand All @@ -258,7 +258,7 @@ macro_rules! graphql_object {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.description($desc)
.deprecated($reason),
$info,
Expand All @@ -277,7 +277,7 @@ macro_rules! graphql_object {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.deprecated($reason),
$info,
$args));
Expand All @@ -295,7 +295,7 @@ macro_rules! graphql_object {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info)
&$crate::to_camel_case(__graphql__stringify!($name)), $info)
.description($desc),
$info,
$args));
Expand All @@ -313,7 +313,7 @@ macro_rules! graphql_object {
@apply_args,
$reg,
$reg.field_convert::<$t, _, Self::Context>(
&$crate::to_camel_case(stringify!($name)), $info),
&$crate::to_camel_case(__graphql__stringify!($name)), $info),
$info,
$args));

Expand Down Expand Up @@ -357,13 +357,13 @@ macro_rules! graphql_object {
) => {};

( @assign_interfaces, $reg:expr, $tgt:expr, [ $($t:ty,)* ] ) => {
$tgt = Some(vec![
$tgt = Some(__graphql__vec![
$($reg.get_type::<$t>(&())),*
]);
};

( @assign_interfaces, $reg:expr, $tgt:expr, [ $($t:ty),* ] ) => {
$tgt = Some(vec![
$tgt = Some(__graphql__vec![
$($reg.get_type::<$t>(&())),*
]);
};
Expand Down Expand Up @@ -453,6 +453,6 @@ macro_rules! graphql_object {
}
) => {
graphql_object!(
( ); $name; $ctxt; (stringify!($name)); $mainself; $( $items )*);
( ); $name; $ctxt; (__graphql__stringify!($name)); $mainself; $( $items )*);
};
}
4 changes: 2 additions & 2 deletions juniper/src/macros/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In addition to implementing `GraphQLType` for the type in question,
usable as arguments and default values.
*/
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! graphql_scalar {
( @as_expr, $e:expr) => { $e };

Expand Down Expand Up @@ -151,6 +151,6 @@ macro_rules! graphql_scalar {
// Entry point
// RustName { ... }
( $name:ty { $( $items:tt )* }) => {
graphql_scalar!( @parse, ( $name, stringify!($name), None ), ( None, None ), $($items)* );
graphql_scalar!( @parse, ( $name, __graphql__stringify!($name), None ), ( None, None ), $($items)* );
};
}
10 changes: 5 additions & 5 deletions juniper/src/macros/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resolvers.
[1]: macro.graphql_object!.html
[2]: macro.graphql_interface!.html
*/
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! graphql_union {
( @as_item, $i:item) => { $i };
( @as_expr, $e:expr) => { $e };
Expand All @@ -43,7 +43,7 @@ macro_rules! graphql_union {
instance_resolvers: | $ctxtvar:pat
| { $( $srctype:ty => $resolver:expr ),* $(,)* } $( $rest:tt )*
) => {
$acc = vec![
$acc = __graphql__vec![
$(
$reg.get_type::<$srctype>(&())
),*
Expand All @@ -68,7 +68,7 @@ macro_rules! graphql_union {
}
)*

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

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

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

// eat commas
Expand Down Expand Up @@ -177,6 +177,6 @@ macro_rules! graphql_union {
$( $items:tt )*
}
) => {
graphql_union!(() $name : $ctxt as (stringify!($name)) | &$mainself | { $( $items )* });
graphql_union!(() $name : $ctxt as (__graphql__stringify!($name)) | &$mainself | { $( $items )* });
};
}

0 comments on commit d496220

Please sign in to comment.