]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-overloaded-index-autoderef.rs
rollup merge of #20092: barosl/rustdoc-line-number-clickable
[rust.git] / src / test / compile-fail / borrowck-overloaded-index-autoderef.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 that we still see borrowck errors of various kinds when using
12 // indexing and autoderef in combination.
13
14 #![feature(associated_types)]
15
16 use std::ops::{Index, IndexMut};
17
18 struct Foo {
19     x: int,
20     y: int,
21 }
22
23 impl Index<String> for Foo {
24     type Output = int;
25
26     fn index<'a>(&'a self, z: &String) -> &'a int {
27         if z.as_slice() == "x" {
28             &self.x
29         } else {
30             &self.y
31         }
32     }
33 }
34
35 impl IndexMut<String> for Foo {
36     type Output = int;
37
38     fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int {
39         if z.as_slice() == "x" {
40             &mut self.x
41         } else {
42             &mut self.y
43         }
44     }
45 }
46
47 fn test1(mut f: Box<Foo>, s: String) {
48     let _p = &mut f[s];
49     let _q = &f[s]; //~ ERROR cannot borrow
50 }
51
52 fn test2(mut f: Box<Foo>, s: String) {
53     let _p = &mut f[s];
54     let _q = &mut f[s]; //~ ERROR cannot borrow
55 }
56
57 struct Bar {
58     foo: Foo
59 }
60
61 fn test3(mut f: Box<Bar>, s: String) {
62     let _p = &mut f.foo[s];
63     let _q = &mut f.foo[s]; //~ ERROR cannot borrow
64 }
65
66 fn test4(mut f: Box<Bar>, s: String) {
67     let _p = &f.foo[s];
68     let _q = &f.foo[s];
69 }
70
71 fn test5(mut f: Box<Bar>, s: String) {
72     let _p = &f.foo[s];
73     let _q = &mut f.foo[s]; //~ ERROR cannot borrow
74 }
75
76 fn test6(mut f: Box<Bar>, g: Foo, s: String) {
77     let _p = &f.foo[s];
78     f.foo = g; //~ ERROR cannot assign
79 }
80
81 fn test7(mut f: Box<Bar>, g: Bar, s: String) {
82     let _p = &f.foo[s];
83     *f = g; //~ ERROR cannot assign
84 }
85
86 fn test8(mut f: Box<Bar>, g: Foo, s: String) {
87     let _p = &mut f.foo[s];
88     f.foo = g; //~ ERROR cannot assign
89 }
90
91 fn test9(mut f: Box<Bar>, g: Bar, s: String) {
92     let _p = &mut f.foo[s];
93     *f = g; //~ ERROR cannot assign
94 }
95
96 fn main() {
97 }
98
99