-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Conflicting method names in the same trait hierachy can only ever be called with UFCS syntax #17151
Comments
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. |
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:
This compiles fine, but if you uncomment the line with |
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. |
Refiled as rust-lang/rust-clippy#3117 |
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. |
fix: Fix attributes on generic parameters colliding in item tree Fixes rust-lang/rust-analyzer#16141
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.
The text was updated successfully, but these errors were encountered: