]> git.lizzy.rs Git - rust.git/blob - tests/ui/span/borrowck-borrow-overloaded-auto-deref-mut.rs
Rollup merge of #106441 - mllken:abstract-socket-noref, r=joshtriplett
[rust.git] / tests / ui / span / borrowck-borrow-overloaded-auto-deref-mut.rs
1 // Test how overloaded deref interacts with borrows when DerefMut
2 // is implemented.
3
4 use std::ops::{Deref, DerefMut};
5
6 struct Own<T> {
7     value: *mut T
8 }
9
10 impl<T> Deref for Own<T> {
11     type Target = T;
12
13     fn deref(&self) -> &T {
14         unsafe { &*self.value }
15     }
16 }
17
18 impl<T> DerefMut for Own<T> {
19     fn deref_mut(&mut self) -> &mut T {
20         unsafe { &mut *self.value }
21     }
22 }
23
24 struct Point {
25     x: isize,
26     y: isize
27 }
28
29 impl Point {
30     fn get(&self) -> (isize, isize) {
31         (self.x, self.y)
32     }
33
34     fn set(&mut self, x: isize, y: isize) {
35         self.x = x;
36         self.y = y;
37     }
38
39     fn x_ref(&self) -> &isize {
40         &self.x
41     }
42
43     fn y_mut(&mut self) -> &mut isize {
44         &mut self.y
45     }
46 }
47
48 fn deref_imm_field(x: Own<Point>) {
49     let __isize = &x.y;
50 }
51
52 fn deref_mut_field1(x: Own<Point>) {
53     let __isize = &mut x.y; //~ ERROR cannot borrow
54 }
55
56 fn deref_mut_field2(mut x: Own<Point>) {
57     let __isize = &mut x.y;
58 }
59
60 fn deref_extend_field(x: &Own<Point>) -> &isize {
61     &x.y
62 }
63
64 fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
65     &mut x.y //~ ERROR cannot borrow
66 }
67
68 fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
69     &mut x.y
70 }
71
72 fn deref_extend_mut_field3(x: &mut Own<Point>) {
73     // Hmm, this is unfortunate, because with box it would work,
74     // but it's presently the expected outcome. See `deref_extend_mut_field4`
75     // for the workaround.
76
77     let _x = &mut x.x;
78     let _y = &mut x.y; //~ ERROR cannot borrow
79     use_mut(_x);
80 }
81 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
82     let p = &mut **x;
83     let _x = &mut p.x;
84     let _y = &mut p.y;
85 }
86
87 fn assign_field1<'a>(x: Own<Point>) {
88     x.y = 3; //~ ERROR cannot borrow
89 }
90
91 fn assign_field2<'a>(x: &'a Own<Point>) {
92     x.y = 3; //~ ERROR cannot borrow
93 }
94
95 fn assign_field3<'a>(x: &'a mut Own<Point>) {
96     x.y = 3;
97 }
98
99 fn assign_field4<'a>(x: &'a mut Own<Point>) {
100     let _p: &mut Point = &mut **x;
101     x.y = 3; //~ ERROR cannot borrow
102     use_mut(_p);
103 }
104 fn deref_imm_method(x: Own<Point>) {
105     let __isize = x.get();
106 }
107
108 fn deref_mut_method1(x: Own<Point>) {
109     x.set(0, 0); //~ ERROR cannot borrow
110 }
111
112 fn deref_mut_method2(mut x: Own<Point>) {
113     x.set(0, 0);
114 }
115
116 fn deref_extend_method(x: &Own<Point>) -> &isize {
117     x.x_ref()
118 }
119
120 fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
121     x.y_mut() //~ ERROR cannot borrow
122 }
123
124 fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
125     x.y_mut()
126 }
127
128 fn assign_method1<'a>(x: Own<Point>) {
129     *x.y_mut() = 3; //~ ERROR cannot borrow
130 }
131
132 fn assign_method2<'a>(x: &'a Own<Point>) {
133     *x.y_mut() = 3; //~ ERROR cannot borrow
134 }
135
136 fn assign_method3<'a>(x: &'a mut Own<Point>) {
137     *x.y_mut() = 3;
138 }
139
140 pub fn main() {}
141
142 fn use_mut<T>(_: &mut T) {}