Files
rust/tests/ui/methods/shadowed-intrinsic-method-deref.rs
T
Esteban Küber 73fe905f3c Detect inherent method behind deref being shadowed by trait method
```
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.
2026-03-10 23:24:27 +00:00

22 lines
635 B
Rust

//@ run-rustfix
#![allow(unused_imports)]
use std::rc::Rc;
use std::cell::RefCell;
use std::borrow::Borrow; // Without this import, the code would compile.
pub struct S {
flag: bool,
}
type SCell = Rc<RefCell<S>>;
fn main() {
// Type annotations just for clarity
let s : SCell = Rc::new(RefCell::new(S {flag: false}));
let sb : &S = &s.borrow();
//~^ ERROR: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied [E0277]
//~| NOTE: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
//~| NOTE: there's an inherent method on `RefCell<S>` of the same name
println!("{:?}", sb.flag);
}