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<T> for Own<T> {
21 fn deref<'a>(&'a self) -> &'a T {
22 unsafe { &*self.value }
26 impl<T> DerefMut<T> for Own<T> {
27 fn deref_mut<'a>(&'a mut self) -> &'a mut T {
28 unsafe { &mut *self.value }
38 fn get(&self) -> (int, int) {
42 fn set(&mut self, x: int, y: int) {
47 fn x_ref<'a>(&'a self) -> &'a int {
51 fn y_mut<'a>(&'a mut self) -> &'a mut int {
56 fn deref_imm_field(x: Own<Point>) {
60 fn deref_mut_field1(x: Own<Point>) {
61 let _i = &mut x.y; //~ ERROR cannot borrow
64 fn deref_mut_field2(mut x: Own<Point>) {
68 fn deref_extend_field<'a>(x: &'a Own<Point>) -> &'a int {
72 fn deref_extend_mut_field1<'a>(x: &'a Own<Point>) -> &'a mut int {
73 &mut x.y //~ ERROR cannot borrow
76 fn deref_extend_mut_field2<'a>(x: &'a mut Own<Point>) -> &'a mut int {
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.
86 let _y = &mut x.y; //~ ERROR cannot borrow
89 fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
95 fn assign_field1<'a>(x: Own<Point>) {
96 x.y = 3; //~ ERROR cannot borrow
99 fn assign_field2<'a>(x: &'a Own<Point>) {
100 x.y = 3; //~ ERROR cannot assign
103 fn assign_field3<'a>(x: &'a mut Own<Point>) {
107 fn assign_field4<'a>(x: &'a mut Own<Point>) {
108 let _p: &mut Point = &mut **x;
109 x.y = 3; //~ ERROR cannot borrow
112 // FIXME(eddyb) #12825 This shouldn't attempt to call deref_mut.
114 fn deref_imm_method(x: Own<Point>) {
119 fn deref_mut_method1(x: Own<Point>) {
120 x.set(0, 0); //~ ERROR cannot borrow
123 fn deref_mut_method2(mut x: Own<Point>) {
127 fn deref_extend_method<'a>(x: &'a Own<Point>) -> &'a int {
131 fn deref_extend_mut_method1<'a>(x: &'a Own<Point>) -> &'a mut int {
132 x.y_mut() //~ ERROR cannot borrow
135 fn deref_extend_mut_method2<'a>(x: &'a mut Own<Point>) -> &'a mut int {
139 fn assign_method1<'a>(x: Own<Point>) {
140 *x.y_mut() = 3; //~ ERROR cannot borrow
143 fn assign_method2<'a>(x: &'a Own<Point>) {
144 *x.y_mut() = 3; //~ ERROR cannot borrow
147 fn assign_method3<'a>(x: &'a mut Own<Point>) {