Skip to content

Detect inherent method behind deref being shadowed by trait method#153674

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-41906
Mar 11, 2026
Merged

Detect inherent method behind deref being shadowed by trait method#153674
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
estebank:issue-41906

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Mar 10, 2026

error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |

In the example above, method borrow is available both on <RefCell<S> as Borrow<S>> and on RefCell<S>. Adding the import use std::borrow::Borrow; causes s.borrow() to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.

Fix #41906. CC #153662.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2026

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 69 candidates
  • Random selection from 17 candidates

@rust-log-analyzer

This comment has been minimized.

```
error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |
```

In the example above, method `borrow` is available both on `<RefCell<S> as Borrow<S>>` *and* on `RefCell<S>`. Adding the import `use std::borrow::Borrow;` causes `s.borrow()` to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.
@petrochenkov
Copy link
Contributor

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 11, 2026

📌 Commit 73fe905 has been approved by petrochenkov

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 11, 2026
rust-bors bot pushed a commit that referenced this pull request Mar 11, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #153072 (Allow merging all libcore/alloc doctests into a single binary)
 - #153408 (miri: make read_discriminant UB when the tag is not in the validity range of the tag field)
 - #153674 (Detect inherent method behind deref being shadowed by trait method)
 - #153689 (Eliminate `QueryLatchInfo`.)
@rust-bors rust-bors bot merged commit 3679ba7 into rust-lang:main Mar 11, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collision of Borrow::borrow() and RefCell::borrow()

4 participants