]> git.lizzy.rs Git - rust.git/blob - tests/ui/typeck/issue-73592-borrow_mut-through-deref.rs
Auto merge of #107303 - compiler-errors:intern-canonical-var-values, r=lcnr
[rust.git] / tests / ui / typeck / issue-73592-borrow_mut-through-deref.rs
1 // check-pass
2 // run-rustfix
3 //
4 // rust-lang/rust#73592: borrow_mut through Deref should work.
5 //
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`.
11 //
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`.
16 //
17 // This bug is only discovered in #73592 after it is already fixed as a side-effect
18 // of a refactoring made in #72280.
19
20 #![warn(unused_mut)]
21
22 use std::pin::Pin;
23 use std::cell::RefCell;
24
25 struct S(RefCell<()>);
26
27 fn test_pin(s: Pin<&S>) {
28     // This works before #72280.
29     let _ = &mut *s.0.borrow_mut();
30 }
31
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();
35 }
36
37 fn test_vec(s: &Vec<RefCell<()>>) {
38     // This should compile but didn't before #72280.
39     let _ = &mut *s[0].borrow_mut();
40 }
41
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();
45 }
46
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();
50 }
51
52 fn main() {
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));
58     test_vec(&vec![s.0]);
59 }