]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-borrow-overloaded-auto-deref-mut.rs
Update compile fail tests to use isize.
[rust.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 _i = &x.y;
60 }
61
62 fn deref_mut_field1(x: Own<Point>) {
63     let _i = &mut x.y; //~ ERROR cannot borrow
64 }
65
66 fn deref_mut_field2(mut x: Own<Point>) {
67     let _i = &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 // FIXME(eddyb) #12825 This shouldn't attempt to call deref_mut.
115 /*
116 fn deref_imm_method(x: Own<Point>) {
117     let _i = x.get();
118 }
119 */
120
121 fn deref_mut_method1(x: Own<Point>) {
122     x.set(0, 0); //~ ERROR cannot borrow
123 }
124
125 fn deref_mut_method2(mut x: Own<Point>) {
126     x.set(0, 0);
127 }
128
129 fn deref_extend_method(x: &Own<Point>) -> &isize {
130     x.x_ref()
131 }
132
133 fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
134     x.y_mut() //~ ERROR cannot borrow
135 }
136
137 fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
138     x.y_mut()
139 }
140
141 fn assign_method1<'a>(x: Own<Point>) {
142     *x.y_mut() = 3; //~ ERROR cannot borrow
143 }
144
145 fn assign_method2<'a>(x: &'a Own<Point>) {
146     *x.y_mut() = 3; //~ ERROR cannot borrow
147 }
148
149 fn assign_method3<'a>(x: &'a mut Own<Point>) {
150     *x.y_mut() = 3;
151 }
152
153 pub fn main() {}