]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/borrowck-borrow-overloaded-auto-deref-mut.rs
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / test / compile-fail / borrowck-borrow-overloaded-auto-deref-mut.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test how overloaded deref interacts with borrows when DerefMut
12 // is implemented.
13
14 use std::ops::{Deref, DerefMut};
15
16 struct Own<T> {
17 value: *mut T
18 }
19
20 impl<T> Deref for Own<T> {
21 type Target = T;
22
23 fn deref(&self) -> &T {
24 unsafe { &*self.value }
25 }
26 }
27
28 impl<T> DerefMut for Own<T> {
29 fn deref_mut(&mut self) -> &mut T {
30 unsafe { &mut *self.value }
31 }
32 }
33
34 struct Point {
35 x: isize,
36 y: isize
37 }
38
39 impl Point {
40 fn get(&self) -> (isize, isize) {
41 (self.x, self.y)
42 }
43
44 fn set(&mut self, x: isize, y: isize) {
45 self.x = x;
46 self.y = y;
47 }
48
49 fn x_ref(&self) -> &isize {
50 &self.x
51 }
52
53 fn y_mut(&mut self) -> &mut isize {
54 &mut self.y
55 }
56 }
57
58 fn deref_imm_field(x: Own<Point>) {
59 let __isize = &x.y;
60 }
61
62 fn deref_mut_field1(x: Own<Point>) {
63 let __isize = &mut x.y; //~ ERROR cannot borrow
64 }
65
66 fn deref_mut_field2(mut x: Own<Point>) {
67 let __isize = &mut x.y;
68 }
69
70 fn deref_extend_field(x: &Own<Point>) -> &isize {
71 &x.y
72 }
73
74 fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
75 &mut x.y //~ ERROR cannot borrow
76 }
77
78 fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
79 &mut x.y
80 }
81
82 fn deref_extend_mut_field3(x: &mut Own<Point>) {
83 // Hmm, this is unfortunate, because with box it would work,
84 // but it's presently the expected outcome. See `deref_extend_mut_field4`
85 // for the workaround.
86
87 let _x = &mut x.x;
88 let _y = &mut x.y; //~ ERROR cannot borrow
89 }
90
91 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
92 let p = &mut **x;
93 let _x = &mut p.x;
94 let _y = &mut p.y;
95 }
96
97 fn assign_field1<'a>(x: Own<Point>) {
98 x.y = 3; //~ ERROR cannot borrow
99 }
100
101 fn assign_field2<'a>(x: &'a Own<Point>) {
102 x.y = 3; //~ ERROR cannot assign
103 }
104
105 fn assign_field3<'a>(x: &'a mut Own<Point>) {
106 x.y = 3;
107 }
108
109 fn assign_field4<'a>(x: &'a mut Own<Point>) {
110 let _p: &mut Point = &mut **x;
111 x.y = 3; //~ ERROR cannot borrow
112 }
113
114 fn deref_imm_method(x: Own<Point>) {
115 let __isize = x.get();
116 }
117
118 fn deref_mut_method1(x: Own<Point>) {
119 x.set(0, 0); //~ ERROR cannot borrow
120 }
121
122 fn deref_mut_method2(mut x: Own<Point>) {
123 x.set(0, 0);
124 }
125
126 fn deref_extend_method(x: &Own<Point>) -> &isize {
127 x.x_ref()
128 }
129
130 fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
131 x.y_mut() //~ ERROR cannot borrow
132 }
133
134 fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
135 x.y_mut()
136 }
137
138 fn assign_method1<'a>(x: Own<Point>) {
139 *x.y_mut() = 3; //~ ERROR cannot borrow
140 }
141
142 fn assign_method2<'a>(x: &'a Own<Point>) {
143 *x.y_mut() = 3; //~ ERROR cannot borrow
144 }
145
146 fn assign_method3<'a>(x: &'a mut Own<Point>) {
147 *x.y_mut() = 3;
148 }
149
150 pub fn main() {}