]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-overloaded-index-autoderef.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-overloaded-index-autoderef.rs
1 // Test that we still see borrowck errors of various kinds when using
2 // indexing and autoderef in combination.
3
4 use std::ops::{Index, IndexMut};
5
6
7
8 struct Foo {
9     x: isize,
10     y: isize,
11 }
12
13 impl<'a> Index<&'a String> for Foo {
14     type Output = isize;
15
16     fn index(&self, z: &String) -> &isize {
17         if *z == "x" {
18             &self.x
19         } else {
20             &self.y
21         }
22     }
23 }
24
25 impl<'a> IndexMut<&'a String> for Foo {
26     fn index_mut(&mut self, z: &String) -> &mut isize {
27         if *z == "x" {
28             &mut self.x
29         } else {
30             &mut self.y
31         }
32     }
33 }
34
35 fn test1(mut f: Box<Foo>, s: String) {
36     let p = &mut f[&s];
37     let q = &f[&s]; //~ ERROR cannot borrow
38     p.use_mut();
39 }
40
41 fn test2(mut f: Box<Foo>, s: String) {
42     let p = &mut f[&s];
43     let q = &mut f[&s]; //~ ERROR cannot borrow
44     p.use_mut();
45 }
46
47 struct Bar {
48     foo: Foo
49 }
50
51 fn test3(mut f: Box<Bar>, s: String) {
52     let p = &mut f.foo[&s];
53     let q = &mut f.foo[&s]; //~ ERROR cannot borrow
54     p.use_mut();
55 }
56
57 fn test4(mut f: Box<Bar>, s: String) {
58     let p = &f.foo[&s];
59     let q = &f.foo[&s];
60     p.use_ref();
61 }
62
63 fn test5(mut f: Box<Bar>, s: String) {
64     let p = &f.foo[&s];
65     let q = &mut f.foo[&s]; //~ ERROR cannot borrow
66     p.use_ref();
67 }
68
69 fn test6(mut f: Box<Bar>, g: Foo, s: String) {
70     let p = &f.foo[&s];
71     f.foo = g; //~ ERROR cannot assign
72     p.use_ref();
73 }
74
75 fn test7(mut f: Box<Bar>, g: Bar, s: String) {
76     let p = &f.foo[&s];
77     *f = g; //~ ERROR cannot assign
78     p.use_ref();
79 }
80
81 fn test8(mut f: Box<Bar>, g: Foo, s: String) {
82     let p = &mut f.foo[&s];
83     f.foo = g; //~ ERROR cannot assign
84     p.use_mut();
85 }
86
87 fn test9(mut f: Box<Bar>, g: Bar, s: String) {
88     let p = &mut f.foo[&s];
89     *f = g; //~ ERROR cannot assign
90     p.use_mut();
91 }
92
93 fn main() {
94 }
95
96 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
97 impl<T> Fake for T { }