Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tuple and tuple struct indexing (RFC 53) #16866

Merged
merged 1 commit into from
Sep 11, 2014

Conversation

ftxqxd
Copy link
Contributor

@ftxqxd ftxqxd commented Aug 30, 2014

This allows code to access the fields of tuples and tuple structs behind the feature gate tuple_indexing:

#![feature(tuple_indexing)]

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

Implements RFC 53. Closes #16950.

@bluss
Copy link
Member

bluss commented Aug 30, 2014

Awesome, you're an implementastic whirlwind! I'm not a reviewer, but I guess some one will ask how this change deals with nested tuples and the syntax t.2.1 (it was supposed to be rejected if it looks like a float).

@ftxqxd ftxqxd changed the title [Waiting on RFC] Implement tuple and tuple struct indexing Implement tuple and tuple struct indexing (RFC 53) Sep 3, 2014
@@ -702,6 +705,38 @@ fn trans_rec_field<'a>(bcx: &'a Block<'a>,
})
}

fn trans_rec_tup_field<'a>(bcx: &'a Block<'a>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this differ to trans_rec_field in any significant way; I wonder if it would be possible to deduplicate the shared code.

@huonw
Copy link
Member

huonw commented Sep 5, 2014

I would like to see many more tests, e.g.:

  • moving out: check that let x = (box 1,); let y = x.0; let z = x.0; correctly fails to compile
  • fat types: check let x: &[int] = &[1, 2, 3]; let z = (x,); z.0 == x; just generally does the right thing/doesn't crash
  • borrows:
    • check that let x = (box 1, 2); let r = &x.0; let y = x; fails to compile
    • check that let x = (1, 2); let a = &x.0; let b = &x.0; works
    • check that let mut x = (1, 2); let a = &x.0; let b = &mut x.0; and ... let a = &mut x.0; ... fails
    • check that let mut x = (1, 2); let a = &x.0; let b = &mut x.1; works

@ftxqxd
Copy link
Contributor Author

ftxqxd commented Sep 5, 2014

I’ve added the tests you suggested, and they all pass. I’ve also removed the code duplication in librustc/middle/trans/expr.rs. I’m not sure about renaming ExprTupField—I suppose that ExprNumField is slightly better at suggesting that it works with tuple structs, but the word ‘tuple’ is in the name ‘tuple struct’ anyway. I’m not really sure it matters, anyway—it’s only inside the compiler.

@ftxqxd ftxqxd force-pushed the tuple-indexing branch 3 times, most recently from f725e6c to 1fb5609 Compare September 9, 2014 05:44
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);
@ftxqxd
Copy link
Contributor Author

ftxqxd commented Sep 9, 2014

Rebased. r?

bors added a commit that referenced this pull request Sep 11, 2014
This allows code to access the fields of tuples and tuple structs behind the feature gate `tuple_indexing`:

```rust
#![feature(tuple_indexing)]

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);
```

Implements [RFC 53](https://github.com/rust-lang/rfcs/blob/master/active/0053-tuple-accessors.md). Closes #16950.
@bors bors closed this Sep 11, 2014
@bors bors merged commit bf274bc into rust-lang:master Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement tuple indexing
5 participants