Skip to content

Odd enum size_of computation with named struct variant #52283

Closed
@Valloric

Description

@Valloric
#![allow(dead_code)]

use std::collections::HashSet;
use std::mem::size_of;

struct InlineData {
  size: u8,
  data: [u32; 13],    
}

enum DataA {
  Inline(InlineData),
  Heap(HashSet<u32>),
}

enum DataB {
  Inline {
    size: u8,
    data: [u32; 13],
  },
  Heap(HashSet<u32>),
}

fn main() {
  assert_eq!(size_of::<DataA>(), size_of::<DataB>());
}

Output:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `64`,
 right: `56`', src/main.rs:25:3

Playground link.

I'd expect DataA and DataB to have the same size. I certainly wouldn't expect a simple refactoring that extracts an inline struct into a named one to increase the size of the enum.

My guess as to what's happening here is that rustc isn't smart enough to put the enum discriminant into the InlineData padding bytes.

cc @jwilm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions