Skip to content

There is no syntax for struct alignment #17537

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

Closed
edef1c opened this issue Sep 25, 2014 · 7 comments
Closed

There is no syntax for struct alignment #17537

edef1c opened this issue Sep 25, 2014 · 7 comments

Comments

@edef1c
Copy link
Contributor

edef1c commented Sep 25, 2014

Currently, a C struct definition including an alignment hint (ALIGN(64)) cannot be translated to Rust.
Our inability to translate this makes some C ABIs impossible to adhere to, causing crashes and undefined behaviour.
The only way to hack around this is to build prepend your struct with a zero-length array of a SIMD type.
We should have some kind of syntax for this. Perhaps #[align(64)]?
@eddyb proposed #[repr(align(64))], but I fear that complicates the repr attribute a bit much.

@mahkoh
Copy link
Contributor

mahkoh commented Sep 25, 2014

You can currently use empty arrays to get aligned structs. No need for transmute:

#[repr(C)]
struct X {
    x: u8,
    _alignment: [u64, ..0],
}

fn main() {
    println!("{}", std::mem::pref_align_of::<X>());
    // Prints 8 on x86_64 linux
}

However, being able to manually align stuff would be good. In particular, GCC allows you to specify the alignment of each field. I've suggested this syntax before:

struct X {
    #[aligned(16)]
    x: u8,
    #[aligned(align_of(libc::c_double))]
    y: u8,
}

@steveklabnik
Copy link
Member

I'm not sure, but I think that this change would require an RFC, and is better as an issue on that repo than here.

@edef1c
Copy link
Contributor Author

edef1c commented Sep 25, 2014

Yep, I'm not really skilled at writing RFC-style things though.

@eddyb
Copy link
Member

eddyb commented Sep 25, 2014

I thought we were going to group type representasion modifiers under #[repr], to avoid polluting the attribute "namespace", among other reasons.
Can anyone confirm or deny?
cc @cmr, @aturon

@aturon
Copy link
Member

aturon commented Sep 25, 2014

@edef1c I think @steveklabnik just means that this issue belongs on the rfcs issue tracker, which is where we're trying to put feature requests now.

@eddyb I'm not sure what the plan is.

@steveklabnik
Copy link
Member

Yes, that's what I meant.

@rust-highfive
Copy link
Contributor

This issue has been moved to the RFCs repo: rust-lang/rfcs#325

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

6 participants