From d92998faec3bab141cbe3f4284119fa1097e29ce Mon Sep 17 00:00:00 2001 From: David Koloski Date: Thu, 3 Nov 2022 01:32:18 -0400 Subject: [PATCH] [derive] Move field type bounds into where clauses (#119) --- zerocopy-derive/src/lib.rs | 21 +++-------- .../tests/ui-msrv/late_compile_pass.stderr | 30 +++------------- .../tests/ui-stable/late_compile_pass.stderr | 30 +++------------- .../tests/ui/late_compile_pass.stderr | 35 ++++++------------- 4 files changed, 25 insertions(+), 91 deletions(-) diff --git a/zerocopy-derive/src/lib.rs b/zerocopy-derive/src/lib.rs index 122f07d252..590008861c 100644 --- a/zerocopy-derive/src/lib.rs +++ b/zerocopy-derive/src/lib.rs @@ -536,21 +536,11 @@ fn impl_block( 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(::core::marker::PhantomData); - // 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) => (), @@ -608,7 +598,6 @@ fn impl_block( 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 diff --git a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr index 93a385aab6..29f18ae05b 100644 --- a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr @@ -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 @@ -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 @@ -33,11 +25,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied = help: the following implementations were found: -note: required by a bound in `::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 `::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 @@ -49,11 +37,7 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied = help: the following implementations were found: -note: required by a bound in `::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 `::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 @@ -65,9 +49,5 @@ error[E0277]: the trait bound `u16: Unaligned` is not satisfied = help: the following implementations were found: -note: required by a bound in `::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 `::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) diff --git a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr index 11794fadf0..76d752e3e6 100644 --- a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr @@ -14,11 +14,7 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied I32 I64 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 @@ -37,11 +33,7 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied I32 I64 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 @@ -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 `::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 `::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 @@ -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 `::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 `::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 @@ -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 `::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 `::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) diff --git a/zerocopy-derive/tests/ui/late_compile_pass.stderr b/zerocopy-derive/tests/ui/late_compile_pass.stderr index 4bcec1dae9..0425f3e90a 100644 --- a/zerocopy-derive/tests/ui/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui/late_compile_pass.stderr @@ -14,11 +14,8 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied I32 I64 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 @@ -37,11 +34,8 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied I32 I64 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 @@ -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 `::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 `::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 @@ -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 `::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 `::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 @@ -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 `::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 `::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)