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

Docs somewhat unclear regarding C-like enum discriminators #17770

Closed
voithos opened this issue Oct 4, 2014 · 4 comments
Closed

Docs somewhat unclear regarding C-like enum discriminators #17770

voithos opened this issue Oct 4, 2014 · 4 comments

Comments

@voithos
Copy link

voithos commented Oct 4, 2014

The reference states pretty clearly that the memory layout of structs is undefined by default, and presumably the same thing can be said of complex enum variants.

However, for C-like enums (simple enums which correspond to some integer type), it doesn't seem to be clear whether implicit discriminators are well defined. One resource (no longer in the actual docs, it seems) says that they are well defined (starts with 0, increases by 1 for each variant). But I couldn't find any other docs that comment on this (the reference does state that an enum with #[repr(C)] will have the default C ABI enum size - but also that enum representation in C is undefined? Is that referring to the implicit discriminators?).

In essence, can we assume that implicit discriminators start with 0 and increase by 1 per variant, or is this never an assumption we can make?

@mahkoh
Copy link
Contributor

mahkoh commented Oct 4, 2014

the reference does state that an enum with #[repr(C)] will have the default C ABI enum size - but also that enum representation in C is undefined? Is that referring to the implicit discriminators?

No, in C they are defined to start with 0 by default. Saying that the representation is undefined in C is also wrong because every implementation defines the representation. Enums in Rust also have to follow the C behavior to make FFI less error prone.

@ghost ghost added the A-docs label Oct 4, 2014
@voithos
Copy link
Author

voithos commented Oct 4, 2014

Saying that the representation is undefined in C is also wrong because every implementation defines the representation.

So, would it be correct to say that enum representation in C is "implementation-dependent"?

Enums in Rust also have to follow the C behavior to make FFI less error prone.

So Rust's C-like enums are guaranteed to start with 0. And this is true regardless of what repr is set to, correct? If so, I could try adjusting the docs to better reflect this, and submit a PR.

@steveklabnik
Copy link
Member

Because I suck at reading specs, Twitter helped: https://twitter.com/jckarter/status/554769119438848000

and that's true:

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, 108) but shall be capable of representing the values of all the members of the enumeration.

Also:

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.

and

An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no = , the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.)

So yeah. We should have this in the docs.

@steveklabnik
Copy link
Member

We now have this text, search for "Enums have a discriminant. ".

lnicola pushed a commit to lnicola/rust that referenced this issue Aug 13, 2024
internal: Remove AbsPathBuf::TryFrom impl that checks too many things at once

rust-lang/rust-analyzer#16889 (comment)
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

No branches or pull requests

3 participants