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 ) ]
912static 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+
1159fn 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}
0 commit comments