Skip to content

Commit dad2206

Browse files
authored
Rollup merge of #146281 - Jules-Bertholet:static-align-thread-local, r=Mark-Simulacrum
Support `#[rustc_align_static]` inside `thread_local!` Tracking issue: rust-lang/rust#146177 ```rust thread_local! { #[rustc_align_static(64)] static SO_ALIGNED: u64 = const { 0 }; } ``` This increases the amount of recursion the macro performs (once per attribute in addition to the previous once per item), making it easier to hit the recursion limit. I’ve added workarounds to limit the impact in the case of long doc comments, but this still needs a crater run just in case. r? libs ``@rustbot`` label A-attributes A-macros A-thread-locals F-static_align T-libs
2 parents 9c5bbfc + d9b51b0 commit dad2206

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

tests/pass/static_align.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![feature(static_align)]
2+
#![deny(non_upper_case_globals)]
3+
4+
use std::cell::Cell;
25

36
// When a static uses `align(N)`, its address should be a multiple of `N`.
47

@@ -8,7 +11,64 @@ static FOO: u64 = 0;
811
#[rustc_align_static(512)]
912
static BAR: u64 = 0;
1013

14+
struct HasDrop(*const HasDrop);
15+
16+
impl Drop for HasDrop {
17+
fn drop(&mut self) {
18+
assert_eq!(core::ptr::from_mut(self).cast_const(), self.0);
19+
}
20+
}
21+
22+
thread_local! {
23+
#[rustc_align_static(4096)]
24+
static LOCAL: u64 = 0;
25+
26+
#[allow(unused_mut, reason = "test attribute handling")]
27+
#[cfg_attr(true, rustc_align_static(4096))]
28+
static CONST_LOCAL: u64 = const { 0 };
29+
30+
#[cfg_attr(any(true), cfg_attr(true, rustc_align_static(4096)))]
31+
#[allow(unused_mut, reason = "test attribute handling")]
32+
static HASDROP_LOCAL: Cell<HasDrop> = Cell::new(HasDrop(core::ptr::null()));
33+
34+
/// I love doc comments.
35+
#[allow(unused_mut, reason = "test attribute handling")]
36+
#[cfg_attr(all(),
37+
cfg_attr(any(true),
38+
cfg_attr(true, rustc_align_static(4096))))]
39+
#[allow(unused_mut, reason = "test attribute handling")]
40+
/// I love doc comments.
41+
static HASDROP_CONST_LOCAL: Cell<HasDrop> = const { Cell::new(HasDrop(core::ptr::null())) };
42+
43+
#[cfg_attr(true,)]
44+
#[cfg_attr(false,)]
45+
#[cfg_attr(
46+
true,
47+
rustc_align_static(32),
48+
cfg_attr(true, allow(non_upper_case_globals, reason = "test attribute handling")),
49+
cfg_attr(false,)
50+
)]
51+
#[cfg_attr(false, rustc_align_static(0))]
52+
static more_attr_testing: u64 = 0;
53+
}
54+
55+
fn thread_local_ptr<T>(key: &'static std::thread::LocalKey<T>) -> *const T {
56+
key.with(|local| core::ptr::from_ref::<T>(local))
57+
}
58+
1159
fn main() {
1260
assert!(core::ptr::from_ref(&FOO).addr().is_multiple_of(256));
1361
assert!(core::ptr::from_ref(&BAR).addr().is_multiple_of(512));
62+
63+
assert!(thread_local_ptr(&LOCAL).addr().is_multiple_of(4096));
64+
assert!(thread_local_ptr(&CONST_LOCAL).addr().is_multiple_of(4096));
65+
assert!(thread_local_ptr(&HASDROP_LOCAL).addr().is_multiple_of(4096));
66+
assert!(thread_local_ptr(&HASDROP_CONST_LOCAL).addr().is_multiple_of(4096));
67+
assert!(thread_local_ptr(&more_attr_testing).addr().is_multiple_of(32));
68+
69+
// Test that address (and therefore alignment) is maintained during drop
70+
let hasdrop_ptr = thread_local_ptr(&HASDROP_LOCAL);
71+
core::mem::forget(HASDROP_LOCAL.replace(HasDrop(hasdrop_ptr.cast())));
72+
let hasdrop_const_ptr = thread_local_ptr(&HASDROP_CONST_LOCAL);
73+
core::mem::forget(HASDROP_CONST_LOCAL.replace(HasDrop(hasdrop_const_ptr.cast())));
1474
}

tests/pass/thread_local-panic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
thread_local! {
2+
static LOCAL: u64 = panic!();
3+
4+
}
5+
6+
fn main() {
7+
let _ = std::panic::catch_unwind(|| LOCAL.with(|_| {}));
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
thread 'main' ($TID) panicked at tests/pass/thread_local-panic.rs:LL:CC:
3+
explicit panic
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect

0 commit comments

Comments
 (0)