1 // Test how overloaded deref interacts with borrows when DerefMut
4 use std::ops::{Deref, DerefMut};
10 impl<T> Deref for Own<T> {
13 fn deref(&self) -> &T {
14 unsafe { &*self.value }
18 impl<T> DerefMut for Own<T> {
19 fn deref_mut(&mut self) -> &mut T {
20 unsafe { &mut *self.value }
30 fn get(&self) -> (isize, isize) {
34 fn set(&mut self, x: isize, y: isize) {
39 fn x_ref(&self) -> &isize {
43 fn y_mut(&mut self) -> &mut isize {
48 fn deref_imm_field(x: Own<Point>) {
52 fn deref_mut_field1(x: Own<Point>) {
53 let __isize = &mut x.y; //~ ERROR cannot borrow
56 fn deref_mut_field2(mut x: Own<Point>) {
57 let __isize = &mut x.y;
60 fn deref_extend_field(x: &Own<Point>) -> &isize {
64 fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
65 &mut x.y //~ ERROR cannot borrow
68 fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
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.
78 let _y = &mut x.y; //~ ERROR cannot borrow
81 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
87 fn assign_field1<'a>(x: Own<Point>) {
88 x.y = 3; //~ ERROR cannot borrow
91 fn assign_field2<'a>(x: &'a Own<Point>) {
92 x.y = 3; //~ ERROR cannot borrow
95 fn assign_field3<'a>(x: &'a mut Own<Point>) {
99 fn assign_field4<'a>(x: &'a mut Own<Point>) {
100 let _p: &mut Point = &mut **x;
101 x.y = 3; //~ ERROR cannot borrow
104 fn deref_imm_method(x: Own<Point>) {
105 let __isize = x.get();
108 fn deref_mut_method1(x: Own<Point>) {
109 x.set(0, 0); //~ ERROR cannot borrow
112 fn deref_mut_method2(mut x: Own<Point>) {
116 fn deref_extend_method(x: &Own<Point>) -> &isize {
120 fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
121 x.y_mut() //~ ERROR cannot borrow
124 fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
128 fn assign_method1<'a>(x: Own<Point>) {
129 *x.y_mut() = 3; //~ ERROR cannot borrow
132 fn assign_method2<'a>(x: &'a Own<Point>) {
133 *x.y_mut() = 3; //~ ERROR cannot borrow
136 fn assign_method3<'a>(x: &'a mut Own<Point>) {
142 fn use_mut<T>(_: &mut T) {}