diff options
| author | P1start <rewi-github@whanau.org> | 2014-08-10 15:54:33 +1200 |
|---|---|---|
| committer | P1start <rewi-github@whanau.org> | 2014-09-10 10:25:12 +1200 |
| commit | bf274bc18bcbfea1377c5c64ae0cc099b03d9beb (patch) | |
| tree | 78f4b0455b6c93991836bed81b7b57096737b462 /src/test | |
| parent | 651106462c357b71a4ca2c02ba2bfedfc38b0035 (diff) | |
| download | rust-bf274bc18bcbfea1377c5c64ae0cc099b03d9beb.tar.gz rust-bf274bc18bcbfea1377c5c64ae0cc099b03d9beb.zip | |
Implement tuple and tuple struct indexing
This allows code to access the fields of tuples and tuple structs:
let x = (1i, 2i);
assert_eq!(x.1, 2);
struct Point(int, int);
let origin = Point(0, 0);
assert_eq!(origin.0, 0);
assert_eq!(origin.1, 0);
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/borrow-tuple-fields.rs | 42 | ||||
| -rw-r--r-- | src/test/compile-fail/move-out-of-tuple-field.rs | 23 | ||||
| -rw-r--r-- | src/test/compile-fail/tuple-index-not-tuple.rs | 22 | ||||
| -rw-r--r-- | src/test/compile-fail/tuple-index-out-of-bounds.rs | 26 | ||||
| -rw-r--r-- | src/test/run-pass/borrow-tuple-fields.rs | 48 | ||||
| -rw-r--r-- | src/test/run-pass/tuple-index-fat-types.rs | 23 | ||||
| -rw-r--r-- | src/test/run-pass/tuple-index.rs | 42 |
7 files changed, 226 insertions, 0 deletions
diff --git a/src/test/compile-fail/borrow-tuple-fields.rs b/src/test/compile-fail/borrow-tuple-fields.rs new file mode 100644 index 00000000000..519bad4e627 --- /dev/null +++ b/src/test/compile-fail/borrow-tuple-fields.rs @@ -0,0 +1,42 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Foo(Box<int>, int); + +struct Bar(int, int); + +fn main() { + let x = (box 1i, 2i); + let r = &x.0; + let y = x; //~ ERROR cannot move out of `x` because it is borrowed + + let mut x = (1i, 2i); + let a = &x.0; + let b = &mut x.0; //~ ERROR cannot borrow `x.0` as mutable because it is also borrowed as + + let mut x = (1i, 2i); + let a = &mut x.0; + let b = &mut x.0; //~ ERROR cannot borrow `x.0` as mutable more than once at a time + + + let x = Foo(box 1i, 2i); + let r = &x.0; + let y = x; //~ ERROR cannot move out of `x` because it is borrowed + + let mut x = Bar(1i, 2i); + let a = &x.0; + let b = &mut x.0; //~ ERROR cannot borrow `x.0` as mutable because it is also borrowed as + + let mut x = Bar(1i, 2i); + let a = &mut x.0; + let b = &mut x.0; //~ ERROR cannot borrow `x.0` as mutable more than once at a time +} diff --git a/src/test/compile-fail/move-out-of-tuple-field.rs b/src/test/compile-fail/move-out-of-tuple-field.rs new file mode 100644 index 00000000000..7f55a78e8b7 --- /dev/null +++ b/src/test/compile-fail/move-out-of-tuple-field.rs @@ -0,0 +1,23 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Foo(Box<int>); + +fn main() { + let x = (box 1i,); + let y = x.0; + let z = x.0; //~ ERROR use of moved value: `x.0` + + let x = Foo(box 1i); + let y = x.0; + let z = x.0; //~ ERROR use of moved value: `x.0` +} diff --git a/src/test/compile-fail/tuple-index-not-tuple.rs b/src/test/compile-fail/tuple-index-not-tuple.rs new file mode 100644 index 00000000000..d4ef0e20b26 --- /dev/null +++ b/src/test/compile-fail/tuple-index-not-tuple.rs @@ -0,0 +1,22 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Point { x: int, y: int } +struct Empty; + +fn main() { + let origin = Point { x: 0, y: 0 }; + origin.0; + //~^ ERROR attempted tuple index `0` on type `Point`, but the type was not + Empty.0; + //~^ ERROR attempted tuple index `0` on type `Empty`, but the type was not +} diff --git a/src/test/compile-fail/tuple-index-out-of-bounds.rs b/src/test/compile-fail/tuple-index-out-of-bounds.rs new file mode 100644 index 00000000000..d16f950a5d4 --- /dev/null +++ b/src/test/compile-fail/tuple-index-out-of-bounds.rs @@ -0,0 +1,26 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Point(int, int); + +fn main() { + let origin = Point(0, 0); + origin.0; + origin.1; + origin.2; + //~^ ERROR attempted out-of-bounds tuple index `2` on type `Point` + let tuple = (0i, 0i); + tuple.0; + tuple.1; + tuple.2; + //~^ ERROR attempted out-of-bounds tuple index `2` on type `(int,int)` +} diff --git a/src/test/run-pass/borrow-tuple-fields.rs b/src/test/run-pass/borrow-tuple-fields.rs new file mode 100644 index 00000000000..046d76c4331 --- /dev/null +++ b/src/test/run-pass/borrow-tuple-fields.rs @@ -0,0 +1,48 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Foo(int, int); + +fn main() { + let x = (1i, 2i); + let a = &x.0; + let b = &x.0; + assert_eq!(*a, 1); + assert_eq!(*b, 1); + + let mut x = (1i, 2i); + { + let a = &x.0; + let b = &mut x.1; + *b = 5; + assert_eq!(*a, 1); + } + assert_eq!(x.0, 1); + assert_eq!(x.1, 5); + + + let x = Foo(1i, 2i); + let a = &x.0; + let b = &x.0; + assert_eq!(*a, 1); + assert_eq!(*b, 1); + + let mut x = Foo(1i, 2i); + { + let a = &x.0; + let b = &mut x.1; + *b = 5; + assert_eq!(*a, 1); + } + assert_eq!(x.0, 1); + assert_eq!(x.1, 5); +} diff --git a/src/test/run-pass/tuple-index-fat-types.rs b/src/test/run-pass/tuple-index-fat-types.rs new file mode 100644 index 00000000000..fdee1d9f96c --- /dev/null +++ b/src/test/run-pass/tuple-index-fat-types.rs @@ -0,0 +1,23 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Foo<'a>(&'a [int]); + +fn main() { + let x: &[int] = &[1i, 2, 3]; + let y = (x,); + assert_eq!(y.0, x); + + let x: &[int] = &[1i, 2, 3]; + let y = Foo(x); + assert_eq!(y.0, x); +} diff --git a/src/test/run-pass/tuple-index.rs b/src/test/run-pass/tuple-index.rs new file mode 100644 index 00000000000..107dc40e186 --- /dev/null +++ b/src/test/run-pass/tuple-index.rs @@ -0,0 +1,42 @@ +// 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. + +#![feature(tuple_indexing)] + +struct Point(int, int); + +fn main() { + let mut x = Point(3, 2); + assert_eq!(x.0, 3); + assert_eq!(x.1, 2); + x.0 += 5; + assert_eq!(x.0, 8); + { + let ry = &mut x.1; + *ry -= 2; + x.0 += 3; + assert_eq!(x.0, 11); + } + assert_eq!(x.1, 0); + + let mut x = (3i, 2i); + assert_eq!(x.0, 3); + assert_eq!(x.1, 2); + x.0 += 5; + assert_eq!(x.0, 8); + { + let ry = &mut x.1; + *ry -= 2; + x.0 += 3; + assert_eq!(x.0, 11); + } + assert_eq!(x.1, 0); + +} |
