-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Collision of Borrow::borrow() and RefCell::borrow() #41906
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
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?