File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ //@ build-pass
2+ //! Check that we can codegen setting and getting discriminants, including non-null niches,
3+ //! for enums with a pointer-like ABI. This used to crash llvm.
4+
5+ #![ feature( rustc_attrs) ]
6+ use std:: { ptr, mem} ;
7+
8+
9+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
10+ #[ rustc_layout_scalar_valid_range_end( 100 ) ]
11+ #[ derive( Copy , Clone ) ]
12+ struct PointerWithRange ( #[ allow( dead_code) ] * const u8 ) ;
13+
14+
15+ fn main ( ) {
16+ let val = unsafe { PointerWithRange ( ptr:: without_provenance ( 90 ) ) } ;
17+
18+ let ptr = Some ( val) ;
19+ assert ! ( ptr. is_some( ) ) ;
20+ let raw = unsafe { mem:: transmute :: < _ , usize > ( ptr) } ;
21+ assert_eq ! ( raw, 90 ) ;
22+
23+ let ptr = Some ( Some ( val) ) ;
24+ assert ! ( ptr. is_some( ) ) ;
25+ assert ! ( ptr. unwrap( ) . is_some( ) ) ;
26+ let raw = unsafe { mem:: transmute :: < _ , usize > ( ptr) } ;
27+ assert_eq ! ( raw, 90 ) ;
28+
29+ let ptr: Option < PointerWithRange > = None ;
30+ assert ! ( ptr. is_none( ) ) ;
31+ let raw = unsafe { mem:: transmute :: < _ , usize > ( ptr) } ;
32+ assert ! ( !( 1 ..=100 ) . contains( & raw) ) ;
33+
34+ let ptr: Option < Option < PointerWithRange > > = None ;
35+ assert ! ( ptr. is_none( ) ) ;
36+ let raw = unsafe { mem:: transmute :: < _ , usize > ( ptr) } ;
37+ assert ! ( !( 1 ..=100 ) . contains( & raw) ) ;
38+ }
Original file line number Diff line number Diff line change 55#![ allow( dead_code) ]
66#![ feature( never_type) ]
77#![ feature( pointer_is_aligned_to) ]
8+ #![ feature( rustc_attrs) ]
89
910use std:: mem:: size_of;
1011use std:: num:: NonZero ;
@@ -237,6 +238,15 @@ struct VecDummy {
237238 len : usize ,
238239}
239240
241+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
242+ #[ rustc_layout_scalar_valid_range_end( 100 ) ]
243+ struct PointerWithRange ( #[ allow( dead_code) ] * const u8 ) ;
244+
245+ const _: ( ) = {
246+ assert ! ( size_of:: <Option <PointerWithRange >>( ) == size_of:: <PointerWithRange >( ) ) ;
247+ assert ! ( size_of:: <Option <Option <PointerWithRange >>>( ) == size_of:: <PointerWithRange >( ) ) ;
248+ } ;
249+
240250pub fn main ( ) {
241251 assert_eq ! ( size_of:: <u8 >( ) , 1 as usize ) ;
242252 assert_eq ! ( size_of:: <u32 >( ) , 4 as usize ) ;
@@ -354,4 +364,6 @@ pub fn main() {
354364 assert ! ( ptr:: from_ref( & v. a) . addr( ) > ptr:: from_ref( & v. b) . addr( ) ) ;
355365
356366
367+ assert_eq ! ( size_of:: <Option <PointerWithRange >>( ) , size_of:: <PointerWithRange >( ) ) ;
368+ assert_eq ! ( size_of:: <Option <Option <PointerWithRange >>>( ) , size_of:: <PointerWithRange >( ) ) ;
357369}
You can’t perform that action at this time.
0 commit comments