Post #568667
2026-03-01 14:57 UTC
Trying to understand recent #RustLang activities (https://github.com/rust-lang/compiler-team/issues/922), why do types with uninhabited fields (or enum variants that are uninhabited) take up space at all?
Example at https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&code=use+core%3A%3Amem%3A%3A*%3B%0Ause+core%3A%3Aany%3A%3A*%3B%0A%0A%23%5Ballow%28dead_code%29%5D%0Astruct+DataPlusInfallible+%7B%0A++++a%3A+u8%2C%0A++++b%3A+u64%2C%0A++++c%3A+core%3A%3Aconvert%3A%3AInfallible%2C%0A%7D%0A%0A%23%5Ballow%28dead_code%29%5D%0Astruct+ZeroPlusInfallible+%7B%0A++++a%3A+%28%29%2C%0A++++c%3A+core%3A%3Aconvert%3A%3AInfallible%2C%0A%7D%0A%0A%23%5Ballow%28dead_code%29%5D%0Astruct+WrappedInfallible+%7B%0A++++c%3A+core%3A%3Aconvert%3A%3AInfallible%2C%0A%7D%0A%0Afn+show_layout%3CT%3E%28%29+%7B%0A++++println%21%28%22Type+%7B%7D+has+size+%7B%7D+and+alignment+%7B%7D%22%2C+type_name%3A%3A%3CT%3E%28%29%2C+size_of%3A%3A%3CT%3E%28%29%2C+align_of%3A%3A%3CT%3E%28%29%29%0A%7D%0A%0Afn+main%28%29+%7B%0A++++show_layout%3A%3A%3CDataPlusInfallible%3E%28%29%3B%0A++++show_layout%3A%3A%3CResult%3Cu8%2C+DataPlusInfallible%3E%3E%28%29%3B%0A++++show_layout%3A%3A%3CResult%3Cu8%2C+ZeroPlusInfallible%3E%3E%28%29%3B%0A++++show_layout%3A%3A%3CResult%3Cu8%2C+WrappedInfallible%3E%3E%28%29%3B%0A++++show_layout%3A%3A%3CResult%3Cu8%2C+core%3A%3Aconvert%3A%3AInfallible%3E%3E%28%29%3B%0A%7D -- when a struct composed of Never and some data (which makes it still uninhabited) is placed in a Result's E position, the result is still larger than T.
Replies (1)
-
@chrysn@chaos.social 2026-03-01 15:24
Found some of it: https://github.com/rust-lang/unsafe-code-guidelines/issues/443#issuecomment-3980285502