]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-overloaded-index-and-overloaded-deref.rs
Rollup merge of #99460 - JanBeh:PR_asref_asmut_docs, r=joshtriplett
[rust.git] / src / test / ui / borrowck / borrowck-overloaded-index-and-overloaded-deref.rs
1 // Check that we properly record borrows when we are doing an
2 // overloaded, autoderef of a value obtained via an overloaded index
3 // operator. The accounting of the all the implicit things going on
4 // here is rather subtle. Issue #20232.
5
6 use std::ops::{Deref, Index};
7
8 struct MyVec<T> { x: T }
9
10 impl<T> Index<usize> for MyVec<T> {
11     type Output = T;
12     fn index(&self, _: usize) -> &T {
13         &self.x
14     }
15 }
16
17 struct MyPtr<T> { x: T }
18
19 impl<T> Deref for MyPtr<T> {
20     type Target = T;
21     fn deref(&self) -> &T {
22         &self.x
23     }
24 }
25
26 struct Foo { f: usize }
27
28 fn main() {
29     let mut v = MyVec { x: MyPtr { x: Foo { f: 22 } } };
30     let i = &v[0].f;
31     v = MyVec { x: MyPtr { x: Foo { f: 23 } } };
32     //~^ ERROR cannot assign to `v` because it is borrowed
33     read(*i);
34 }
35
36 fn read(_: usize) { }