Skip to content
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

Trait method has higher precedence than inherent method if autoref is required for the inherent method #68826

Closed
jplatte opened this issue Feb 4, 2020 · 2 comments
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jplatte
Copy link
Contributor

jplatte commented Feb 4, 2020

I was very surprised when I found the following outputs "trait method" (playground):

struct Struct;

impl Struct {
    fn foo(&self) {
        println!("inherent method")
    }
}

pub trait Foo {
    fn foo(self);
}

impl Foo for Struct {
    fn foo(self) {
        println!("trait method")
    }
}

fn main() {
    Struct.foo();
}

Up until now I always thought that an inherent method always takes precedence over a trait method of the same name. Is that not true in this case / is this behaviour intended?

This came up in yew, which defined an inherent try_into (now renamed) with a type parameter that would start producing errors about unexpected type parameters when TryInto (or stdweb::unstable::TryInto) was brought into scope.

@estebank estebank added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Feb 4, 2020
@nikomatsakis
Copy link
Contributor

This is the current behavior. It can indeed be surprising but making changes here would have pretty dramatic effects and would require us to invest a lot of effort into the transition (definitely this would require an edition change). I'm going to go ahead and close this issue -- if we wanted to pursue it, the right thing to do would be to file a lang team project proposal, but I can say now that I would be somewhat reluctant to start in on this work.

@jplatte
Copy link
Contributor Author

jplatte commented Sep 4, 2021

I saw this came up again with TryInto entering the prelude (#86657 fixes the lint to behave correctly around autoref). Is there some documentation for the current behavior somewhere? If not, should I open a new issue about that or can we reopen this one to track documentation of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants