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