]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/borrowck-overloaded-index-ref-index.rs
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / test / compile-fail / borrowck-overloaded-index-ref-index.rs
CommitLineData
1a4d82fc
JJ
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
11use std::ops::{Index, IndexMut};
12
13struct Foo {
14 x: isize,
15 y: isize,
16}
17
c34b1796 18impl<'a> Index<&'a String> for Foo {
1a4d82fc
JJ
19 type Output = isize;
20
c34b1796 21 fn index(&self, z: &String) -> &isize {
85aaf69f 22 if *z == "x" {
1a4d82fc
JJ
23 &self.x
24 } else {
25 &self.y
26 }
27 }
28}
29
c34b1796
AL
30impl<'a> IndexMut<&'a String> for Foo {
31 fn index_mut(&mut self, z: &String) -> &mut isize {
85aaf69f 32 if *z == "x" {
1a4d82fc
JJ
33 &mut self.x
34 } else {
35 &mut self.y
36 }
37 }
38}
39
40struct Bar {
41 x: isize,
42}
43
44impl Index<isize> for Bar {
45 type Output = isize;
46
c34b1796 47 fn index<'a>(&'a self, z: isize) -> &'a isize {
1a4d82fc
JJ
48 &self.x
49 }
50}
51
52fn main() {
53 let mut f = Foo {
54 x: 1,
55 y: 2,
56 };
57 let mut s = "hello".to_string();
58 let rs = &mut s;
c34b1796 59 println!("{}", f[&s]);
1a4d82fc 60 //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
c34b1796 61 f[&s] = 10;
1a4d82fc
JJ
62 //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
63 let s = Bar {
64 x: 1,
65 };
66 s[2] = 20;
67 //~^ ERROR cannot assign to immutable indexed content
68}