rust-lang / rust-lang/rust

Enum size depends on ordering of members

Open
#117,238 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

enum E {
    E1,
    E2,
    E3,
    E4,
    E5,
    E6,
    E7(bool),
    E8,
    E9,
    E10,
    E11,
    E12,
    E13,
    E14,
    E15,
    E16,
    E17,
    E18,
    E19,
    E20,
    E21,
    E22,
    E23,
    E24,
    E25,
    E26,
    E27,
    E28,
    E29,
    E30,
    E31,
    E32,
    E33,
    E34,
    E35,
    E36,
    E37,
    E38,
    E39,
    E40,
    E41,
    E42,
    E43,
    E44,
    E45,
    E46,
    E47,
    E48,
    E49,
    E50,
    E51,
    E52,
    E53,
    E54,
    E55,
    E56,
    E57,
    E58,
    E59,
    E60,
    E61,
    E62,
    E63,
    E64,
    E65,
    E66,
    E67,
    E68,
    E69,
    E70,
    E71,
    E72,
    E73,
    E74,
    E75,
    E76,
    E77,
    E78,
    E79,
    E80,
    E81,
    E82,
    E83,
    E84,
    E85,
    E86,
    E87,
    E88,
    E89,
    E90,
    E91,
    E92,
    E93,
    E94,
    E95,
    E96,
    E97,
    E98,
    E99,
    E100,
    E101,
    E102,
    E103,
    E104,
    E105,
    E106,
    E107,
    E108,
    E109,
    E110,
    E111,
    E112,
    E113,
    E114,
    E115,
    E116,
    E117,
    E118,
    E119,
    E120,
    E121,
    E122,
    E123,
    E124,
    E125,
    E126,
    E127,
    E128,
    E129,
    E130,
    E131,
    E132,
    E133,
    E134,
    E135,
    E136,
    E137,
    E138,
    E139,
    E140,
    E141,
    E142,
    E143,
    E144,
    E145,
    E146,
    E147,
    E148,
    E149,
    E150,
    E151,
    E152,
    E153,
    E154,
    E155,
    E156,
    E157,
    E158,
    E159,
    E160,
    E161,
    E162,
    E163,
    E164,
    E165,
    E166,
    E167,
    E168,
    E169,
    E170,
    E171,
    E172,
    E173,
    E174,
    E175,
    E176,
    E177,
    E178,
    E179,
    E180,
    E181,
    E182,
    E183,
    E184,
    E185,
    E186,
    E187,
    E188,
    E189,
    E190,
    E191,
    E192,
    E193,
    E194,
    E195,
    E196,
    E197,
    E198,
    E199,
    E200,
    E201,
    E202,
    E203,
    E204,
    E205,
    E206,
    E207,
    E208,
    E209,
    E210,
    E211,
    E212,
    E213,
    E214,
    E215,
    E216,
    E217,
    E218,
    E219,
    E220,
    E221,
    E222,
    E223,
    E224,
    E225,
    E226,
    E227,
    E228,
    E229,
    E230,
    E231,
    E232,
    E233,
    E234,
    E235,
    E236,
    E237,
    E238,
    E239,
    E240,
    E241,
    E242,
    E243,
    E244,
    E245,
    E246,
    E247,
    E248,
    E249,
    E250,
    E251,
    E252,
    E253,
    E254,
    E255,
}

enum F {
    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    F13,
    F14,
    F15,
    F16,
    F17,
    F18,
    F19,
    F20,
    F21,
    F22,
    F23,
    F24,
    F25,
    F26,
    F27,
    F28,
    F29,
    F30,
    F31,
    F32,
    F33,
    F34,
    F35,
    F36,
    F37,
    F38,
    F39,
    F40,
    F41,
    F42,
    F43,
    F44,
    F45,
    F46,
    F47,
    F48,
    F49,
    F50,
    F51,
    F52,
    F53,
    F54,
    F55,
    F56,
    F57,
    F58,
    F59,
    F60,
    F61,
    F62,
    F63,
    F64,
    F65,
    F66,
    F67,
    F68,
    F69,
    F70,
    F71,
    F72,
    F73,
    F74,
    F75,
    F76,
    F77,
    F78,
    F79,
    F80,
    F81,
    F82,
    F83,
    F84,
    F85,
    F86,
    F87,
    F88,
    F89,
    F90,
    F91,
    F92,
    F93,
    F94,
    F95,
    F96,
    F97,
    F98,
    F99,
    F100,
    F101,
    F102,
    F103,
    F104,
    F105,
    F106,
    F107,
    F108,
    F109,
    F110,
    F111,
    F112,
    F113,
    F114,
    F115,
    F116,
    F117,
    F118,
    F119,
    F120,
    F121,
    F122,
    F123,
    F124,
    F125,
    F126,
    F127,
    F128,
    F129,
    F130,
    F131,
    F132,
    F133,
    F134,
    F135,
    F136,
    F137,
    F138,
    F139,
    F140,
    F141,
    F142,
    F143,
    F144,
    F145,
    F146,
    F147,
    F148,
    F149,
    F150,
    F151,
    F152,
    F153,
    F154,
    F155,
    F156,
    F157,
    F158,
    F159,
    F160,
    F161,
    F162,
    F163,
    F164,
    F165,
    F166,
    F167,
    F168,
    F169,
    F170,
    F171,
    F172,
    F173,
    F174,
    F175,
    F176,
    F177,
    F178,
    F179,
    F180,
    F181,
    F182,
    F183,
    F184,
    F185,
    F186,
    F187,
    F188,
    F189,
    F190,
    F191,
    F192,
    F193,
    F194,
    F195,
    F196,
    F197,
    F198,
    F199,
    F200,
    F201,
    F202,
    F203,
    F204,
    F205,
    F206,
    F207,
    F208,
    F209,
    F210,
    F211,
    F212,
    F213,
    F214,
    F215,
    F216,
    F217,
    F218,
    F219,
    F220,
    F221,
    F222,
    F223,
    F224,
    F225,
    F226,
    F227,
    F228,
    F229,
    F230,
    F231,
    F232,
    F233,
    F234,
    F235,
    F236,
    F237,
    F238,
    F239,
    F240,
    F241,
    F242,
    F243,
    F244,
    F245,
    F246,
    F247,
    F248,
    F249,
    F250,
    F251,
    F252,
    F253,
    F254,
    F255(bool),
}

fn main () {
    let ae = E::E1;
    let be = E::E7(true);
    let ce = E::E8;
    let af = F::F1;
    let bf = F::F255(true);
    let cf = F::F8;
}

Then I ran this in gdb and examined the size of the enums:

(gdb) p sizeof(ae)
$1 = 2
(gdb) p sizeof(af)
$2 = 1

I expected these to have the same size. The two enums differ only in the placement of the (bool)-carrying member.

Instead, the two enums have different sizes.

rustc --version --verbose:

prentzel. rustc --version --verbose
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

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 compiling and running the standalone Rust reproducer from the issue, then confirm the enum sizes with the shown gdb commands. Trace the compiler's enum layout handling to determine why member ordering changes the size; done means the behavior is corrected or its intended layout rule is established with regression coverage.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.