-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
rustc's ty::layout refers to tagged unions' tag as "discr(iminant)" incorrectly. #49938
Comments
👍 |
I've been busy teaching myself how Rust I've been struggling to find a good definition of tagged unions and how they relate to |
Mostly. This is how an enum is represented that has the tagged layout. https://github.com/rust-lang/rust/blob/master/src/librustc/ty/layout.rs#L820 So |
@samWson Wikipedia has a page on them, although they focus on the high-level concept (like Rust's See the C/C++ example under https://en.wikipedia.org/wiki/Tagged_union#1970s_&_1980s for how you might "manually" implement a tagged union in a language without the higher-level concept. |
@oli-obk Going by the original issue, is the enum quoted above an example of the code that needs to be changed to use the correct terminology? For example, the code at the quoted link will become: /// General-case enums: for each case there is a struct, and they all have
/// all space reserved for the tag, and their first field starts
/// at a non-0 offset, after where the tag would go.
Tagged {
tag: Scalar,
variants: Vec<LayoutDetails>,
}, Likewise, any uses of type Is this cleanup of terminology what the issue is trying to solve? |
Yes, that's exactly the cleanup meant here |
Great. I'll get started on the cleanup then. |
Is there any code outside of |
@samWson try searching for EDIT: as for tests, there shouldn't be any changes, as long as you're just doing renamings. |
Pull request is passing CI. Please review and let me know if there are more changes required. #50309 |
Refer rust-lang#49938 Previously tagged unions' tag was refered to as a discr(iminant). Here the changes use tag instead which is the correct terminology when refering to the memory representation of tagged unions.
The current terminology is that the (optionally explicitly specified) numerical values associated with
enum
variants are called "discriminants", while the representation may use "tags".By default (that is, unless e.g.
#[repr(iN)]
/#[repr(uN)]
is specified), discriminants have theisize
type and tags usually take the smallest signed/unsigned primitive integer that fits all the values.All types (not just
enum
s) have a "discriminant", which is always0u8
for all non-enum
types.For
enum
s, the discriminant is computed from the memory representation, which specifically for unoptimized tagged unions is done by casting the tag to the discriminant type.In the general case, the original discriminant values do not need to be used in-memory.
However, some of the older code in the compiler mixes up the two terms and can cause confusion.
Ideally, for
enum
s, we would use "discr(iminant)" for the value associated with each variant, and "tag" for its encoding in the memory representation of tagged unions.cc @oli-obk @nikomatsakis
The text was updated successfully, but these errors were encountered: