]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-borrow-overloaded-auto-deref.rs
Do not use a suggestion to change a binding's name to a type
[rust.git] / src / test / ui / borrowck / borrowck-borrow-overloaded-auto-deref.rs
1 // Test how overloaded deref interacts with borrows when only
2 // Deref and not DerefMut is implemented.
3
4 use std::ops::Deref;
5 use std::rc::Rc;
6
7 struct Point {
8     x: isize,
9     y: isize
10 }
11
12 impl Point {
13     fn get(&self) -> (isize, isize) {
14         (self.x, self.y)
15     }
16
17     fn set(&mut self, x: isize, y: isize) {
18         self.x = x;
19         self.y = y;
20     }
21
22     fn x_ref(&self) -> &isize {
23         &self.x
24     }
25
26     fn y_mut(&mut self) -> &mut isize {
27         &mut self.y
28     }
29 }
30
31 fn deref_imm_field(x: Rc<Point>) {
32     let __isize = &x.y;
33 }
34
35 fn deref_mut_field1(x: Rc<Point>) {
36     let __isize = &mut x.y; //~ ERROR cannot borrow
37 }
38
39 fn deref_mut_field2(mut x: Rc<Point>) {
40     let __isize = &mut x.y; //~ ERROR cannot borrow
41 }
42
43 fn deref_extend_field(x: &Rc<Point>) -> &isize {
44     &x.y
45 }
46
47 fn deref_extend_mut_field1(x: &Rc<Point>) -> &mut isize {
48     &mut x.y //~ ERROR cannot borrow
49 }
50
51 fn deref_extend_mut_field2(x: &mut Rc<Point>) -> &mut isize {
52     &mut x.y //~ ERROR cannot borrow
53 }
54
55 fn assign_field1<'a>(x: Rc<Point>) {
56     x.y = 3; //~ ERROR cannot assign
57 }
58
59 fn assign_field2<'a>(x: &'a Rc<Point>) {
60     x.y = 3; //~ ERROR cannot assign
61 }
62
63 fn assign_field3<'a>(x: &'a mut Rc<Point>) {
64     x.y = 3; //~ ERROR cannot assign
65 }
66
67 fn deref_imm_method(x: Rc<Point>) {
68     let __isize = x.get();
69 }
70
71 fn deref_mut_method1(x: Rc<Point>) {
72     x.set(0, 0); //~ ERROR cannot borrow
73 }
74
75 fn deref_mut_method2(mut x: Rc<Point>) {
76     x.set(0, 0); //~ ERROR cannot borrow
77 }
78
79 fn deref_extend_method(x: &Rc<Point>) -> &isize {
80     x.x_ref()
81 }
82
83 fn deref_extend_mut_method1(x: &Rc<Point>) -> &mut isize {
84     x.y_mut() //~ ERROR cannot borrow
85 }
86
87 fn deref_extend_mut_method2(x: &mut Rc<Point>) -> &mut isize {
88     x.y_mut() //~ ERROR cannot borrow
89 }
90
91 fn assign_method1<'a>(x: Rc<Point>) {
92     *x.y_mut() = 3; //~ ERROR cannot borrow
93 }
94
95 fn assign_method2<'a>(x: &'a Rc<Point>) {
96     *x.y_mut() = 3; //~ ERROR cannot borrow
97 }
98
99 fn assign_method3<'a>(x: &'a mut Rc<Point>) {
100     *x.y_mut() = 3; //~ ERROR cannot borrow
101 }
102
103 pub fn main() {}