1- //@ build -pass
1+ //@ run -pass
22//@ revisions: normal randomize-layout
3- //@ [randomize-layout]compile-flags: -Zrandomize-layout
3+ //@ [randomize-layout]compile-flags: -Zrandomize-layout -Zlayout-seed=1
44
5- #! [ crate_type = "lib" ]
5+ use std :: ptr ;
66
7- struct Foo < T > ( u32 , T , u8 ) ;
8-
9- struct Wrapper < T > ( T ) ;
107
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 ) ;
1113#[ repr( transparent) ]
12- struct TransparentWrapper ( u16 ) ;
14+ #[ allow( dead_code) ]
15+ pub struct TransparentWrapper ( u16 ) ;
1316
1417const _: ( ) = {
18+ // fn pointers are treated interchangably and don't affect layout
19+ assert ! ( std:: mem:: offset_of!( Foo :: <fn ( u32 ) >, 1 ) == std:: mem:: offset_of!( Foo :: <fn ( ) -> usize >, 1 ) ) ;
20+
1521 // behavior of the current implementation, not guaranteed
1622 #[ cfg( not( randomize_layout) ) ]
1723 assert ! ( std:: mem:: offset_of!( Foo :: <u16 >, 1 ) == std:: mem:: offset_of!( Foo :: <Wrapper <u16 >>, 1 ) ) ;
@@ -28,3 +34,16 @@ const _: () = {
2834 std:: mem:: offset_of!( Foo :: <u16 >, 1 ) != std:: mem:: offset_of!( Foo :: <TransparentWrapper >, 1 )
2935 ) ;
3036} ;
37+
38+ #[ allow( dead_code) ]
39+ struct Unsizable < T : ?Sized > ( usize , T ) ;
40+
41+ fn main ( ) {
42+ // offset_of doesn't let us probe the unsized field, check at runtime.
43+ let x = & Unsizable :: < [ u32 ; 4 ] > ( 0 , [ 0 ; 4 ] ) ;
44+ let y: & Unsizable :: < [ u32 ] > = x;
45+
46+ // type coercion must not change the layout.
47+ assert_eq ! ( ptr:: from_ref( & x. 1 ) . addr( ) , ptr:: from_ref( & y. 1 ) . addr( ) ) ;
48+
49+ }
0 commit comments