Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd enum size_of computation with named struct variant #52283

Closed
Valloric opened this issue Jul 12, 2018 · 3 comments
Closed

Odd enum size_of computation with named struct variant #52283

Valloric opened this issue Jul 12, 2018 · 3 comments

Comments

@Valloric
Copy link
Contributor

Valloric commented Jul 12, 2018

#![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

@kennytm
Copy link
Member

kennytm commented Jul 12, 2018

I believe this is a special case of #46213.

@nagisa
Copy link
Member

nagisa commented Jul 13, 2018

That is right @kennytm. Closing.

@Valloric in general, since repr(Rust) ADTs have no specified layouts, there should be no expectations with regards to equivalence between two different (or even two same looking) types.

@nagisa nagisa closed this as completed Jul 13, 2018
@Valloric
Copy link
Contributor Author

Valloric commented Jul 14, 2018

@nagisa

in general, since repr(Rust) ADTs have no specified layouts, there should be no expectations with regards to equivalence between two different (or even two same looking) types.

Sure, the layout is unspecified, but users still have expectations that what looks like a simple refactor that extracts an unnamed struct into a named one shouldn't increase the size of an enum.

In other words, this particular case doesn't look like a missed optimization to a user; it just looks like a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants