Clarify layout of tuples when last field is unsized#1952
Clarify layout of tuples when last field is unsized#1952Darksonn wants to merge 1 commit intorust-lang:masterfrom
Conversation
|
r? @oli-obk |
|
Failed to set assignee to
|
|
It's only still legal because it causes regressions in dead code to remove it. You can't construct values of such tuples soundly. There is no specified layout of such tuples, so I'd prefer not to document any layout and instead state " type that can't be instantiated, solely used for type system shenanigans" |
|
This PR does not document any layout of such tuples. It adds a note to clarify that a certain guarantee is not made. |
|
Yes, but it still implies these types are useful. At the start of the section it says tuples don't have a specified layout. What value does the note add? |
|
It says that tuples have the same layout as structs, and for structs it is sometimes the case that the last field is guaranteed to be laid out at the end even if repr(rust) is used. Furthermore, it has previously been the case that tuples were always laid out in this manner (and maybe we still do so?).
I don't agree with this claim. Yes, the compiler has a certain freedom in what the precise layout is, but we say it has the repr(rust) layout, and we do in fact document certain guarantees about repr(rust). In my example, if we add an assertion that the size/alignment rustc decides to pick is compatible with the buffer, then any codepath that passes the assertion does not trigger UB. |
This is a follow-up to rust-lang/rust#137728.
Note that we only removed coercions, which is what relied on the layout matching. Tuple types with unsized last field are still legal in the language:
playground
One way to rationalize this is that it's not different from the fact that these two types might have incompatible layouts: