Skip to content

don't generate extra impl for Eq assertions #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 1 addition & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,7 @@ fn generate_impl(
let body = generate_body(derive_where, trait_, item, generics);

let ident = item.ident();
let mut output = trait_.impl_item(crate_, full_item, imp, ident, ty, &where_clause, body);

if let Some((path, body)) = trait_.additional_impl() {
output.extend(quote! {
#[automatically_derived]
impl #imp #path for #ident #ty
#where_clause
{
#body
}
})
}

output
trait_.impl_item(crate_, full_item, imp, ident, ty, &where_clause, body)
}

/// Generate implementation method body for a [`Trait`].
Expand Down
68 changes: 22 additions & 46 deletions src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,15 @@ fn struct_() -> Result<()> {
}
}

const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl<T> DeriveWhereAssertEq for Test<T> {
fn assert(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> {
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

// For some reason the comparison fails without the extra space at the end.
let _: __AssertEq<std::marker::PhantomData<T> >;
}
// place extra space to avoid token being printed as `>>`
let _: __AssertEq<std::marker::PhantomData<T> >;
}
};

#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> { }
}

#[automatically_derived]
impl<T> ::core::hash::Hash for Test<T> {
Expand Down Expand Up @@ -148,23 +140,15 @@ fn tuple() -> Result<()> {
}
}

const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl<T> DeriveWhereAssertEq for Test<T> {
fn assert(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> {
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

// For some reason the comparison fails without the extra space at the end.
let _: __AssertEq<std::marker::PhantomData<T> >;
}
// place extra space to avoid token being printed as `>>`
let _: __AssertEq<std::marker::PhantomData<T> >;
}
};

#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> { }
}

#[automatically_derived]
impl<T> ::core::hash::Hash for Test<T> {
Expand Down Expand Up @@ -301,24 +285,16 @@ fn enum_() -> Result<()> {
}
}

const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl<T> DeriveWhereAssertEq for Test<T> {
fn assert(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> {
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

// For some reason the comparison fails without the extra space at the end.
let _: __AssertEq<std::marker::PhantomData<T> >;
let _: __AssertEq<std::marker::PhantomData<T> >;
}
// place extra space to avoid token being printed as `>>`
let _: __AssertEq<std::marker::PhantomData<T> >;
let _: __AssertEq<std::marker::PhantomData<T> >;
}
};

#[automatically_derived]
impl<T> ::core::cmp::Eq for Test<T> { }
}

#[automatically_derived]
impl<T> ::core::hash::Hash for Test<T> {
Expand Down
56 changes: 16 additions & 40 deletions src/test/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,17 @@ fn check_trait_bounds() -> Result<()> {
}
}

const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl<T, U> DeriveWhereAssertEq for Test<T, U>
where T: ::core::cmp::Eq
{
fn assert(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

// For some reason the comparison fails without the extra space at the end.
let _: __AssertEq<T >;
let _: __AssertEq<std::marker::PhantomData<U> >;
}
}
};

#[automatically_derived]
impl<T, U> ::core::cmp::Eq for Test<T, U>
where T: ::core::cmp::Eq
{ }
{
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

let _: __AssertEq<T>;
let _: __AssertEq<std::marker::PhantomData<U> >;
}
}

#[automatically_derived]
impl<T, U> ::core::hash::Hash for Test<T, U>
Expand Down Expand Up @@ -386,32 +375,19 @@ fn check_multiple_trait_bounds() -> Result<()> {
}
}

const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl<T, U, V> DeriveWhereAssertEq for Test<T, U, V>
where
T: ::core::cmp::Eq,
U: ::core::cmp::Eq
{
fn assert(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

// For some reason the comparison fails without the extra space at the end.
let _: __AssertEq<T >;
let _: __AssertEq<std::marker::PhantomData<(U, V)> >;
}
}
};

#[automatically_derived]
impl<T, U, V> ::core::cmp::Eq for Test<T, U, V>
where
T: ::core::cmp::Eq,
U: ::core::cmp::Eq
{ }
{
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);
let _: __AssertEq<T>;
// place extra space to avoid token being printed as `>>`
let _: __AssertEq<std::marker::PhantomData<(U, V)> >;
}
}

#[automatically_derived]
impl<T, U, V> ::core::hash::Hash for Test<T, U, V>
Expand Down
5 changes: 0 additions & 5 deletions src/trait_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,6 @@ pub trait TraitImpl: Deref<Target = Trait> {
/// Returns fully qualified [`Path`] for this trait.
fn path(&self) -> Path;

/// Additional implementation to add for this [`Trait`].
fn additional_impl(&self) -> Option<(Path, TokenStream)> {
None
}

/// Trait to implement. Only used by [`Eq`] and
/// [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html).
#[allow(clippy::too_many_arguments)]
Expand Down
35 changes: 3 additions & 32 deletions src/trait_/eq.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! [`Eq`](trait@std::cmp::Eq) implementation.

use std::{borrow::Cow, ops::Deref};
use std::ops::Deref;

use proc_macro2::TokenStream;
use quote::quote;
use syn::{DeriveInput, Ident, ImplGenerics, Path, TypeGenerics, WhereClause};
use syn::Path;

use crate::{util, Data, DeriveTrait, DeriveWhere, Item, SplitGenerics, Trait, TraitImpl};

Expand All @@ -24,35 +24,6 @@ impl TraitImpl for Eq {
util::path_from_strs(&["core", "cmp", "Eq"])
}

fn additional_impl(&self) -> Option<(Path, TokenStream)> {
Some((self.path(), quote! {}))
}

fn impl_item(
&self,
_: Option<&Path>,
_: &DeriveInput,
imp: &ImplGenerics<'_>,
ident: &Ident,
ty: &TypeGenerics<'_>,
where_clause: &Option<Cow<'_, WhereClause>>,
body: TokenStream,
) -> TokenStream {
quote! {
const _: () = {
trait DeriveWhereAssertEq {
fn assert(&self);
}

impl #imp DeriveWhereAssertEq for #ident #ty
#where_clause
{
#body
}
};
}
}

fn build_signature(
&self,
_derive_where: &DeriveWhere,
Expand All @@ -61,7 +32,7 @@ impl TraitImpl for Eq {
body: &TokenStream,
) -> TokenStream {
quote! {
fn assert(&self) {
fn assert_receiver_is_total_eq(&self) {
struct __AssertEq<__T: ::core::cmp::Eq + ?::core::marker::Sized>(::core::marker::PhantomData<__T>);

#body
Expand Down