rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.55k stars 12.74k forks source link

Tracking Issue for `#[repr(align(...))]` on function items (fn_align) #82232

Open repnop opened 3 years ago

repnop commented 3 years ago

This feature allows specifying an alignment for function items by adding the #[repr(align(...))] attribute to them. The feature gate for the issue is #![feature(fn_align)]

Example

#[repr(align(16))]
fn requires_alignment() {
    // ...
}

Steps

evanrichter commented 3 years ago

Is it be possible to apply this to methods as well?

#![feature(fn_align)]

#[repr(align(1))]
fn main() {
    Bar::foo();
}

struct Bar;

impl Bar {
    #[repr(align(1))]
    fn foo() {
        println!("bar");
    }
}
error[E0517]: attribute should be applied to a struct, enum, function, or union
  --> src/main.rs:11:12
   |
11 |       #[repr(align(1))]
   |              ^^^^^^^^
12 | /     fn foo() {
13 | |         println!("bar");
14 | |     }
   | |_____- not a struct, enum, function, or union

For more information about this error, try `rustc --explain E0517`.
error: could not compile `asdf` due to previous error
repnop commented 3 years ago

I don't see why not? (I'm surprised it doesn't work already honestly lol) I'll see if I can find some time to investigate that and make another PR to allow that 👍

bunnie commented 1 year ago

Thanks for opening this, I just bumped my head into this exact problem as well!

workingjubilee commented 3 months ago

This should come with a command-line flag to address the request made in #128830 for global code alignment control.