File tree Expand file tree Collapse file tree
tests/codegen-llvm/issues Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ //! Ensure that redundant null checks on `&mut T` from `Option<(_, &mut T)>` are eliminated.
2+
3+ //@ compile-flags: -Copt-level=3
4+
5+ #![ crate_type = "lib" ]
6+
7+ type T = [ u64 ; 4 ] ;
8+
9+ // CHECK-LABEL: @f0(
10+ #[ no_mangle]
11+ pub fn f0 ( stack : & mut Stack , f : fn ( & T ) ) -> bool {
12+ // CHECK-NOT: icmp eq ptr.*null.*
13+ f_impl :: < 0 > ( stack, f)
14+ }
15+
16+ // CHECK-LABEL: @f1(
17+ #[ no_mangle]
18+ pub fn f1 ( stack : & mut Stack , f : fn ( & T ) ) -> bool {
19+ // CHECK-NOT: icmp eq ptr.*null.*
20+ f_impl :: < 1 > ( stack, f)
21+ }
22+
23+ // CHECK-LABEL: @f2(
24+ #[ no_mangle]
25+ pub fn f2 ( stack : & mut Stack , f : fn ( & T ) ) -> bool {
26+ // CHECK-NOT: icmp eq ptr.*null.*
27+ f_impl :: < 2 > ( stack, f)
28+ }
29+
30+ #[ inline( always) ]
31+ fn f_impl < const N : usize > ( stack : & mut Stack , f : fn ( & T ) ) -> bool {
32+ let Some ( ( a, b) ) = stack. popn_top :: < N > ( ) else {
33+ return false ;
34+ } ;
35+ a. iter ( ) . for_each ( f) ;
36+ f ( b) ;
37+ true
38+ }
39+
40+ pub struct Stack {
41+ data : Vec < T > ,
42+ }
43+
44+ impl Stack {
45+ #[ inline]
46+ fn popn_top < const N : usize > ( & mut self ) -> Option < ( [ T ; N ] , & mut T ) > {
47+ if self . data . len ( ) < N + 1 {
48+ return None ;
49+ }
50+ unsafe { Some ( ( self . popn_unchecked ( ) , self . top_unchecked ( ) ) ) }
51+ }
52+
53+ unsafe fn popn_unchecked < const N : usize > ( & mut self ) -> [ T ; N ] {
54+ core:: array:: from_fn ( |_| unsafe { self . pop_unchecked ( ) } )
55+ }
56+
57+ unsafe fn pop_unchecked ( & mut self ) -> T {
58+ self . data . pop ( ) . unwrap_unchecked ( )
59+ }
60+
61+ unsafe fn top_unchecked ( & mut self ) -> & mut T {
62+ self . data . last_mut ( ) . unwrap_unchecked ( )
63+ }
64+ }
You can’t perform that action at this time.
0 commit comments