Skip to content

Rustdoc: size layout should show "uninhabited" for empty enums, not 0 #87008

Closed
@jyn514

Description

@jyn514
Member

Originally posted by @jyn514 in #86263 (comment)

Activity

added
requires-nightlyThis issue requires a nightly compiler in some way.
T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
C-bugCategory: This is a bug.
on Jul 9, 2021
jyn514

jyn514 commented on Jul 9, 2021

@jyn514
MemberAuthor
added
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
on Jul 9, 2021
camelid

camelid commented on Jul 9, 2021

@camelid
Member

I'm ambivalent, I don't think showing zero is necessarily wrong, especially since that's what's reported by layout_of.

asquared31415

asquared31415 commented on Jul 9, 2021

@asquared31415
Contributor

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

pickfire commented on Jul 10, 2021

@pickfire
Contributor

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

GuillaumeGomez commented on Jul 10, 2021

@GuillaumeGomez
Member

I think displaying 0 is better as well, but we can do as @pickfire suggested and simply display both.

jyn514

jyn514 commented on Jul 10, 2021

@jyn514
MemberAuthor

0 is wrong. 0 means "this is a zero sized type", like () or NoneError; it can be created. Uninhabited types cannot be created, they are things like ! and enum Void {}. IMO posting 0 there is actively misleading. See also https://internals.rust-lang.org/t/size-of-uninhabited-types/7651.

I don't understand uninhabited when I first see it.

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

jyn514 commented on Jul 10, 2021

@jyn514
MemberAuthor

I'm ambivalent, I don't think showing zero is necessarily wrong, especially since that's what's reported by layout_of.

This is a bug imo, it allows transmuting from ZSTs to uninhabited types which is UB.

GuillaumeGomez

GuillaumeGomez commented on Jul 10, 2021

@GuillaumeGomez
Member

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

pickfire commented on Jul 11, 2021

@pickfire
Contributor

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".

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

nerandell commented on Sep 21, 2021

@nerandell

@rustbot claim

scottmcm

scottmcm commented on Oct 19, 2022

@scottmcm
Member

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

camelid commented on Oct 19, 2022

@camelid
Member

Note that uninhabited is orthogonal to size. You can have an uninhabited type of any size.

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

scottmcm commented on Oct 19, 2022

@scottmcm
Member

That would surprise me, since that'd be different from what size_of and align_of do: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e1ad034d9812b5356493e4faefe4be61

added 2 commits that reference this issue on Mar 4, 2023

Rollup merge of rust-lang#108734 - clubby789:rustdoc-layout-uninhabit…

f2ae034

Rollup merge of rust-lang#108734 - clubby789:rustdoc-layout-uninhabit…

99fad38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesA-rustdoc-type-layoutArea: `rustdoc --show-type-layout` (nightly-only)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nerandell@jonas-schievink@GuillaumeGomez@pickfire@scottmcm

      Issue actions

        Rustdoc: size layout should show "uninhabited" for empty enums, not 0 · Issue #87008 · rust-lang/rust