]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-borrow-overloaded-auto-deref.rs
Update compile fail tests to use isize.
[rust.git] / src / test / compile-fail / borrowck-borrow-overloaded-auto-deref.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 only
12 // Deref and not DerefMut is implemented.
13
14 use std::ops::Deref;
15
16 struct Rc<T> {
17     value: *const T
18 }
19
20 impl<T> Deref for Rc<T> {
21     type Target = T;
22
23     fn deref(&self) -> &T {
24         unsafe { &*self.value }
25     }
26 }
27
28 struct Point {
29     x: isize,
30     y: isize
31 }
32
33 impl Point {
34     fn get(&self) -> (isize, isize) {
35         (self.x, self.y)
36     }
37
38     fn set(&mut self, x: isize, y: isize) {
39         self.x = x;
40         self.y = y;
41     }
42
43     fn x_ref(&self) -> &isize {
44         &self.x
45     }
46
47     fn y_mut(&mut self) -> &mut isize {
48         &mut self.y
49     }
50 }
51
52 fn deref_imm_field(x: Rc<Point>) {
53     let _i = &x.y;
54 }
55
56 fn deref_mut_field1(x: Rc<Point>) {
57     let _i = &mut x.y; //~ ERROR cannot borrow
58 }
59
60 fn deref_mut_field2(mut x: Rc<Point>) {
61     let _i = &mut x.y; //~ ERROR cannot borrow
62 }
63
64 fn deref_extend_field(x: &Rc<Point>) -> &isize {
65     &x.y
66 }
67
68 fn deref_extend_mut_field1(x: &Rc<Point>) -> &mut isize {
69     &mut x.y //~ ERROR cannot borrow
70 }
71
72 fn deref_extend_mut_field2(x: &mut Rc<Point>) -> &mut isize {
73     &mut x.y //~ ERROR cannot borrow
74 }
75
76 fn assign_field1<'a>(x: Rc<Point>) {
77     x.y = 3; //~ ERROR cannot assign
78 }
79
80 fn assign_field2<'a>(x: &'a Rc<Point>) {
81     x.y = 3; //~ ERROR cannot assign
82 }
83
84 fn assign_field3<'a>(x: &'a mut Rc<Point>) {
85     x.y = 3; //~ ERROR cannot assign
86 }
87
88 fn deref_imm_method(x: Rc<Point>) {
89     let _i = x.get();
90 }
91
92 fn deref_mut_method1(x: Rc<Point>) {
93     x.set(0, 0); //~ ERROR cannot borrow
94 }
95
96 fn deref_mut_method2(mut x: Rc<Point>) {
97     x.set(0, 0); //~ ERROR cannot borrow
98 }
99
100 fn deref_extend_method(x: &Rc<Point>) -> &isize {
101     x.x_ref()
102 }
103
104 fn deref_extend_mut_method1(x: &Rc<Point>) -> &mut isize {
105     x.y_mut() //~ ERROR cannot borrow
106 }
107
108 fn deref_extend_mut_method2(x: &mut Rc<Point>) -> &mut isize {
109     x.y_mut() //~ ERROR cannot borrow
110 }
111
112 fn assign_method1<'a>(x: Rc<Point>) {
113     *x.y_mut() = 3; //~ ERROR cannot borrow
114 }
115
116 fn assign_method2<'a>(x: &'a Rc<Point>) {
117     *x.y_mut() = 3; //~ ERROR cannot borrow
118 }
119
120 fn assign_method3<'a>(x: &'a mut Rc<Point>) {
121     *x.y_mut() = 3; //~ ERROR cannot borrow
122 }
123
124 pub fn main() {}