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.
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.
11 // Test how overloaded deref interacts with borrows when DerefMut
14 use std::ops::{Deref, DerefMut};
20 impl<T> Deref for Own<T> {
23 fn deref(&self) -> &T {
24 unsafe { &*self.value }
28 impl<T> DerefMut for Own<T> {
29 fn deref_mut(&mut self) -> &mut T {
30 unsafe { &mut *self.value }
40 fn get(&self) -> (isize, isize) {
44 fn set(&mut self, x: isize, y: isize) {
49 fn x_ref(&self) -> &isize {
53 fn y_mut(&mut self) -> &mut isize {
58 fn deref_imm_field(x: Own<Point>) {
62 fn deref_mut_field1(x: Own<Point>) {
63 let _i = &mut x.y; //~ ERROR cannot borrow
66 fn deref_mut_field2(mut x: Own<Point>) {
70 fn deref_extend_field(x: &Own<Point>) -> &isize {
74 fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
75 &mut x.y //~ ERROR cannot borrow
78 fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
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.
88 let _y = &mut x.y; //~ ERROR cannot borrow
91 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
97 fn assign_field1<'a>(x: Own<Point>) {
98 x.y = 3; //~ ERROR cannot borrow
101 fn assign_field2<'a>(x: &'a Own<Point>) {
102 x.y = 3; //~ ERROR cannot assign
105 fn assign_field3<'a>(x: &'a mut Own<Point>) {
109 fn assign_field4<'a>(x: &'a mut Own<Point>) {
110 let _p: &mut Point = &mut **x;
111 x.y = 3; //~ ERROR cannot borrow
114 // FIXME(eddyb) #12825 This shouldn't attempt to call deref_mut.
116 fn deref_imm_method(x: Own<Point>) {
121 fn deref_mut_method1(x: Own<Point>) {
122 x.set(0, 0); //~ ERROR cannot borrow
125 fn deref_mut_method2(mut x: Own<Point>) {
129 fn deref_extend_method(x: &Own<Point>) -> &isize {
133 fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
134 x.y_mut() //~ ERROR cannot borrow
137 fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
141 fn assign_method1<'a>(x: Own<Point>) {
142 *x.y_mut() = 3; //~ ERROR cannot borrow
145 fn assign_method2<'a>(x: &'a Own<Point>) {
146 *x.y_mut() = 3; //~ ERROR cannot borrow
149 fn assign_method3<'a>(x: &'a mut Own<Point>) {