]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-borrow-overloaded-deref.rs
7aac9458e3c9ad58df0184115b7c09474414f733
[rust.git] / src / test / compile-fail / borrowck-borrow-overloaded-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: *T
18 }
19
20 impl<T> Deref<T> for Rc<T> {
21     fn deref<'a>(&'a self) -> &'a T {
22         unsafe { &*self.value }
23     }
24 }
25
26 fn deref_imm(x: Rc<int>) {
27     let _i = &*x;
28 }
29
30 fn deref_mut1(x: Rc<int>) {
31     let _i = &mut *x; //~ ERROR cannot borrow
32 }
33
34 fn deref_mut2(mut x: Rc<int>) {
35     let _i = &mut *x; //~ ERROR cannot borrow
36 }
37
38 fn deref_extend<'a>(x: &'a Rc<int>) -> &'a int {
39     &**x
40 }
41
42 fn deref_extend_mut1<'a>(x: &'a Rc<int>) -> &'a mut int {
43     &mut **x //~ ERROR cannot borrow
44 }
45
46 fn deref_extend_mut2<'a>(x: &'a mut Rc<int>) -> &'a mut int {
47     &mut **x //~ ERROR cannot borrow
48 }
49
50 fn assign1<'a>(x: Rc<int>) {
51     *x = 3; //~ ERROR cannot assign
52 }
53
54 fn assign2<'a>(x: &'a Rc<int>) {
55     **x = 3; //~ ERROR cannot assign
56 }
57
58 fn assign3<'a>(x: &'a mut Rc<int>) {
59     **x = 3; //~ ERROR cannot assign
60 }
61
62 pub fn main() {}