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

[repr(C)] enum only accepts int constants #15270

Closed
mahkoh opened this issue Jun 30, 2014 · 6 comments
Closed

[repr(C)] enum only accepts int constants #15270

mahkoh opened this issue Jun 30, 2014 · 6 comments
Labels
A-ffi Area: Foreign Function Interface (FFI)

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Jun 30, 2014

static X: i32 = 1;
static Y: i32 = 2;

#[repr(C)]
enum Foo {
    Bar = X,
    Baz = Y,
}

This worked in 1e6b699 but not in a490871.

cc @huonw

@mahkoh
Copy link
Contributor Author

mahkoh commented Jun 30, 2014

Looks like a5bb0a3 .

cc @pcwalton

@chris-morgan
Copy link
Member

The solution is #[repr(i32)].

It is bad that this was not marked as a breaking change. #15234 does list a breaking change, but not this one.

@huonw
Copy link
Member

huonw commented Jun 30, 2014

@chris-morgan that is not the real solution, since repr(i32) is not guaranteed to match repr(C) everywhere. (On that note, repr(C) defaulting to int isn't great either, since Rust's int doesn't match C's int.)

@mahkoh
Copy link
Contributor Author

mahkoh commented Jun 30, 2014

@chris-morgan: bindgen creates c_uint (= u32 on my system) statics for enums with no negative entries and c_int (= i32 on my system) for enums with negative entries. So you can't just use i32 always.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 23, 2014

Some more problems with int:

You can have enums with u64::MAX variants in C which will have a size of 8.

This is complicated to get right in Rust because u64::MAX will overflow and give you a -1 variant on x86_64. On x86 it's outright impossible to get a repr(C) enum of size 8.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 23, 2014

The constants should simply be untyped like the const RFC suggested.

@bstrie bstrie added the A-ffi Area: Foreign Function Interface (FFI) label Nov 6, 2014
@mahkoh mahkoh closed this as completed Apr 11, 2015
@rust-lang rust-lang locked and limited conversation to collaborators Apr 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-ffi Area: Foreign Function Interface (FFI)
Projects
None yet
Development

No branches or pull requests

4 participants