Closed Kimundi closed 12 months ago
I believe that the decision we came to here: https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution.md#traits
Doesn't explicitly cover this case, but "you need to disambiguate with UFCS" is considered a minor enough fix that it's acceptable.
Doesn't explicitly cover this case, but "you need to disambiguate with UFCS" is considered a minor enough fix that it's acceptable.
I ran into this and when the traits involved have associated types, input/output parameters disambiguation is painful. Anyhow I wish the error messages in E0034 will tell me what do I need to type to disambiguate the calls for each candidate (gonna fill that as a different issue).
Possibly related(?) is the following:
use std::ops::Deref;
struct S;
struct T;
static USIZE: usize = 5;
impl Deref for T {
type Target = usize;
fn deref(&self) -> &Self::Target { &USIZE }
}
trait Check<T> {
fn check(&self, _: &T) {}
}
impl Check<usize> for S {}
//impl Check<u64> for S {} // XXX
fn main() {
S.check(&T);
}
This compiles fine, but if you uncomment the line with XXX
it will error with "the trait Check<T>
is not implemented for S
". While this is technically true, it could unambiguously coerce. Is there a rule against this written down somewhere (I can imagine there might be, just to keep things simple)?
Triage: The previous comment is unrelated to the rest of the issue. Otherwise, the only actionable part of this issue is to add a lint to warn when a trait has a function with the same name as one of its supertraits' functions. Due to the edge-case nature and the errors that'll be seen first, I propose refiling that lint against Clippy and closing this issue.
I ran into this as well. The UFCS syntax is unfortunately not as ergonomic as normal method syntax, it'd be nice if there was a way to use method syntax.
Would it be possible to resolve this by having the subtrait's items shadow the supertrait's items, so that a method syntax call would resolve to the subtrait? The supertrait's items could still be referenced through UFCS.
I realize that changing the language to work this way would probably require an RFC. I'd just like to ask if there are any known issues that would prevent this.
Closing this as the clippy issue covers it to the extent of what can be done.
During the writing of this issue I reached the conclusion that the current semantic is probably the best solution to this problem already, but I'm opening this issue anyway to encourage discussion or at least getting confirmation from official site that this is the intended behavior.
Right now, you can use the same method name in different traits, even if they exist in a inheritance relation, eg this compiles:
This is currently possible to prevent a fragile base class problem: A library maintainer providing a supertrait should be able to add an new method without breaking downstream code.
However, even if you don't explicitly import
Foo
orBar
, any place where you try to use thefoo()
method ofBar
you get a "conflicting methods in scope" error, because super traits are automatically brought in scope for method resolution:In generics:
In trait objects:
Which means in practice you'd run into this problem anyway.
Calling associated functions works though, so you could use UFCS to differentiate the methods:
However, a conflict like this still forces a code change decision on the provider of the trait and all downstream users:
Both options are approximately equally big changes for downstream users, and none of them allows conflicting method names to be used with the original dot syntax, so the question is if this is working as intended, or whether it might be improved upon.
At the very least, it might be a good idea to implement a warn-per-default lint for the conflicting method name in the sub trait definition.