diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2014-10-31 05:44:10 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2014-11-05 11:29:15 -0500 |
| commit | 0b5bc3314fa9bf768163828a8e38bac588936fe9 (patch) | |
| tree | 5e080bbbd80c8e71da11c4aef39a2170b414c1ba /src/test | |
| parent | 33ef78fa8bbe9b8d05ba0da607d4da5e31475a95 (diff) | |
| download | rust-0b5bc3314fa9bf768163828a8e38bac588936fe9.tar.gz rust-0b5bc3314fa9bf768163828a8e38bac588936fe9.zip | |
Implement new operator dispatch semantics.
Key points are: 1. `a + b` maps directly to `Add<A,B>`, where `A` and `B` are the types of `a` and `b`. 2. Indexing and slicing autoderefs consistently.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/borrowck-overloaded-index-autoderef.rs | 91 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-13482-2.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-15207.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-17033.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-2149.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/slice-mut-2.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/type-params-in-different-spaces-1.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/overloaded-index-assoc-list.rs (renamed from src/test/run-pass/overload-index-operator.rs) | 0 | ||||
| -rw-r--r-- | src/test/run-pass/overloaded-index-autoderef.rs | 79 | ||||
| -rw-r--r-- | src/test/run-pass/overloaded-index-in-field.rs | 52 | ||||
| -rw-r--r-- | src/test/run-pass/overloaded-index.rs | 17 |
11 files changed, 243 insertions, 6 deletions
diff --git a/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs new file mode 100644 index 00000000000..2253d7512c0 --- /dev/null +++ b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs @@ -0,0 +1,91 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we still see borrowck errors of various kinds when using +// indexing and autoderef in combination. + +struct Foo { + x: int, + y: int, +} + +impl Index<String,int> for Foo { + fn index<'a>(&'a self, z: &String) -> &'a int { + if z.as_slice() == "x" { + &self.x + } else { + &self.y + } + } +} + +impl IndexMut<String,int> for Foo { + fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int { + if z.as_slice() == "x" { + &mut self.x + } else { + &mut self.y + } + } +} + +fn test1(mut f: Box<Foo>, s: String) { + let _p = &mut f[s]; + let _q = &f[s]; //~ ERROR cannot borrow +} + +fn test2(mut f: Box<Foo>, s: String) { + let _p = &mut f[s]; + let _q = &mut f[s]; //~ ERROR cannot borrow +} + +struct Bar { + foo: Foo +} + +fn test3(mut f: Box<Bar>, s: String) { + let _p = &mut f.foo[s]; + let _q = &mut f.foo[s]; //~ ERROR cannot borrow +} + +fn test4(mut f: Box<Bar>, s: String) { + let _p = &f.foo[s]; + let _q = &f.foo[s]; +} + +fn test5(mut f: Box<Bar>, s: String) { + let _p = &f.foo[s]; + let _q = &mut f.foo[s]; //~ ERROR cannot borrow +} + +fn test6(mut f: Box<Bar>, g: Foo, s: String) { + let _p = &f.foo[s]; + f.foo = g; //~ ERROR cannot assign +} + +fn test7(mut f: Box<Bar>, g: Bar, s: String) { + let _p = &f.foo[s]; + *f = g; //~ ERROR cannot assign +} + +fn test8(mut f: Box<Bar>, g: Foo, s: String) { + let _p = &mut f.foo[s]; + f.foo = g; //~ ERROR cannot assign +} + +fn test9(mut f: Box<Bar>, g: Bar, s: String) { + let _p = &mut f.foo[s]; + *f = g; //~ ERROR cannot assign +} + +fn main() { +} + + diff --git a/src/test/compile-fail/issue-13482-2.rs b/src/test/compile-fail/issue-13482-2.rs index 6746b90e32d..4ec8c2b1b7e 100644 --- a/src/test/compile-fail/issue-13482-2.rs +++ b/src/test/compile-fail/issue-13482-2.rs @@ -14,7 +14,7 @@ fn main() { let x = [1,2]; let y = match x { [] => None, - //~^ ERROR types: expected `[_#0i, ..2]`, found `[_#7, ..0]` + //~^ ERROR types: expected `[_#0i, ..2]`, found `[_#7t, ..0]` // (expected array of 2 elements, found array of 0 elements) [a,_] => Some(a) }; diff --git a/src/test/compile-fail/issue-15207.rs b/src/test/compile-fail/issue-15207.rs index 37fcabef0ed..61877775269 100644 --- a/src/test/compile-fail/issue-15207.rs +++ b/src/test/compile-fail/issue-15207.rs @@ -11,7 +11,6 @@ fn main() { loop { break.push(1) //~ ERROR the type of this value must be known in this context - //~^ ERROR multiple applicable methods in scope ; } } diff --git a/src/test/compile-fail/issue-17033.rs b/src/test/compile-fail/issue-17033.rs index 7590546d40a..5048a9aa919 100644 --- a/src/test/compile-fail/issue-17033.rs +++ b/src/test/compile-fail/issue-17033.rs @@ -11,7 +11,7 @@ #![feature(overloaded_calls)] fn f<'r>(p: &'r mut fn(p: &mut ())) { - p(()) //~ ERROR mismatched types: expected `&mut ()`, found `()` + (*p)(()) //~ ERROR mismatched types: expected `&mut ()`, found `()` } fn main() {} diff --git a/src/test/compile-fail/issue-2149.rs b/src/test/compile-fail/issue-2149.rs index e64d674b7c8..1150f40db76 100644 --- a/src/test/compile-fail/issue-2149.rs +++ b/src/test/compile-fail/issue-2149.rs @@ -18,7 +18,6 @@ impl<A> vec_monad<A> for Vec<A> { let mut r = panic!(); for elt in self.iter() { r = r + f(*elt); } //~^ ERROR the type of this value must be known - //~^^ ERROR not implemented } } fn main() { diff --git a/src/test/compile-fail/slice-mut-2.rs b/src/test/compile-fail/slice-mut-2.rs index 09019448a67..6778ed88ff7 100644 --- a/src/test/compile-fail/slice-mut-2.rs +++ b/src/test/compile-fail/slice-mut-2.rs @@ -15,5 +15,5 @@ fn main() { let x: &[int] = &[1, 2, 3, 4, 5]; // Can't mutably slice an immutable slice - let y = x[mut 2..4]; //~ ERROR cannot take a mutable slice of a value with type `&[int]` + let y = x[mut 2..4]; //~ ERROR cannot borrow } diff --git a/src/test/compile-fail/type-params-in-different-spaces-1.rs b/src/test/compile-fail/type-params-in-different-spaces-1.rs index 6e32e6e4835..c87e8541758 100644 --- a/src/test/compile-fail/type-params-in-different-spaces-1.rs +++ b/src/test/compile-fail/type-params-in-different-spaces-1.rs @@ -12,7 +12,7 @@ use std::num::Num; trait BrokenAdd: Num { fn broken_add<T>(&self, rhs: T) -> Self { - *self + rhs //~ ERROR mismatched types + *self + rhs //~ ERROR expected `Self`, found `T` } } diff --git a/src/test/run-pass/overload-index-operator.rs b/src/test/run-pass/overloaded-index-assoc-list.rs index 7c6ad45a9ef..7c6ad45a9ef 100644 --- a/src/test/run-pass/overload-index-operator.rs +++ b/src/test/run-pass/overloaded-index-assoc-list.rs diff --git a/src/test/run-pass/overloaded-index-autoderef.rs b/src/test/run-pass/overloaded-index-autoderef.rs new file mode 100644 index 00000000000..d51956da894 --- /dev/null +++ b/src/test/run-pass/overloaded-index-autoderef.rs @@ -0,0 +1,79 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test overloaded indexing combined with autoderef. + +struct Foo { + x: int, + y: int, +} + +impl Index<int,int> for Foo { + fn index(&self, z: &int) -> &int { + if *z == 0 { + &self.x + } else { + &self.y + } + } +} + +impl IndexMut<int,int> for Foo { + fn index_mut(&mut self, z: &int) -> &mut int { + if *z == 0 { + &mut self.x + } else { + &mut self.y + } + } +} + +trait Int { + fn get(self) -> int; + fn get_from_ref(&self) -> int; + fn inc(&mut self); +} + +impl Int for int { + fn get(self) -> int { self } + fn get_from_ref(&self) -> int { *self } + fn inc(&mut self) { *self += 1; } +} + +fn main() { + let mut f = box Foo { + x: 1, + y: 2, + }; + + assert_eq!(f[1], 2); + + f[0] = 3; + + assert_eq!(f[0], 3); + + // Test explicit IndexMut where `f` must be autoderef: + { + let p = &mut f[1]; + *p = 4; + } + + // Test explicit Index where `f` must be autoderef: + { + let p = &f[1]; + assert_eq!(*p, 4); + } + + // Test calling methods with `&mut self`, `self, and `&self` receivers: + f[1].inc(); + assert_eq!(f[1].get(), 5); + assert_eq!(f[1].get_from_ref(), 5); +} + diff --git a/src/test/run-pass/overloaded-index-in-field.rs b/src/test/run-pass/overloaded-index-in-field.rs new file mode 100644 index 00000000000..e8b0408ca0d --- /dev/null +++ b/src/test/run-pass/overloaded-index-in-field.rs @@ -0,0 +1,52 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test using overloaded indexing when the "map" is stored in a +// field. This caused problems at some point. + +struct Foo { + x: int, + y: int, +} + +struct Bar { + foo: Foo +} + +impl Index<int,int> for Foo { + fn index(&self, z: &int) -> &int { + if *z == 0 { + &self.x + } else { + &self.y + } + } +} + +trait Int { + fn get(self) -> int; + fn get_from_ref(&self) -> int; + fn inc(&mut self); +} + +impl Int for int { + fn get(self) -> int { self } + fn get_from_ref(&self) -> int { *self } + fn inc(&mut self) { *self += 1; } +} + +fn main() { + let f = Bar { foo: Foo { + x: 1, + y: 2, + } }; + assert_eq!(f.foo[1].get(), 2); +} + diff --git a/src/test/run-pass/overloaded-index.rs b/src/test/run-pass/overloaded-index.rs index 1187e066950..23bebfa35d7 100644 --- a/src/test/run-pass/overloaded-index.rs +++ b/src/test/run-pass/overloaded-index.rs @@ -33,6 +33,18 @@ impl IndexMut<int,int> for Foo { } } +trait Int { + fn get(self) -> int; + fn get_from_ref(&self) -> int; + fn inc(&mut self); +} + +impl Int for int { + fn get(self) -> int { self } + fn get_from_ref(&self) -> int { *self } + fn inc(&mut self) { *self += 1; } +} + fn main() { let mut f = Foo { x: 1, @@ -49,5 +61,10 @@ fn main() { let p = &f[1]; assert_eq!(*p, 4); } + + // Test calling methods with `&mut self`, `self, and `&self` receivers: + f[1].inc(); + assert_eq!(f[1].get(), 5); + assert_eq!(f[1].get_from_ref(), 5); } |
