Skip to content

Commit

Permalink
Merge pull request #1412 from dtolnay/automaticallyderived
Browse files Browse the repository at this point in the history
Mark all generated impl blocks with #[automatically_derived]
  • Loading branch information
dtolnay authored Dec 11, 2024
2 parents 25f8c84 + 1d4fe98 commit 6f538a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions macro/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn struct_copy(strct: &Struct, span: Span) -> TokenStream {
let generics = &strct.generics;

quote_spanned! {span=>
#[automatically_derived]
impl #generics ::cxx::core::marker::Copy for #ident #generics {}
}
}
Expand All @@ -126,6 +127,7 @@ fn struct_clone(strct: &Struct, span: Span) -> TokenStream {
};

quote_spanned! {span=>
#[automatically_derived]
#[allow(clippy::expl_impl_clone_on_copy)]
impl #generics ::cxx::core::clone::Clone for #ident #generics {
fn clone(&self) -> Self {
Expand All @@ -143,6 +145,7 @@ fn struct_debug(strct: &Struct, span: Span) -> TokenStream {
let field_names = fields.clone().map(Ident::to_string);

quote_spanned! {span=>
#[automatically_derived]
impl #generics ::cxx::core::fmt::Debug for #ident #generics {
fn fmt(&self, formatter: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
formatter.debug_struct(#struct_name)
Expand All @@ -159,6 +162,7 @@ fn struct_default(strct: &Struct, span: Span) -> TokenStream {
let fields = strct.fields.iter().map(|field| &field.name.rust);

quote_spanned! {span=>
#[automatically_derived]
#[allow(clippy::derivable_impls)] // different spans than the derived impl
impl #generics ::cxx::core::default::Default for #ident #generics {
fn default() -> Self {
Expand All @@ -178,6 +182,7 @@ fn struct_ord(strct: &Struct, span: Span) -> TokenStream {
let fields = strct.fields.iter().map(|field| &field.name.rust);

quote_spanned! {span=>
#[automatically_derived]
impl #generics ::cxx::core::cmp::Ord for #ident #generics {
fn cmp(&self, other: &Self) -> ::cxx::core::cmp::Ordering {
#(
Expand Down Expand Up @@ -214,6 +219,7 @@ fn struct_partial_ord(strct: &Struct, span: Span) -> TokenStream {
};

quote_spanned! {span=>
#[automatically_derived]
impl #generics ::cxx::core::cmp::PartialOrd for #ident #generics {
#[allow(clippy::non_canonical_partial_ord_impl)]
#[allow(renamed_and_removed_lints, clippy::incorrect_partial_ord_impl_on_ord_type)] // Rust 1.73 and older
Expand All @@ -228,6 +234,7 @@ fn enum_copy(enm: &Enum, span: Span) -> TokenStream {
let ident = &enm.name.rust;

quote_spanned! {span=>
#[automatically_derived]
impl ::cxx::core::marker::Copy for #ident {}
}
}
Expand All @@ -236,6 +243,7 @@ fn enum_clone(enm: &Enum, span: Span) -> TokenStream {
let ident = &enm.name.rust;

quote_spanned! {span=>
#[automatically_derived]
#[allow(clippy::expl_impl_clone_on_copy)]
impl ::cxx::core::clone::Clone for #ident {
fn clone(&self) -> Self {
Expand All @@ -257,6 +265,7 @@ fn enum_debug(enm: &Enum, span: Span) -> TokenStream {
let fallback = format!("{}({{}})", ident);

quote_spanned! {span=>
#[automatically_derived]
impl ::cxx::core::fmt::Debug for #ident {
fn fmt(&self, formatter: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
match *self {
Expand All @@ -272,6 +281,7 @@ fn enum_ord(enm: &Enum, span: Span) -> TokenStream {
let ident = &enm.name.rust;

quote_spanned! {span=>
#[automatically_derived]
impl ::cxx::core::cmp::Ord for #ident {
fn cmp(&self, other: &Self) -> ::cxx::core::cmp::Ordering {
::cxx::core::cmp::Ord::cmp(&self.repr, &other.repr)
Expand All @@ -284,6 +294,7 @@ fn enum_partial_ord(enm: &Enum, span: Span) -> TokenStream {
let ident = &enm.name.rust;

quote_spanned! {span=>
#[automatically_derived]
impl ::cxx::core::cmp::PartialOrd for #ident {
#[allow(clippy::non_canonical_partial_ord_impl)]
#[allow(renamed_and_removed_lints, clippy::incorrect_partial_ord_impl_on_ord_type)] // Rust 1.73 and older
Expand Down
17 changes: 17 additions & 0 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn expand_struct(strct: &Struct) -> TokenStream {
#[repr(C)]
#struct_def

#[automatically_derived]
unsafe impl #generics ::cxx::ExternType for #ident #generics {
#[allow(unused_attributes)] // incorrect lint
#[doc(hidden)]
Expand Down Expand Up @@ -312,6 +313,7 @@ fn expand_struct_forbid_drop(strct: &Struct) -> TokenStream {
let impl_token = Token![impl](strct.visibility.span);

quote_spanned! {span=>
#[automatically_derived]
#impl_token #generics self::Drop for super::#ident #generics {}
}
}
Expand Down Expand Up @@ -358,11 +360,13 @@ fn expand_enum(enm: &Enum) -> TokenStream {
#[repr(transparent)]
#enum_def

#[automatically_derived]
#[allow(non_upper_case_globals)]
impl #ident {
#(#variants)*
}

#[automatically_derived]
unsafe impl ::cxx::ExternType for #ident {
#[allow(unused_attributes)] // incorrect lint
#[doc(hidden)]
Expand Down Expand Up @@ -405,6 +409,7 @@ fn expand_cxx_type(ety: &ExternType) -> TokenStream {
#[repr(C)]
#extern_type_def

#[automatically_derived]
unsafe impl #generics ::cxx::ExternType for #ident #generics {
#[allow(unused_attributes)] // incorrect lint
#[doc(hidden)]
Expand All @@ -428,6 +433,7 @@ fn expand_cxx_type_assert_pinned(ety: &ExternType, types: &Types) -> TokenStream
fn infer() {}
}

#[automatically_derived]
impl<T> __AmbiguousIfImpl<()> for T
where
T: ?::cxx::core::marker::Sized
Expand All @@ -436,6 +442,7 @@ fn expand_cxx_type_assert_pinned(ety: &ExternType, types: &Types) -> TokenStream
#[allow(dead_code)]
struct __Invalid;

#[automatically_derived]
impl<T> __AmbiguousIfImpl<__Invalid> for T
where
T: ?::cxx::core::marker::Sized + ::cxx::core::marker::Unpin,
Expand Down Expand Up @@ -766,6 +773,7 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
&elided_generics
};
quote_spanned! {ident.span()=>
#[automatically_derived]
impl #generics #receiver_ident #receiver_generics {
#doc
#attrs
Expand Down Expand Up @@ -831,6 +839,7 @@ fn expand_rust_type_impl(ety: &ExternType) -> TokenStream {
let unsafe_impl = quote_spanned!(ety.type_token.span=> unsafe impl);

let mut impls = quote_spanned! {span=>
#[automatically_derived]
#[doc(hidden)]
#unsafe_impl #generics ::cxx::private::RustType for #ident #generics {}
};
Expand All @@ -840,6 +849,7 @@ fn expand_rust_type_impl(ety: &ExternType) -> TokenStream {
let type_id = type_id(&ety.name);
let span = derive.span;
impls.extend(quote_spanned! {span=>
#[automatically_derived]
unsafe impl #generics ::cxx::ExternType for #ident #generics {
#[allow(unused_attributes)] // incorrect lint
#[doc(hidden)]
Expand Down Expand Up @@ -920,6 +930,7 @@ fn expand_forbid(impls: TokenStream) -> TokenStream {
quote! {
mod forbid {
pub trait Drop {}
#[automatically_derived]
#[allow(drop_bounds)]
impl<T: ?::cxx::core::marker::Sized + ::cxx::core::ops::Drop> self::Drop for T {}
#impls
Expand Down Expand Up @@ -1301,6 +1312,7 @@ fn expand_rust_box(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let prevent_unwind_drop_label = format!("::{} as Drop>::drop", ident);

quote_spanned! {end_span=>
#[automatically_derived]
#[doc(hidden)]
#unsafe_token impl #impl_generics ::cxx::private::ImplBox for #ident #ty_generics {}
#[doc(hidden)]
Expand Down Expand Up @@ -1359,6 +1371,7 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let prevent_unwind_drop_label = format!("::{} as Drop>::drop", elem);

quote_spanned! {end_span=>
#[automatically_derived]
#[doc(hidden)]
#unsafe_token impl #impl_generics ::cxx::private::ImplVec for #elem #ty_generics {}
#[doc(hidden)]
Expand Down Expand Up @@ -1466,6 +1479,7 @@ fn expand_unique_ptr(
let unsafe_token = format_ident!("unsafe", span = begin_span);

quote_spanned! {end_span=>
#[automatically_derived]
#unsafe_token impl #impl_generics ::cxx::private::UniquePtrTarget for #ident #ty_generics {
fn __typename(f: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
f.write_str(#name)
Expand Down Expand Up @@ -1559,6 +1573,7 @@ fn expand_shared_ptr(
let unsafe_token = format_ident!("unsafe", span = begin_span);

quote_spanned! {end_span=>
#[automatically_derived]
#unsafe_token impl #impl_generics ::cxx::private::SharedPtrTarget for #ident #ty_generics {
fn __typename(f: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
f.write_str(#name)
Expand Down Expand Up @@ -1620,6 +1635,7 @@ fn expand_weak_ptr(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl
let unsafe_token = format_ident!("unsafe", span = begin_span);

quote_spanned! {end_span=>
#[automatically_derived]
#unsafe_token impl #impl_generics ::cxx::private::WeakPtrTarget for #ident #ty_generics {
fn __typename(f: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
f.write_str(#name)
Expand Down Expand Up @@ -1748,6 +1764,7 @@ fn expand_cxx_vector(
};

quote_spanned! {end_span=>
#[automatically_derived]
#unsafe_token impl #impl_generics ::cxx::private::VectorElement for #elem #ty_generics {
fn __typename(f: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result {
f.write_str(#name)
Expand Down

0 comments on commit 6f538a7

Please sign in to comment.