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

Allow const struct fields and tuple indexing #19266

Merged
merged 2 commits into from
Nov 27, 2014
Merged

Allow const struct fields and tuple indexing #19266

merged 2 commits into from
Nov 27, 2014

Conversation

aochagavia
Copy link
Contributor

With this PR, the following code works:

#![feature(tuple_indexing)]
struct MyStruct { field1: uint }

const S: MyStruct = MyStruct { field1: 42u };
const T: (uint,) = (42u,);

struct ConstCheck {
    array1: [int, ..S.field1],
    array2: [int, ..T.0],
}

Closes #19244
Related #19265

@aochagavia
Copy link
Contributor Author

@huonw The changes I have made so far (only for fields, not tuple indices) didn't work. I have no clue how to fix it. Could you (or anybody) point me in the right direction?

EDIT: I have figured it out and it works now :)

@aochagavia aochagavia changed the title WIP: Allow const struct fields and tuple indexing Allow const struct fields and tuple indexing Nov 24, 2014
@aochagavia
Copy link
Contributor Author

This is ready! However, error reporting within const_eval is quite limited. We should consider to improve it in order to give more useful messages (especially regarding the span).

Example:

struct MyStruct { field1: uint }

const S: MyStruct = MyStruct { field1: 42u };

struct ConstCheck {
    broken_1: [int, ..S.field2],
            //~^ ERROR: expected constant expr for array length: unexistent struct field
}

I think I'll leave this for another PR

@aochagavia
Copy link
Contributor Author

r? @alexcrichton

@alexcrichton
Copy link
Member

Can you some run-pass test which confirm that this works, as well as a compile-fail test for each failure mode?

@aochagavia
Copy link
Contributor Author

@alexcrichton Is it ok now?

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Nov 27, 2014
With this PR, the following code works:

```
#![feature(tuple_indexing)]
struct MyStruct { field1: uint }

const S: MyStruct = MyStruct { field1: 42u };
const T: (uint,) = (42u,);

struct ConstCheck {
    array1: [int, ..S.field1],
    array2: [int, ..T.0],
}
```

Closes rust-lang#19244
Related rust-lang#19265
@bors bors merged commit 080e625 into rust-lang:master Nov 27, 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.

Constancy broken by tuple indexing (newtypes may unwrap in the runtime)
3 participants