4 // rust-lang/rust#73592: borrow_mut through Deref should work.
6 // Before #72280, when we see something like `&mut *rcvr.method()`, we
7 // incorrectly requires `rcvr` to be type-checked as a mut place. While this
8 // requirement is usually correct for smart pointers, it is overly restrictive
9 // for types like `Mutex` or `RefCell` which can produce a guard that
10 // implements `DerefMut` from `&self`.
12 // Making it more confusing, because we use Deref as the fallback when DerefMut
13 // is implemented, we won't see an issue when the smart pointer does not
14 // implement `DerefMut`. It only causes an issue when `rcvr` is obtained via a
15 // type that implements both `Deref` or `DerefMut`.
17 // This bug is only discovered in #73592 after it is already fixed as a side-effect
18 // of a refactoring made in #72280.
23 use std::cell::RefCell;
25 struct S(RefCell<()>);
27 fn test_pin(s: Pin<&S>) {
28 // This works before #72280.
29 let _ = &mut *s.0.borrow_mut();
32 fn test_pin_mut(s: Pin<&mut S>) {
33 // This should compile but didn't before #72280.
34 let _ = &mut *s.0.borrow_mut();
37 fn test_vec(s: &Vec<RefCell<()>>) {
38 // This should compile but didn't before #72280.
39 let _ = &mut *s[0].borrow_mut();
42 fn test_mut_pin(mut s: Pin<&S>) {
43 //~^ WARN variable does not need to be mutable
44 let _ = &mut *s.0.borrow_mut();
47 fn test_mut_pin_mut(mut s: Pin<&mut S>) {
48 //~^ WARN variable does not need to be mutable
49 let _ = &mut *s.0.borrow_mut();
53 let mut s = S(RefCell::new(()));
54 test_pin(Pin::new(&s));
55 test_pin_mut(Pin::new(&mut s));
56 test_mut_pin(Pin::new(&s));
57 test_mut_pin_mut(Pin::new(&mut s));