about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-09-10 14:17:38 +0200
committerGitHub <noreply@github.com>2025-09-10 14:17:38 +0200
commit422c76adae98b55d13a07a8fb47d8bc47fa96fdf (patch)
tree331146cd308765ca059238a4534b7a8c4d8d5910 /tests
parentd0ba5e33ff7e05f6ccd365fe97b822e33beaed6b (diff)
parentcbacd00f106778830803ad2e2364518f06ff9078 (diff)
downloadrust-422c76adae98b55d13a07a8fb47d8bc47fa96fdf.tar.gz
rust-422c76adae98b55d13a07a8fb47d8bc47fa96fdf.zip
Rollup merge of #146178 - folkertdev:static-align, r=jdonszelmann,ralfjung,traviscross
Implement `#[rustc_align_static(N)]` on `static`s

Tracking issue: https://github.com/rust-lang/rust/issues/146177

```rust
#![feature(static_align)]

#[rustc_align_static(64)]
static SO_ALIGNED: u64 = 0;
```

We need a different attribute than `rustc_align` because unstable attributes are tied to their feature (we can't have two unstable features use the same unstable attribute). Otherwise this uses all of the same infrastructure as `#[rustc_align]`.

r? `@traviscross`
Diffstat (limited to 'tests')
-rw-r--r--tests/codegen-llvm/align-static.rs31
-rw-r--r--tests/ui/attributes/malformed-static-align.rs17
-rw-r--r--tests/ui/attributes/malformed-static-align.stderr45
-rw-r--r--tests/ui/feature-gates/feature-gate-static_align.rs11
-rw-r--r--tests/ui/feature-gates/feature-gate-static_align.stderr23
-rw-r--r--tests/ui/static/static-align.rs26
6 files changed, 153 insertions, 0 deletions
diff --git a/tests/codegen-llvm/align-static.rs b/tests/codegen-llvm/align-static.rs
new file mode 100644
index 00000000000..53db998919a
--- /dev/null
+++ b/tests/codegen-llvm/align-static.rs
@@ -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;
diff --git a/tests/ui/attributes/malformed-static-align.rs b/tests/ui/attributes/malformed-static-align.rs
new file mode 100644
index 00000000000..305d8acf8af
--- /dev/null
+++ b/tests/ui/attributes/malformed-static-align.rs
@@ -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;
diff --git a/tests/ui/attributes/malformed-static-align.stderr b/tests/ui/attributes/malformed-static-align.stderr
new file mode 100644
index 00000000000..35f654d3990
--- /dev/null
+++ b/tests/ui/attributes/malformed-static-align.stderr
@@ -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`.
diff --git a/tests/ui/feature-gates/feature-gate-static_align.rs b/tests/ui/feature-gates/feature-gate-static_align.rs
new file mode 100644
index 00000000000..4d8f0e18d94
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-static_align.rs
@@ -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;
+}
diff --git a/tests/ui/feature-gates/feature-gate-static_align.stderr b/tests/ui/feature-gates/feature-gate-static_align.stderr
new file mode 100644
index 00000000000..b45fcdefc9c
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-static_align.stderr
@@ -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`.
diff --git a/tests/ui/static/static-align.rs b/tests/ui/static/static-align.rs
new file mode 100644
index 00000000000..93241db09f9
--- /dev/null
+++ b/tests/ui/static/static-align.rs
@@ -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)) };
+}