rust-lang / rust-lang/rust

Missed layout optimisation with multiple niches

Open
#119,507 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-layout C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::num::NonZeroU8;

struct Test {
    niche_one: NonZeroU8,
    niche_two: NonZeroU8,
}

fn main() {
    assert_eq!(
        std::mem::size_of::<Test>(),
        std::mem::size_of::<Option<Option<Test>>>()
    );
}

I expected this program to exit fine, and fit the tag of the second option in the available niche.
Instead, the program panics, as the second option wastes a byte of memory making it's own tag byte.

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (e51e98dde 2023-12-31)
binary: rustc
commit-hash: e51e98dde6a60637b6a71b8105245b629ac3fe77
commit-date: 2023-12-31
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Rust program with the reported rustc nightly version and compare its layout across relevant compiler versions. Investigate rustc's type-layout and niche-optimization handling for nested Options with multiple NonZeroU8 fields. Done means the reported layout behavior is corrected or the issue is explained and covered by an appropriate compiler regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.