Closed
Description
Originally posted by @jyn514 in #86263 (comment)
Metadata
Metadata
Assignees
Labels
Area: Memory layout of typesArea: `rustdoc --show-type-layout` (nightly-only)Category: This is a bug.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Relevant to the rustdoc team, which will review and decide on the PR/issue.This issue requires a nightly compiler in some way.
Activity
jyn514 commentedon Jul 9, 2021
cc @camelid @pickfire
camelid commentedon Jul 9, 2021
I'm ambivalent, I don't think showing zero is necessarily wrong, especially since that's what's reported by
layout_of
.asquared31415 commentedon Jul 9, 2021
In my opinion, I think that while size 0 is not wrong, that showing that it's uninhabited is more right, or at least more clear and makes the distinction.
pickfire commentedon Jul 10, 2021
I don't understand uninhabited when I first see it. It is a hard word compared to 0. I think 0 is easier. Why not both? What if we show
0 (uninhabited)
?Also, what if the enum is not empty but have a single/multiple ZST like phantom type?
GuillaumeGomez commentedon Jul 10, 2021
I think displaying 0 is better as well, but we can do as @pickfire suggested and simply display both.
jyn514 commentedon Jul 10, 2021
0 is wrong. 0 means "this is a zero sized type", like
()
orNoneError
; it can be created. Uninhabited types cannot be created, they are things like!
andenum Void {}
. IMO posting 0 there is actively misleading. See also https://internals.rust-lang.org/t/size-of-uninhabited-types/7651.Ok, then we can link to some docs. I didn't see anything in the nomicon but I'm sure they'd take a PR adding it to "exotically sized types".
jyn514 commentedon Jul 10, 2021
This is a bug imo, it allows transmuting from ZSTs to uninhabited types which is UB.
GuillaumeGomez commentedon Jul 10, 2021
Even if they cannot be created, their size remain 0 as they don't "exist", however since they cannot be constructed, it makes sense to mention they're unhabited types. Well, I don't have a strong conviction on this in any case. I think we all agree for the mention of "unhabited" though.
pickfire commentedon Jul 11, 2021
Yeah, that solved the issue of 0 which is wrong. I do think it is better to link it to https://doc.rust-lang.org/stable/reference/glossary.html?highlight=uninhabited#uninhabited, but I think it's wrong to put that in "size" such that it should be in "type" instead, because it doesn't have a size to begin with.
Well, there are parts where rust sacrificed correctness for ease of understanding, one example is
mut
which means uniqueness not exactly mutable all the time.nerandell commentedon Sep 21, 2021
@rustbot claim
scottmcm commentedon Oct 19, 2022
Note that uninhabited is orthogonal to size. You can have an uninhabited type of any size.
So I think showing uninhabitedness is good, but in addition to the size+align, not instead.
camelid commentedon Oct 19, 2022
Hmm, I think that
layout_of
will report that the size is always 0 for an uninhabited type though, unless that's changed recently.scottmcm commentedon Oct 19, 2022
That would surprise me, since that'd be different from what
size_of
andalign_of
do: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e1ad034d9812b5356493e4faefe4be61Rollup merge of rust-lang#108734 - clubby789:rustdoc-layout-uninhabit…
Rollup merge of rust-lang#108734 - clubby789:rustdoc-layout-uninhabit…