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 ! (
20+ std:: mem:: offset_of!( Foo :: <fn ( u32 ) >, 1 ) == std:: mem:: offset_of!( Foo :: <fn ( ) -> usize >, 1 )
21+ ) ;
22+
1523 // behavior of the current implementation, not guaranteed
1624 #[ cfg( not( randomize_layout) ) ]
1725 assert ! ( std:: mem:: offset_of!( Foo :: <u16 >, 1 ) == std:: mem:: offset_of!( Foo :: <Wrapper <u16 >>, 1 ) ) ;
@@ -28,3 +36,15 @@ const _: () = {
2836 std:: mem:: offset_of!( Foo :: <u16 >, 1 ) != std:: mem:: offset_of!( Foo :: <TransparentWrapper >, 1 )
2937 ) ;
3038} ;
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