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);
This commit is contained in:
P1start 2014-08-10 15:54:33 +12:00
parent 651106462c
commit bf274bc18b
34 changed files with 549 additions and 14 deletions

View file

@ -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
}

View file

@ -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`
}

View file

@ -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
}

View file

@ -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)`
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}