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 first-class struct and tuple constants #23260

Closed
aochagavia opened this issue Mar 10, 2015 · 5 comments · Fixed by #23275
Closed

Allow first-class struct and tuple constants #23260

aochagavia opened this issue Mar 10, 2015 · 5 comments · Fixed by #23275

Comments

@aochagavia
Copy link
Contributor

The following code should compile, but does not:

struct MyStruct { field: usize }
struct Nested { nested: MyStruct }
struct Mix2 { nested: ((usize,),) }

const STRUCT: MyStruct = MyStruct { field: 42 };
const TUP: (usize,) = (43,);
const NESTED_S: Nested = Nested { nested: MyStruct { field: 5 } };
const NESTED_T: ((usize,),) = ((4,),);
const MIX_1: ((Nested,),) = ((Nested { nested: MyStruct { field: 3 } },),);
const MIX_2: Mix2 = Mix2 { nested: ((2,),) };
const INSTANT_1: usize = (MyStruct { field: 1 }).field;
const INSTANT_2: usize = (0,).0;

fn main() {
    let a = [0; STRUCT.field];
    let b = [0; TUP.0];
    let c = [0; NESTED_S.nested.field];
    let d = [0; (NESTED_T.0).0];
    let e = [0; (MIX_1.0).0.nested.field];
    let f = [0; (MIX_2.nested.0).0];
    let g = [0; INSTANT_1];
    let h = [0; INSTANT_2];
}
@aochagavia
Copy link
Contributor Author

@eddyb Someone told me on IRC that you know how check_const.rs works. I am not really sure about the ConstQualif that structs and tuples should have. Could you give me a hint?

@eddyb
Copy link
Member

eddyb commented Mar 10, 2015

I think all that code is fine, have you checked rustc::middle::const_eval?

@aochagavia
Copy link
Contributor Author

It doesn't compile on the latest nightly (only a and b work, because of a hack I introduced a couple of months ago).

This are the errors:

$ rustc test.rs
test.rs:17:17: 17:38 error: expected constant integer for repeat count, found no
n-constant expression [E0307]
test.rs:17     let c = [0; NESTED_S.nested.field];
                           ^~~~~~~~~~~~~~~~~~~~~
test.rs:18:17: 18:31 error: expected constant integer for repeat count, found no
n-constant expression [E0307]
test.rs:18     let d = [0; (NESTED_T.0).0];
                           ^~~~~~~~~~~~~~
test.rs:19:17: 19:41 error: expected constant integer for repeat count, found no
n-constant expression [E0307]
test.rs:19     let e = [0; (MIX_1.0).0.nested.field];
                           ^~~~~~~~~~~~~~~~~~~~~~~~
test.rs:20:17: 20:35 error: expected constant integer for repeat count, found no
n-constant expression [E0307]
test.rs:20     let f = [0; (MIX_2.nested.0).0];
                           ^~~~~~~~~~~~~~~~~~
test.rs:21:17: 21:26 error: expected constant integer for repeat count, found va
riable [E0307]
test.rs:21     let g = [0; INSTANT_1];
                           ^~~~~~~~~
test.rs:22:17: 22:26 error: expected constant integer for repeat count, found va
riable [E0307]
test.rs:22     let h = [0; INSTANT_2];
                           ^~~~~~~~~
error: aborting due to 6 previous errors

@eddyb
Copy link
Member

eddyb commented Mar 10, 2015

Yes, that is const_eval - it just doesn't know how to do those accesses - check_consts hasn't had a chance to run at this point, you're still in typeck.

@aochagavia
Copy link
Contributor Author

Oops... I actually meant const_eval when I said check_consts. Now I realize I don't need ConstQualif at all... Thanks!

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 a pull request may close this issue.

2 participants