-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Method resolution for trait object types does not prefer inherent methods #51402
Comments
cc #45251 |
Tagging and nominating to discuss whether this behavior is a bug or intended. |
Worth noting in any discussion around this that since inherent methods can only be defined in the same crate as the type, this would only affect any crate which intentionally tried to provide an inherent method for a trait object with the same name as a method on the trait itself. |
Niko explained to me that this issue is actually inverted from what I thought: all trait methods are considered inherent methods of the trait object type. I've removed the I-nominated tag. However, in my use case there's this interesting problem, which can be demonstrated roughly like this: trait Foo {
fn dynamic(&self) -> &dyn Foo where Self: SIzed { self as &dyn Foo }
}
impl dyn Foo {
fn dynamic(&self) -> &dyn Foo { self }
} The conversion requires There's no way to provide a method with a single name for all |
It'd be backwards compatible to remove the whole trait methods are psuedo-inherent methods on trait objects thing. And it seems to be causing more confusion than it seems to save. It's also one extra complexity in the language that's already complex enough. A more conservative option is to only have inherent methods for the methods that are object safe. So in the case of the |
@withoutboats actually, it is possible if you use an additional trait: |
But the user has to explicitly import that trait, which is worse for discoverability and for typing in less advanced IDEs. |
I just ran into this issue, pretty much in the situation described by @withoutboats. In my case, the return type contains |
Normally, inherent methods are given precedence over all trait methods, so that there is no ambiguity.
However, this behavior does not appear to be implemented for trait object types:
https://play.rust-lang.org/?gist=7d4534a5d973249c66244b9affa03199&version=stable&mode=debug
What's worse, UFCS does not allow for specifying that you want the inherent method, because
Foo::f(x)
in this code will remain ambiguous. In general, UFCS isn't designed to support disambiguating to inherent methods because inherent methods are supposed to always take precedence.The text was updated successfully, but these errors were encountered: