Skip to content

Commit

Permalink
Check std::prelude hygiene in codegen tests (#1195, #1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Oct 13, 2023
1 parent a433a27 commit 57628de
Show file tree
Hide file tree
Showing 19 changed files with 808 additions and 711 deletions.
61 changes: 32 additions & 29 deletions juniper/src/macros/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,17 +693,19 @@ macro_rules! assert_field_args {
$field_name: expr $(,)?
) => {
const _: () = {
const BASE_NAME: &str = <$base_ty as $crate::macros::reflect::BaseType<$scalar>>::NAME;
const IMPL_NAME: &str = <$impl_ty as $crate::macros::reflect::BaseType<$scalar>>::NAME;
const ERR_PREFIX: &str = $crate::const_concat!(
const BASE_NAME: &::core::primitive::str =
<$base_ty as $crate::macros::reflect::BaseType<$scalar>>::NAME;
const IMPL_NAME: &::core::primitive::str =
<$impl_ty as $crate::macros::reflect::BaseType<$scalar>>::NAME;
const ERR_PREFIX: &::core::primitive::str = $crate::const_concat!(
"Failed to implement interface `",
BASE_NAME,
"` on `",
IMPL_NAME,
"`: ",
);

const FIELD_NAME: &str = $field_name;
const FIELD_NAME: &::core::primitive::str = $field_name;

const BASE_ARGS: ::juniper::macros::reflect::Arguments =
<$base_ty as $crate::macros::reflect::FieldMeta<
Expand All @@ -728,20 +730,20 @@ macro_rules! assert_field_args {
TypeMismatch,
}

const fn unwrap_error(v: ::std::result::Result<(), Error>) -> Error {
const fn unwrap_error(v: ::core::result::Result<(), Error>) -> Error {
match v {
// Unfortunately we can't use `unreachable!()` here, as this
// branch will be executed either way.
Ok(()) => Error {
::core::result::Result::Ok(()) => Error {
cause: Cause::RequiredField,
base: ("unreachable", "unreachable", 1),
implementation: ("unreachable", "unreachable", 1),
},
Err(err) => err,
::core::result::Result::Err(err) => err,
}
}

const fn check() -> Result<(), Error> {
const fn check() -> ::core::result::Result<(), Error> {
let mut base_i = 0;
while base_i < BASE_ARGS.len() {
let (base_name, base_type, base_wrap_val) = BASE_ARGS[base_i];
Expand Down Expand Up @@ -800,18 +802,18 @@ macro_rules! assert_field_args {
base_i += 1;
}
if !was_found {
return Err(Error {
return ::core::result::Result::Err(Error {
cause: Cause::AdditionalNonNullableField,
base: (impl_name, impl_type, impl_wrapped_val),
implementation: (impl_name, impl_type, impl_wrapped_val),
});
}
}

Ok(())
::core::result::Result::Ok(())
}

const RES: ::std::result::Result<(), Error> = check();
const RES: ::core::result::Result<(), Error> = check();
if RES.is_err() {
const ERROR: Error = unwrap_error(RES);

Expand All @@ -822,7 +824,7 @@ macro_rules! assert_field_args {
const IMPL_TYPE_FORMATTED: &str =
$crate::format_type!(ERROR.implementation.1, ERROR.implementation.2);

const MSG: &str = match ERROR.cause {
const MSG: &::core::primitive::str = match ERROR.cause {
Cause::TypeMismatch => {
$crate::const_concat!(
"Argument `",
Expand Down Expand Up @@ -865,9 +867,9 @@ macro_rules! assert_field_args {
#[macro_export]
macro_rules! const_concat {
($($s:expr),* $(,)?) => {{
const LEN: usize = 0 $(+ $s.as_bytes().len())*;
const CNT: usize = [$($s),*].len();
const fn concat(input: [&str; CNT]) -> [u8; LEN] {
const LEN: ::core::primitive::usize = 0 $(+ $s.as_bytes().len())*;
const CNT: ::core::primitive::usize = [$($s),*].len();
const fn concat(input: [&::core::primitive::str; CNT]) -> [::core::primitive::u8; LEN] {
let mut bytes = [0; LEN];
let (mut i, mut byte) = (0, 0);
while i < CNT {
Expand All @@ -881,12 +883,12 @@ macro_rules! const_concat {
}
bytes
}
const CON: [u8; LEN] = concat([$($s),*]);
const CON: [::core::primitive::u8; LEN] = concat([$($s),*]);

// TODO: Use `.unwrap()` once it becomes `const`.
match ::std::str::from_utf8(&CON) {
::std::result::Result::Ok(s) => s,
_ => unreachable!(),
match ::core::str::from_utf8(&CON) {
::core::result::Result::Ok(s) => s,
_ => ::core::unreachable!(),
}
}};
}
Expand Down Expand Up @@ -936,13 +938,13 @@ macro_rules! format_type {
) = ($ty, $wrapped_value);
const RES_LEN: usize = $crate::macros::reflect::type_len_with_wrapped_val(TYPE.0, TYPE.1);

const OPENING_BRACKET: &str = "[";
const CLOSING_BRACKET: &str = "]";
const BANG: &str = "!";
const OPENING_BRACKET: &::core::primitive::str = "[";
const CLOSING_BRACKET: &::core::primitive::str = "]";
const BANG: &::core::primitive::str = "!";

const fn format_type_arr() -> [u8; RES_LEN] {
const fn format_type_arr() -> [::core::primitive::u8; RES_LEN] {
let (ty, wrap_val) = TYPE;
let mut type_arr: [u8; RES_LEN] = [0; RES_LEN];
let mut type_arr: [::core::primitive::u8; RES_LEN] = [0; RES_LEN];

let mut current_start = 0;
let mut current_end = RES_LEN - 1;
Expand Down Expand Up @@ -1003,13 +1005,14 @@ macro_rules! format_type {
type_arr
}

const TYPE_ARR: [u8; RES_LEN] = format_type_arr();
const TYPE_ARR: [::core::primitive::u8; RES_LEN] = format_type_arr();

// TODO: Use `.unwrap()` once it becomes `const`.
const TYPE_FORMATTED: &str = match ::std::str::from_utf8(TYPE_ARR.as_slice()) {
::std::result::Result::Ok(s) => s,
_ => unreachable!(),
};
const TYPE_FORMATTED: &::core::primitive::str =
match ::core::str::from_utf8(TYPE_ARR.as_slice()) {
::core::result::Result::Ok(s) => s,
_ => unreachable!(),
};

TYPE_FORMATTED
}};
Expand Down
12 changes: 0 additions & 12 deletions tests/codegen/pass/local_results.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/codegen/pass/local_send.rs

This file was deleted.

7 changes: 0 additions & 7 deletions tests/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,3 @@ fn test_failing_compilation() {
let t = trybuild::TestCases::new();
t.compile_fail("fail/**/*.rs");
}

#[rustversion::nightly]
#[test]
fn test_passing_compilation() {
let t = trybuild::TestCases::new();
t.pass("pass/**/*.rs");
}
8 changes: 6 additions & 2 deletions tests/integration/tests/codegen_enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use juniper::{

use self::common::util::{schema, schema_with_scalar};

// Override `std::prelude` items to check whether macros expand hygienically.
#[allow(unused_imports)]
use self::common::hygiene::*;

mod trivial {
use super::*;

Expand Down Expand Up @@ -863,7 +867,7 @@ mod bounded_generic_scalar {
use super::*;

#[derive(GraphQLEnum)]
#[graphql(scalar = S: ScalarValue + Clone)]
#[graphql(scalar = S: ScalarValue + prelude::Clone)]
enum Character {
Human,
Droid,
Expand Down Expand Up @@ -896,7 +900,7 @@ mod bounded_generic_scalar {
mod explicit_custom_context {
use super::*;

struct CustomContext(String);
struct CustomContext(prelude::String);

impl juniper::Context for CustomContext {}

Expand Down
10 changes: 7 additions & 3 deletions tests/integration/tests/codegen_input_object_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use juniper::{

use self::common::util::schema;

// Override `std::prelude` items to check whether macros expand hygienically.
#[allow(unused_imports)]
use self::common::hygiene::*;

mod trivial {
use super::*;

Expand Down Expand Up @@ -267,16 +271,16 @@ mod default_nullable_value {
#[derive(GraphQLInputObject)]
struct Point2D {
#[graphql(default = 10.0)]
x: Option<f64>,
x: prelude::Option<f64>,
#[graphql(default = 10.0)]
y: Option<f64>,
y: prelude::Option<f64>,
}

struct QueryRoot;

#[graphql_object]
impl QueryRoot {
fn x(point: Point2D) -> Option<f64> {
fn x(point: Point2D) -> prelude::Option<f64> {
point.x
}
}
Expand Down
Loading

0 comments on commit 57628de

Please sign in to comment.