-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Collision of Borrow::borrow() and RefCell::borrow() #41906
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
Comments
It's an ugly problem with auto-deref, but you can still write it manually as FWIW, this conflict is also why |
@cuviper Yeah, While solvable with the current syntax, I would say (from my own experience) that it is rather confusing, especially since it does apply to a common construct (e.g. Any thoughts on adding a hint for this kind of error for |
Adding a hint sounds great, but I can't guide you as I haven't yet learned how to do this myself. :) |
We can add a |
@durka Thanks for the hint and a pointer, but I think this is too general: whenever Any other ideas? Perhaps some hint message would help even in the more general cases but I do not see the right wording. |
I have relabeled this as a diagnostics bug -- I wonder if it could make sense to just lint on the |
IDE's like the clion rust plugin can automatically import std::borrow::Borrow when you are using the auto-complete leading to confusion when code suddenly stops compiling when it looks the same. Its not easy to notice an extra import was added. |
The trait
std::borrow::Borrow
and typestd::rc::RefCell
both have a methodborrow()
. This leads to ambiguity, possibly hard-to-understand errors and most importantly breaking unrelated code by addinguse std::borrow::Borrow;
.Consider the following code:
(Rust playground link)
This code compiles and works as intended. But when you add
use std::borrow::Borrow;
, the compiler complains:The problem is that the method call
s.borrow()
is applied toRc
which is an instance ofBorrow
rather than to the containedRefCell
as intended (and as working in the example above), and the error just complains that this borrow is not possible (which is fine).This is mentioned by @spirali in related #41865, but this issue deals with a different part of the problem. (Namely that if you remove the type annotation from
let sb : &S = ...
, the error message will complain about ambiguity of theBorrow::borrow()
return type in a confusing way.)I would propose to change the method name for
RefCell::borrow
to something else (a painful change, but leaving such bugs around things implementing theDeref
trait might be even worse for the language long-term) and avoid this naming collisions whenever possible (is there a guideline for that?). Any other solutions?The text was updated successfully, but these errors were encountered: