Skip to content

Use explicit T::foo(self, args..) calls in generated methods #40

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

Merged
merged 4 commits into from
Oct 10, 2018
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
9 changes: 4 additions & 5 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn gen_method_item(

// Generate the body of the function. This mainly depends on the self type,
// but also on the proxy type.
let name = &sig.ident;
let fn_name = &sig.ident;
let body = match self_arg {
// Fn proxy types get a special treatment
_ if proxy_type.is_fn() => {
Expand All @@ -476,25 +476,24 @@ fn gen_method_item(
// No receiver
SelfType::None => {
// The proxy type is a reference, smartpointer or Box.
quote! { #proxy_ty_param::#name #generic_types(#args) }
quote! { #proxy_ty_param::#fn_name #generic_types(#args) }
}

// Receiver `self` (by value)
SelfType::Value => {
// The proxy type is a Box.
quote! { (*self).#name #generic_types(#args) }
quote! { #proxy_ty_param::#fn_name #generic_types(*self, #args) }
}

// `&self` or `&mut self` receiver
SelfType::Ref | SelfType::Mut => {
// The proxy type could be anything in the `Ref` case, and `&mut`
// or Box in the `Mut` case.
quote! { (*self).#name #generic_types(#args) }
quote! { #proxy_ty_param::#fn_name #generic_types(self, #args) }
}
};

// Combine body with signature
// TODO: maybe add `#[inline]`?
Ok(quote! { #sig { #body }})
}

Expand Down
26 changes: 26 additions & 0 deletions tests/compile-pass/method_name_shadowing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use auto_impl::auto_impl;

trait AllExt {
fn foo(&self, _: i32);
}

impl<T> AllExt for T {
fn foo(&self, _: i32) {}
}

// This will expand to:
//
// impl<T: Foo> Foo for &T {
// fn foo(&self, _x: bool) {
// T::foo(self, _x)
// }
// }
//
// With this test we want to make sure, that the call `T::foo` is always
// unambiguous. Luckily, Rust is nice here. And if we only know `T: Foo`, then
// other global functions are not even considered. Having a test for this
// doesn't hurt though.
#[auto_impl(&)]
trait Foo {
fn foo(&self, _x: bool);
}