Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upCollision of Borrow::borrow() and RefCell::borrow() #41906
Comments
gavento
changed the title
Naming collision of Borrow::borrow() and Rc::borrow()
Naming collision of Borrow::borrow() and RefCell::borrow()
May 11, 2017
gavento
changed the title
Naming collision of Borrow::borrow() and RefCell::borrow()
Collision of Borrow::borrow() and RefCell::borrow()
May 11, 2017
This comment has been minimized.
This comment has been minimized.
|
It's an ugly problem with auto-deref, but you can still write it manually as FWIW, this conflict is also why |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
Adding a hint sounds great, but I can't guide you as I haven't yet learned how to do this myself. :) |
This comment has been minimized.
This comment has been minimized.
|
We can add a |
This comment has been minimized.
This comment has been minimized.
|
@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. |
gavento commentedMay 11, 2017
The trait
std::borrow::Borrowand typestd::rc::RefCellboth 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 toRcwhich is an instance ofBorrowrather than to the containedRefCellas 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::borrowto something else (a painful change, but leaving such bugs around things implementing theDereftrait 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?