1
- //@ build -pass
1
+ //@ run -pass
2
2
//@ revisions: normal randomize-layout
3
- //@ [randomize-layout]compile-flags: -Zrandomize-layout
3
+ //@ [randomize-layout]compile-flags: -Zrandomize-layout -Zlayout-seed=1
4
4
5
- #! [ crate_type = "lib" ]
5
+ use std :: ptr ;
6
6
7
- struct Foo < T > ( u32 , T , u8 ) ;
8
-
9
- struct Wrapper < T > ( T ) ;
10
7
8
+ // these types only have their field offsets taken, they're never constructed
9
+ #[ allow( dead_code) ]
10
+ pub struct Foo < T > ( u32 , T , u8 ) ;
11
+ #[ allow( dead_code) ]
12
+ pub struct Wrapper < T > ( T ) ;
11
13
#[ repr( transparent) ]
12
- struct TransparentWrapper ( u16 ) ;
14
+ #[ allow( dead_code) ]
15
+ pub struct TransparentWrapper ( u16 ) ;
13
16
14
17
const _: ( ) = {
18
+ // fn pointers are treated interchangably and don't affect layout
19
+ assert ! (
20
+ std:: mem:: offset_of!( Foo :: <fn ( u32 ) >, 1 ) == std:: mem:: offset_of!( Foo :: <fn ( ) -> usize >, 1 )
21
+ ) ;
22
+
15
23
// behavior of the current implementation, not guaranteed
16
24
#[ cfg( not( randomize_layout) ) ]
17
25
assert ! ( std:: mem:: offset_of!( Foo :: <u16 >, 1 ) == std:: mem:: offset_of!( Foo :: <Wrapper <u16 >>, 1 ) ) ;
@@ -28,3 +36,15 @@ const _: () = {
28
36
std:: mem:: offset_of!( Foo :: <u16 >, 1 ) != std:: mem:: offset_of!( Foo :: <TransparentWrapper >, 1 )
29
37
) ;
30
38
} ;
39
+
40
+ #[ allow( dead_code) ]
41
+ struct Unsizable < T : ?Sized > ( usize , T ) ;
42
+
43
+ fn main ( ) {
44
+ // offset_of doesn't let us probe the unsized field, check at runtime.
45
+ let x = & Unsizable :: < [ u32 ; 4 ] > ( 0 , [ 0 ; 4 ] ) ;
46
+ let y: & Unsizable :: < [ u32 ] > = x;
47
+
48
+ // type coercion must not change the layout.
49
+ assert_eq ! ( ptr:: from_ref( & x. 1 ) . addr( ) , ptr:: from_ref( & y. 1 ) . addr( ) ) ;
50
+ }
0 commit comments