]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-borrow-overloaded-auto-deref-mut.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[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<T> for Own<T> {
21     fn deref<'a>(&'a self) -> &'a T {
22         unsafe { &*self.value }
23     }
24 }
25
26 impl<T> DerefMut<T> for Own<T> {
27     fn deref_mut<'a>(&'a mut self) -> &'a mut T {
28         unsafe { &mut *self.value }
29     }
30 }
31
32 struct Point {
33     x: int,
34     y: int
35 }
36
37 impl Point {
38     fn get(&self) -> (int, int) {
39         (self.x, self.y)
40     }
41
42     fn set(&mut self, x: int, y: int) {
43         self.x = x;
44         self.y = y;
45     }
46
47     fn x_ref<'a>(&'a self) -> &'a int {
48         &self.x
49     }
50
51     fn y_mut<'a>(&'a mut self) -> &'a mut int {
52         &mut self.y
53     }
54 }
55
56 fn deref_imm_field(x: Own<Point>) {
57     let _i = &x.y;
58 }
59
60 fn deref_mut_field1(x: Own<Point>) {
61     let _i = &mut x.y; //~ ERROR cannot borrow
62 }
63
64 fn deref_mut_field2(mut x: Own<Point>) {
65     let _i = &mut x.y;
66 }
67
68 fn deref_extend_field<'a>(x: &'a Own<Point>) -> &'a int {
69     &x.y
70 }
71
72 fn deref_extend_mut_field1<'a>(x: &'a Own<Point>) -> &'a mut int {
73     &mut x.y //~ ERROR cannot borrow
74 }
75
76 fn deref_extend_mut_field2<'a>(x: &'a mut Own<Point>) -> &'a mut int {
77     &mut x.y
78 }
79
80 fn deref_extend_mut_field3<'a>(x: &'a mut Own<Point>) {
81     // Hmm, this is unfortunate, because with box it would work,
82     // but it's presently the expected outcome. See `deref_extend_mut_field4`
83     // for the workaround.
84
85     let _x = &mut x.x;
86     let _y = &mut x.y; //~ ERROR cannot borrow
87 }
88
89 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
90     let p = &mut **x;
91     let _x = &mut p.x;
92     let _y = &mut p.y;
93 }
94
95 fn assign_field1<'a>(x: Own<Point>) {
96     x.y = 3; //~ ERROR cannot borrow
97 }
98
99 fn assign_field2<'a>(x: &'a Own<Point>) {
100     x.y = 3; //~ ERROR cannot assign
101 }
102
103 fn assign_field3<'a>(x: &'a mut Own<Point>) {
104     x.y = 3;
105 }
106
107 fn assign_field4<'a>(x: &'a mut Own<Point>) {
108     let _p: &mut Point = &mut **x;
109     x.y = 3; //~ ERROR cannot borrow
110 }
111
112 // FIXME(eddyb) #12825 This shouldn't attempt to call deref_mut.
113 /*
114 fn deref_imm_method(x: Own<Point>) {
115     let _i = x.get();
116 }
117 */
118
119 fn deref_mut_method1(x: Own<Point>) {
120     x.set(0, 0); //~ ERROR cannot borrow
121 }
122
123 fn deref_mut_method2(mut x: Own<Point>) {
124     x.set(0, 0);
125 }
126
127 fn deref_extend_method<'a>(x: &'a Own<Point>) -> &'a int {
128     x.x_ref()
129 }
130
131 fn deref_extend_mut_method1<'a>(x: &'a Own<Point>) -> &'a mut int {
132     x.y_mut() //~ ERROR cannot borrow
133 }
134
135 fn deref_extend_mut_method2<'a>(x: &'a mut Own<Point>) -> &'a mut int {
136     x.y_mut()
137 }
138
139 fn assign_method1<'a>(x: Own<Point>) {
140     *x.y_mut() = 3; //~ ERROR cannot borrow
141 }
142
143 fn assign_method2<'a>(x: &'a Own<Point>) {
144     *x.y_mut() = 3; //~ ERROR cannot borrow
145 }
146
147 fn assign_method3<'a>(x: &'a mut Own<Point>) {
148     *x.y_mut() = 3;
149 }
150
151 pub fn main() {}