-
Notifications
You must be signed in to change notification settings - Fork 1.6k
consider changing size of empty enum to usize::MAX #1076
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
Comments
(another option that may be more palatable for various reasons would be to give empty enums zero size rather than |
As argued (not by me) in this thread the logically sensible size for uninhabited types would actually be negative infinity, because this makes the sizes of product and sum types with uninhabited types as members work out properly. Of course, this doesn't help much in practice because the type of sizes of types is Might this also be related to DST, e.g. the "truly unsized types" stuff? 1 To a first approximation, of course there's also padding, the tag field, etc. (Edit: I see you've also considered |
It's not enough for it to have a huge/zero/negative infinity size, because even a reference to an uninhabited type should itself be an uninhabited type (since references are guaranteed to be valid). It could be implemented using a more general version of DSTs/truly unsized types, where types get to freely choose the representation of references to themselves (there could be a built-in uninhabited type which all uninhabited types use as the representation for their references). More realistically, shouldn't the compiler detect that code which actually uses uninhabited types, either directly or through a reference, is invalid? (similar, but not quite the same as how you can't use incomplete types in C++) |
Given that uninhabited types currently have a size of |
I have been looking into using an uninhabited type in some changes to rust-postgres to avoid hardcoding support for specific SSL backends: sfackler/rust-postgres@49c09c3#diff-9a0fb54f94cd0bef81a2ada2e9fb0c57R46. It involves having a type that stores the uninhabited type by value, so setting its size to The idea is that the default for the |
spawned off of rust-lang/rust#24590
Empty enums such as:
are a hazard in a number of ways; in particular, odd people like myself keep wanting to investigate what happens if you unsafely inject a value into
Empty
, e.g. viamem::transmute
(or by derefing an unsafe pointer*const Empty
, etc).The resulting code you get when doing a
match
on such a value is pretty much guaranteed to be undefined behavior. The project developers have stated that the memory layout of an empty enum (in fact, of all non C-like enums) is undefined, and thus undefined behavior is justifiable here.Still, it seems to easy to expose this case, and it seems like it would be really easy to prevent it, e.g. by changing the representation size of an empty enum to something semi-absurd. Like
usize::MAX
.The text was updated successfully, but these errors were encountered: