Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions zerocopy-derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream

quote! {
#repr
#[allow(dead_code, non_camel_case_types)]
#[allow(dead_code)]
enum ___ZerocopyTag {
#(#variants,)*
}
Expand Down Expand Up @@ -90,7 +90,6 @@ fn generate_tag_consts(data: &DataEnum) -> TokenStream {
//
// Casting between two integers of the same size (e.g. i32 -> u32)
// is a no-op.
#[allow(non_upper_case_globals)]
const #tag_ident: ___ZerocopyTagPrimitive =
___ZerocopyTag::#variant_ident as ___ZerocopyTagPrimitive;
}
Expand Down Expand Up @@ -143,7 +142,6 @@ fn generate_variant_structs(

let variant_struct = parse_quote! {
#[repr(C)]
#[allow(non_snake_case)]
struct #variant_struct_ident #impl_generics (
core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
#(#field_types,)*
Expand Down Expand Up @@ -200,7 +198,6 @@ fn generate_variants_union(

let variants_union = parse_quote! {
#[repr(C)]
#[allow(non_snake_case)]
union ___ZerocopyVariants #generics {
#(#fields)*
// Enums can have variants with no fields, but unions must
Expand Down Expand Up @@ -446,7 +443,6 @@ pub(crate) fn derive_is_bit_valid(

let variants = raw_enum.project::<_, { #zerocopy_crate::ident_id!(variants) }>();

#[allow(non_upper_case_globals)]
match tag {
#(#match_arms,)*
_ => false,
Expand Down
89 changes: 35 additions & 54 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ macro_rules! derive {
Ok(zerocopy_crate) => zerocopy_crate,
Err(e) => return e.into_compile_error().into(),
};
$inner(&ast, Trait::$trait, &zerocopy_crate).into_ts().into()
let ts = $inner(&ast, Trait::$trait, &zerocopy_crate).into_ts();
// We wrap in `const_block` as a backstop in case any derive fails
// to wrap its output in `const_block` (and thus fails to annotate)
// with the full set of `#[allow(...)]` attributes).
const_block([Some(ts)]).into()
}
};
}
Expand Down Expand Up @@ -337,7 +341,6 @@ fn derive_known_layout_inner(
// Define the collection of type-level field handles.
let field_defs = field_indices.iter().zip(&fields).map(|(idx, (vis, _, _))| {
quote! {
#[allow(non_camel_case_types)]
#vis struct #idx;
}
});
Expand All @@ -352,7 +355,6 @@ fn derive_known_layout_inner(
// would not respect `Self` hygiene, as `Self` would refer to
// the helper struct we are generating, not the derive target
// type.
#[allow(deprecated)]
unsafe impl #impl_generics #zerocopy_crate::util::macro_util::Field<#idx> for #ident #ty_generics
where
#predicates
Expand Down Expand Up @@ -393,11 +395,6 @@ fn derive_known_layout_inner(
// `#ty`, not `__ZerocopyKnownLayoutMaybeUninit` (see #2116).
#repr
#[doc(hidden)]
// Required on some rustc versions due to a lint that is only
// triggered when `derive(KnownLayout)` is applied to `repr(C)`
// structs that are generated by macros. See #2177 for details.
#[allow(private_bounds)]
#[allow(deprecated)]
#vis struct __ZerocopyKnownLayoutMaybeUninit<#params> (
#(#zerocopy_crate::util::macro_util::core_reexport::mem::MaybeUninit<
<#ident #ty_generics as
Expand All @@ -424,13 +421,11 @@ fn derive_known_layout_inner(
// sound, since `__ZerocopyKnownLayoutMaybeUninit` is guaranteed
// to have the same layout as the derive target type, except
// that `__ZerocopyKnownLayoutMaybeUninit` admits uninit bytes.
#[allow(deprecated)]
unsafe impl #impl_generics #zerocopy_crate::KnownLayout for __ZerocopyKnownLayoutMaybeUninit #ty_generics
where
#trailing_field_ty: #zerocopy_crate::KnownLayout,
#predicates
{
#[allow(clippy::missing_inline_in_public_items)]
fn only_derive_is_allowed_to_implement_this_trait() {}

type PointerMetadata = <#ident #ty_generics as #zerocopy_crate::KnownLayout>::PointerMetadata;
Expand Down Expand Up @@ -631,10 +626,6 @@ fn derive_hash_inner(
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let where_predicates = where_clause.map(|clause| &clause.predicates);
Ok(quote! {
#[allow(deprecated)]
// While there are not currently any warnings that this suppresses (that
// we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::hash::Hash for #type_ident #ty_generics
where
Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
Expand Down Expand Up @@ -677,12 +668,6 @@ fn derive_eq_inner(
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let where_predicates = where_clause.map(|clause| &clause.predicates);
Ok(quote! {
// FIXME(#553): Add a test that generates a warning when
// `#[allow(deprecated)]` isn't present.
#[allow(deprecated)]
// While there are not currently any warnings that this suppresses (that
// we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::cmp::PartialEq for #type_ident #ty_generics
where
Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
Expand All @@ -696,12 +681,6 @@ fn derive_eq_inner(
}
}

// FIXME(#553): Add a test that generates a warning when
// `#[allow(deprecated)]` isn't present.
#[allow(deprecated)]
// While there are not currently any warnings that this suppresses (that
// we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
impl #impl_generics #zerocopy_crate::util::macro_util::core_reexport::cmp::Eq for #type_ident #ty_generics
where
Self: #zerocopy_crate::IntoBytes + #zerocopy_crate::Immutable,
Expand Down Expand Up @@ -867,13 +846,7 @@ fn derive_has_field_struct_union(
.build()
});

quote! {
#[allow(non_camel_case_types)]
const _: () = {
#(#field_tokens)*
#(#has_fields)*
};
}
const_block(field_tokens.into_iter().chain(has_fields).map(Some))
}

/// A struct is `TryFromBytes` if:
Expand Down Expand Up @@ -1945,10 +1918,6 @@ impl<'a> ImplBlockBuilder<'a> {

let inner_extras = self.inner_extras;
let impl_tokens = quote! {
#[allow(deprecated, non_local_definitions)]
// While there are not currently any warnings that this suppresses
// (that we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
unsafe impl < #(#params),* > #trait_path for #type_ident < #(#param_idents),* >
where
#(#bounds,)*
Expand All @@ -1959,23 +1928,8 @@ impl<'a> ImplBlockBuilder<'a> {
}
};

if let Some(outer_extras) = self.outer_extras.filter(|e| !e.is_empty()) {
// So that any items defined in `#outer_extras` don't conflict with
// existing names defined in this scope.
quote! {
#[allow(deprecated, non_local_definitions)]
// While there are not currently any warnings that this suppresses
// (that we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
const _: () = {
#impl_tokens

#outer_extras
};
}
} else {
impl_tokens
}
let outer_extras = self.outer_extras.filter(|e| !e.is_empty());
const_block([Some(impl_tokens), outer_extras])
}
}

Expand All @@ -2000,3 +1954,30 @@ impl BoolExt for bool {
}
}
}

fn const_block(items: impl IntoIterator<Item = Option<TokenStream>>) -> TokenStream {
let items = items.into_iter().flatten();
quote! {
#[allow(
// FIXME(#553): Add a test that generates a warning when
// `#[allow(deprecated)]` isn't present.
deprecated,
// Required on some rustc versions due to a lint that is only
// triggered when `derive(KnownLayout)` is applied to `repr(C)`
// structs that are generated by macros. See #2177 for details.
private_bounds,
non_local_definitions,
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
non_ascii_idents,
clippy::missing_inline_in_public_items,
)]
// While there are not currently any warnings that this suppresses
// (that we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
const _: () = {
#(#items)*
};
}
}
Loading
Loading