From c9e1ae137373ba5df06b8c3312a56499d65c5b13 Mon Sep 17 00:00:00 2001 From: zachs18 <8355914+zachs18@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:40:20 -0600 Subject: [PATCH] Remove where clause from derived TransparentWrapper impls. (#146) * Remove where clause from derived TransparentWrapper impls. * Add test for TransparentWrapper trait bound regression. --- derive/src/lib.rs | 6 ++++++ derive/src/traits.rs | 7 +++++++ tests/derive.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 6a40e8c..e16d43e 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -339,6 +339,12 @@ fn derive_marker_trait_inner( quote!() }; + let where_clause = if Trait::requires_where_clause() { + where_clause + } else { + None + }; + Ok(quote! { #asserts diff --git a/derive/src/traits.rs b/derive/src/traits.rs index 2e3fec9..2590267 100644 --- a/derive/src/traits.rs +++ b/derive/src/traits.rs @@ -32,6 +32,9 @@ pub trait Derivable { fn trait_impl(_input: &DeriveInput) -> Result<(TokenStream, TokenStream)> { Ok((quote!(), quote!())) } + fn requires_where_clause() -> bool { + true + } } pub struct Pod; @@ -300,6 +303,10 @@ impl Derivable for TransparentWrapper { } } } + + fn requires_where_clause() -> bool { + false + } } pub struct Contiguous; diff --git a/tests/derive.rs b/tests/derive.rs index 7859865..f06ff77 100644 --- a/tests/derive.rs +++ b/tests/derive.rs @@ -23,3 +23,29 @@ struct TransparentWithZeroSized { a: u16, b: (), } + +#[derive(TransparentWrapper)] +#[repr(transparent)] +struct TransparentWithGeneric { + a: T, +} + +/// Ensuring that no additional bounds are emitted. +/// See https://github.com/Lokathor/bytemuck/issues/145 +fn test_generic(x: T) -> TransparentWithGeneric { + TransparentWithGeneric::wrap(x) +} + +#[derive(TransparentWrapper)] +#[repr(transparent)] +#[transparent(T)] +struct TransparentWithGenericAndZeroSized { + a: T, + b: () +} + +/// Ensuring that no additional bounds are emitted. +/// See https://github.com/Lokathor/bytemuck/issues/145 +fn test_generic_with_zst(x: T) -> TransparentWithGenericAndZeroSized { + TransparentWithGenericAndZeroSized::wrap(x) +}