-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Implement #[rustc_align_static(N)]
on static
s
#146178
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -621,6 +621,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ | |
), | ||
// FIXME(#82232, #143834): temporarily renamed to mitigate `#[align]` nameres ambiguity | ||
gated!(rustc_align, Normal, template!(List: &["alignment"]), DuplicatesOk, EncodeCrossCrate::No, fn_align, experimental!(rustc_align)), | ||
gated!(rustc_align_static, Normal, template!(List: &["alignment"]), DuplicatesOk, EncodeCrossCrate::No, static_align, experimental!(rustc_align_static)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RalfJung this is where the attribute gets declared, and the feature name on this line ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's very odd, why does it have to be unique? That's not how features work anywhere else in the compiler, AFAIK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, sorry, what I meant by unique is that an attribute must be tied to one unstable feature. So, one unstable feature can "contain" multiple attributes, but an attribute always is tied to only one unstable feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, by "attribute used for feature" you mean "attribute is gated by feature". That's a rather confusing and non-standard way of saying things.^^ So what you want is to have a separate feature gate for aligning functions vs aligning statics, and the attribute registration macro isn't flexible enough to allow that. Since these are all temporary names anyway, I guess it doesn't matter too much, but when it comes to stabilization we very much might need the ability to stabilize an attribute in some positions while leaving it unstable elsewhere. Otherwise we have no way of unstably extending where an existing stable attribute can be used. So this limitation of attribute feature gating seems like it could become problematic. Cc @jdonszelmann |
||
ungated!( | ||
unsafe(Edition2024) export_name, Normal, | ||
template!(NameValueStr: "name", "https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![feature(static_align)] | ||
|
||
// When a static uses `align(N)`, its address should be a multiple of `N`. | ||
|
||
#[rustc_align_static(256)] | ||
static FOO: u64 = 0; | ||
|
||
#[rustc_align_static(512)] | ||
static BAR: u64 = 0; | ||
|
||
fn main() { | ||
assert!(core::ptr::from_ref(&FOO).addr().is_multiple_of(256)); | ||
assert!(core::ptr::from_ref(&BAR).addr().is_multiple_of(512)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 | ||
|
||
#![crate_type = "lib"] | ||
#![feature(static_align)] | ||
|
||
// CHECK: @STATIC_ALIGN = | ||
// CHECK-SAME: align 16 | ||
#[no_mangle] | ||
#[rustc_align_static(16)] | ||
pub static STATIC_ALIGN: u64 = 0; | ||
|
||
// CHECK: @ALIGN_SPECIFIED_TWICE_1 = | ||
// CHECK-SAME: align 64 | ||
#[no_mangle] | ||
#[rustc_align_static(32)] | ||
#[rustc_align_static(64)] | ||
pub static ALIGN_SPECIFIED_TWICE_1: u64 = 0; | ||
|
||
// CHECK: @ALIGN_SPECIFIED_TWICE_2 = | ||
// CHECK-SAME: align 128 | ||
#[no_mangle] | ||
#[rustc_align_static(128)] | ||
#[rustc_align_static(32)] | ||
pub static ALIGN_SPECIFIED_TWICE_2: u64 = 0; | ||
|
||
// CHECK: @ALIGN_SPECIFIED_TWICE_3 = | ||
// CHECK-SAME: align 256 | ||
#[no_mangle] | ||
#[rustc_align_static(32)] | ||
#[rustc_align_static(256)] | ||
pub static ALIGN_SPECIFIED_TWICE_3: u64 = 0; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(static_align)] | ||
#![crate_type = "lib"] | ||
|
||
#[rustc_align_static = 16] //~ ERROR malformed `rustc_align_static` attribute input | ||
static S1: () = (); | ||
|
||
#[rustc_align_static("hello")] //~ ERROR invalid alignment value: not an unsuffixed integer | ||
static S2: () = (); | ||
|
||
#[rustc_align_static(0)] //~ ERROR invalid alignment value: not a power of two | ||
static S3: () = (); | ||
|
||
#[repr(align(16))] //~ ERROR `#[repr(align(...))]` is not supported on static | ||
static S4: () = (); | ||
|
||
#[rustc_align_static(16)] //~ ERROR `#[rustc_align_static]` attribute cannot be used on structs | ||
struct Struct1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error[E0539]: malformed `rustc_align_static` attribute input | ||
--> $DIR/malformed-static-align.rs:4:1 | ||
| | ||
LL | #[rustc_align_static = 16] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected this to be a list | ||
| help: must be of the form: `#[rustc_align_static(<alignment in bytes>)]` | ||
|
||
error[E0589]: invalid alignment value: not an unsuffixed integer | ||
--> $DIR/malformed-static-align.rs:7:22 | ||
| | ||
LL | #[rustc_align_static("hello")] | ||
| ^^^^^^^ | ||
|
||
error[E0589]: invalid alignment value: not a power of two | ||
--> $DIR/malformed-static-align.rs:10:22 | ||
| | ||
LL | #[rustc_align_static(0)] | ||
| ^ | ||
|
||
error: `#[rustc_align_static]` attribute cannot be used on structs | ||
--> $DIR/malformed-static-align.rs:16:1 | ||
| | ||
LL | #[rustc_align_static(16)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: `#[rustc_align_static]` can be applied to statics and foreign statics | ||
|
||
error: `#[repr(align(...))]` is not supported on statics | ||
--> $DIR/malformed-static-align.rs:13:8 | ||
| | ||
LL | #[repr(align(16))] | ||
| ^^^^^^^^^ | ||
| | ||
help: use `#[rustc_align_static(...)]` instead | ||
--> $DIR/malformed-static-align.rs:13:8 | ||
| | ||
LL | #[repr(align(16))] | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
Some errors have detailed explanations: E0539, E0589. | ||
For more information about an error, try `rustc --explain E0539`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![crate_type = "lib"] | ||
|
||
#[rustc_align_static(16)] | ||
//~^ ERROR the `#[rustc_align_static]` attribute is an experimental feature | ||
static REQUIRES_ALIGNMENT: u64 = 0; | ||
|
||
extern "C" { | ||
#[rustc_align_static(16)] | ||
//~^ ERROR the `#[rustc_align_static]` attribute is an experimental feature | ||
static FOREIGN_STATIC: u32; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error[E0658]: the `#[rustc_align_static]` attribute is an experimental feature | ||
--> $DIR/feature-gate-static_align.rs:3:1 | ||
| | ||
LL | #[rustc_align_static(16)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #146177 <https://github.com/rust-lang/rust/issues/146177> for more information | ||
= help: add `#![feature(static_align)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error[E0658]: the `#[rustc_align_static]` attribute is an experimental feature | ||
--> $DIR/feature-gate-static_align.rs:8:5 | ||
| | ||
LL | #[rustc_align_static(16)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #146177 <https://github.com/rust-lang/rust/issues/146177> for more information | ||
= help: add `#![feature(static_align)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//@ run-pass | ||
#![feature(static_align)] | ||
|
||
#[rustc_align_static(64)] | ||
static A: u8 = 0; | ||
|
||
#[rustc_align_static(64)] | ||
static B: u8 = 0; | ||
|
||
#[rustc_align_static(128)] | ||
#[no_mangle] | ||
static EXPORTED: u64 = 0; | ||
|
||
unsafe extern "C" { | ||
#[rustc_align_static(128)] | ||
#[link_name = "EXPORTED"] | ||
static C: u64; | ||
} | ||
|
||
fn main() { | ||
assert!(core::ptr::from_ref(&A).addr().is_multiple_of(64)); | ||
assert!(core::ptr::from_ref(&B).addr().is_multiple_of(64)); | ||
|
||
assert!(core::ptr::from_ref(&EXPORTED).addr().is_multiple_of(128)); | ||
unsafe { assert!(core::ptr::from_ref(&C).addr().is_multiple_of(128)) }; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.