Fields of enums and unions should be allowed to overlap#2168
Fields of enums and unions should be allowed to overlap#2168Darksonn wants to merge 2 commits intorust-lang:masterfrom
Conversation
8cdbf45 to
247edb4
Compare
|
LGTM with the nit fixed -- the only change here is fixing what clearly looks like an editorial issue. |
Co-authored-by: Ralf Jung <post@ralfj.de>
|
Lol this seems to have been wrong for years (#1152 landed in 2022) and nobody complained in all that time that union fields apparently must be disjoint?^^ |
|
Any chance I could get a review for this one? |
|
I'm a bit surprised this even needs lang team discussion -- it doesn't add any new guarantees, it just removes the accidental guarantee that enum/union fields do not overlap (which is obviously nonsense). So I had expected this to be treated as an editorial change. |
I'm not quite following, why would an enum variant be different from a struct in this regard? |
|
The only difference is that the reference does not already contain a statement about enums, and this was intended to be an editorial change only. |
|
We have so far deliberately left open the possibility for this type to have size 0: enum E {
A(i32, !),
B
} |
I thought that was the purpose of f9ec1d2, to make this also cover struct-like variants and possibly also tuple-like variants (which I consider just a special case where the struct fields have numbers instead of identifiers)?
That seems like it is more of a question of: Do uninhabited variants count for the purpose of determining the number of variants (which isn't relevant right now because that is an unstable feature) and does a single-variant fieldless enum have size zero (which doesn't seem to be in conflict here?). I'm not quite following how the field layout guarantees would be any different here. That is, I wouldn't expect |
The way it is currently written it also covers enums and unions. Not enum variants, but enums themselves. That's clearly nonsense.
If we say that all the fields of a tuple variant fit inside the surrounding enum, we'd force my example enum to be at least 4 bytes large. |
IIRC covering enum variants was the intent. The text could certainly be clarified to say that. I didn't think about unions at the time because they are the stepchild that gets ignored all the time.
When never types are stabilized, we can add a rule to say that uninhabited variants have different properties. I'm not sure how that is relevant right now. |
|
Uninhabited types already exist, I just used enum Void {}
enum E {
A(i32, Void),
B
}So, this is not something we can delegate to some time in the future. |
The lang team very explicitly did not want to rule out this layout optimization in the discussion around We could try to do some careful wording where we specify the layout of enum variants and them separately say how that gets turned into the layout of the entire enum, but no such discussion is currently present, and absent that discussion it is very confusing to talk about the layout of enum variants as if they were a thing in the language that had a layout on its own right. |
This fixes a bug that we currently say that fields of enums and unions must not overlap.
As mentioned in #2166 (comment) there is currently no exception for uninhabited types. We should determine whether such an exception should be added, but this is a pre-existing problem so let's not block this PR on that.
This PR does not add any statement about overlap for fields of the same variant of an enum because the uninhabited type concern should be addressed first before we say anything about enums.