Skip to content

Commit

Permalink
[derive] Move field type bounds into where clauses (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski authored and joshlf committed Aug 3, 2023
1 parent 73f2057 commit d92998f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 91 deletions.
21 changes: 5 additions & 16 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,21 +536,11 @@ fn impl_block<D: DataExt>(
GenericParam::Const(cnst) => quote!(#cnst),
});

let trait_bound_body = if require_trait_bound {
let implements_type_ident =
Ident::new(format!("Implements{}", trait_ident).as_str(), Span::call_site());
let implements_type_tokens = quote!(#implements_type_ident);
let types = non_type_param_field_types.map(|ty| quote!(#implements_type_tokens<#ty>));
quote!(
// A type with a type parameter that must implement `#trait_ident`.
struct #implements_type_ident<F: ?Sized + zerocopy::#trait_ident>(::core::marker::PhantomData<F>);
// For each field type, an instantiation that won't type check if
// that type doesn't implement `#trait_ident`.
#(let _: #types;)*
)
} else {
quote!()
};
if require_trait_bound {
for ty in non_type_param_field_types {
where_clause.predicates.push(parse_quote!(#ty: zerocopy::#trait_ident));
}
}

match (field_types.is_empty(), padding_check) {
(true, _) | (false, PaddingCheck::None) => (),
Expand Down Expand Up @@ -608,7 +598,6 @@ fn impl_block<D: DataExt>(
quote! {
unsafe impl < #(#params),* > zerocopy::#trait_ident for #type_ident < #(#param_idents),* > #where_clause {
fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized {
#trait_bound_body
}
}
#use_concrete
Expand Down
30 changes: 5 additions & 25 deletions zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
18 | #[derive(FromBytes)]
| ^^^^^^^^^ the trait `FromBytes` is not implemented for `&'static str`
|
note: required by a bound in `ImplementsFromBytes`
--> tests/ui-msrv/late_compile_pass.rs:18:10
|
18 | #[derive(FromBytes)]
| ^^^^^^^^^ required by this bound in `ImplementsFromBytes`
= help: see issue #48214
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
Expand All @@ -17,11 +13,7 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
30 | #[derive(AsBytes)]
| ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes`
|
note: required by a bound in `ImplementsAsBytes`
--> tests/ui-msrv/late_compile_pass.rs:30:10
|
30 | #[derive(AsBytes)]
| ^^^^^^^ required by this bound in `ImplementsAsBytes`
= help: see issue #48214
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -33,11 +25,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
note: required by a bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-msrv/late_compile_pass.rs:40:10
|
40 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -49,11 +37,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
note: required by a bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-msrv/late_compile_pass.rs:48:10
|
48 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -65,9 +49,5 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
note: required by a bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-msrv/late_compile_pass.rs:55:10
|
55 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)
30 changes: 5 additions & 25 deletions zerocopy-derive/tests/ui-stable/late_compile_pass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
I32<O>
I64<O>
and $N others
note: required by a bound in `ImplementsFromBytes`
--> tests/ui-stable/late_compile_pass.rs:18:10
|
18 | #[derive(FromBytes)]
| ^^^^^^^^^ required by this bound in `ImplementsFromBytes`
= help: see issue #48214
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
Expand All @@ -37,11 +33,7 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
I32<O>
I64<O>
and $N others
note: required by a bound in `ImplementsAsBytes`
--> tests/ui-stable/late_compile_pass.rs:30:10
|
30 | #[derive(AsBytes)]
| ^^^^^^^ required by this bound in `ImplementsAsBytes`
= help: see issue #48214
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -53,11 +45,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-stable/late_compile_pass.rs:40:10
|
40 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -69,11 +57,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-stable/late_compile_pass.rs:48:10
|
48 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -85,9 +69,5 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui-stable/late_compile_pass.rs:55:10
|
55 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)
35 changes: 10 additions & 25 deletions zerocopy-derive/tests/ui/late_compile_pass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
I32<O>
I64<O>
and $N others
note: required by a bound in `ImplementsFromBytes`
--> tests/ui/late_compile_pass.rs:18:10
|
18 | #[derive(FromBytes)]
| ^^^^^^^^^ required by this bound in `ImplementsFromBytes`
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
Expand All @@ -37,11 +34,8 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
I32<O>
I64<O>
and $N others
note: required by a bound in `ImplementsAsBytes`
--> tests/ui/late_compile_pass.rs:30:10
|
30 | #[derive(AsBytes)]
| ^^^^^^^ required by this bound in `ImplementsAsBytes`
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -53,11 +47,8 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:40:10
|
40 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned1 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -69,11 +60,8 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:48:10
|
48 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned2 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
Expand All @@ -85,9 +73,6 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied
= help: the following other types implement trait `Unaligned`:
i8
u8
note: required by a bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
--> tests/ui/late_compile_pass.rs:55:10
|
55 | #[derive(Unaligned)]
| ^^^^^^^^^ required by this bound in `<Unaligned3 as Unaligned>::only_derive_is_allowed_to_implement_this_trait::ImplementsUnaligned`
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit d92998f

Please sign in to comment.