]>
git.proxmox.com Git - rustc.git/blob - 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.
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 __isize
= &mut x
.y
; //~ ERROR cannot borrow
66 fn deref_mut_field2(mut x
: Own
<Point
>) {
67 let __isize
= &mut x
.y
;
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 fn deref_imm_method(x
: Own
<Point
>) {
115 let __isize
= x
.get();
118 fn deref_mut_method1(x
: Own
<Point
>) {
119 x
.set(0, 0); //~ ERROR cannot borrow
122 fn deref_mut_method2(mut x
: Own
<Point
>) {
126 fn deref_extend_method(x
: &Own
<Point
>) -> &isize {
130 fn deref_extend_mut_method1(x
: &Own
<Point
>) -> &mut isize {
131 x
.y_mut() //~ ERROR cannot borrow
134 fn deref_extend_mut_method2(x
: &mut Own
<Point
>) -> &mut isize {
138 fn assign_method1
<'a
>(x
: Own
<Point
>) {
139 *x
.y_mut() = 3; //~ ERROR cannot borrow
142 fn assign_method2
<'a
>(x
: &'a Own
<Point
>) {
143 *x
.y_mut() = 3; //~ ERROR cannot borrow
146 fn assign_method3
<'a
>(x
: &'a
mut Own
<Point
>) {