Skip to content

Commit 57cf2de

Browse files
committed
Update borrowck tests to test that index is by-move now
1 parent 8e58af4 commit 57cf2de

3 files changed

+82
-8
lines changed

src/test/compile-fail/borrowck-overloaded-index-2.rs src/test/compile-fail/borrowck-overloaded-index-move-from-vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct MyVec<T> {
1919
impl<T> Index<usize> for MyVec<T> {
2020
type Output = T;
2121

22-
fn index(&self, &i: &usize) -> &T {
22+
fn index(&self, i: usize) -> &T {
2323
&self.data[i]
2424
}
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
use std::ops::{Index, IndexMut};
12+
13+
struct Foo {
14+
x: isize,
15+
y: isize,
16+
}
17+
18+
impl Index<String> for Foo {
19+
type Output = isize;
20+
21+
fn index(&self, z: String) -> &isize {
22+
if z == "x" {
23+
&self.x
24+
} else {
25+
&self.y
26+
}
27+
}
28+
}
29+
30+
impl IndexMut<String> for Foo {
31+
fn index_mut(&mut self, z: String) -> &mut isize {
32+
if z == "x" {
33+
&mut self.x
34+
} else {
35+
&mut self.y
36+
}
37+
}
38+
}
39+
40+
struct Bar {
41+
x: isize,
42+
}
43+
44+
impl Index<isize> for Bar {
45+
type Output = isize;
46+
47+
fn index<'a>(&'a self, z: isize) -> &'a isize {
48+
&self.x
49+
}
50+
}
51+
52+
fn 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;
59+
60+
println!("{}", f[s]);
61+
//~^ ERROR cannot move out of `s` because it is borrowed
62+
63+
f[s] = 10;
64+
//~^ ERROR cannot move out of `s` because it is borrowed
65+
//~| ERROR use of moved value: `s`
66+
67+
let s = Bar {
68+
x: 1,
69+
};
70+
let i = 2;
71+
let _j = &i;
72+
println!("{}", s[i]); // no error, i is copy
73+
println!("{}", s[i]);
74+
}

src/test/compile-fail/borrowck-overloaded-index.rs src/test/compile-fail/borrowck-overloaded-index-ref-index.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ struct Foo {
1515
y: isize,
1616
}
1717

18-
impl Index<String> for Foo {
18+
impl<'a> Index<&'a String> for Foo {
1919
type Output = isize;
2020

21-
fn index<'a>(&'a self, z: &String) -> &'a isize {
21+
fn index(&self, z: &String) -> &isize {
2222
if *z == "x" {
2323
&self.x
2424
} else {
@@ -27,8 +27,8 @@ impl Index<String> for Foo {
2727
}
2828
}
2929

30-
impl IndexMut<String> for Foo {
31-
fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut isize {
30+
impl<'a> IndexMut<&'a String> for Foo {
31+
fn index_mut(&mut self, z: &String) -> &mut isize {
3232
if *z == "x" {
3333
&mut self.x
3434
} else {
@@ -44,7 +44,7 @@ struct Bar {
4444
impl Index<isize> for Bar {
4545
type Output = isize;
4646

47-
fn index<'a>(&'a self, z: &isize) -> &'a isize {
47+
fn index<'a>(&'a self, z: isize) -> &'a isize {
4848
&self.x
4949
}
5050
}
@@ -56,9 +56,9 @@ fn main() {
5656
};
5757
let mut s = "hello".to_string();
5858
let rs = &mut s;
59-
println!("{}", f[s]);
59+
println!("{}", f[&s]);
6060
//~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
61-
f[s] = 10;
61+
f[&s] = 10;
6262
//~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
6363
let s = Bar {
6464
x: 1,

0 commit comments

Comments
 (0)