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