@@ -43,40 +43,40 @@ pub(crate) mod stack_analysis {
4343 }
4444
4545 impl Kind {
46- fn from_i64 ( v : i64 ) -> Option < Self > {
47- match v {
48- 1 => Some ( Self :: Iterator ) ,
49- 2 => Some ( Self :: Except ) ,
50- 3 => Some ( Self :: Object ) ,
51- 4 => Some ( Self :: Null ) ,
52- 5 => Some ( Self :: Lasti ) ,
53- _ => None ,
54- }
46+ const fn from_i64 ( v : i64 ) -> Option < Self > {
47+ Some ( match v {
48+ 1 => Self :: Iterator ,
49+ 2 => Self :: Except ,
50+ 3 => Self :: Object ,
51+ 4 => Self :: Null ,
52+ 5 => Self :: Lasti ,
53+ _ => return None ,
54+ } )
5555 }
5656 }
5757
58- pub ( crate ) fn push_value ( stack : i64 , kind : i64 ) -> i64 {
58+ pub ( crate ) const fn push_value ( stack : i64 , kind : i64 ) -> i64 {
5959 if ( stack as u64 ) >= WILL_OVERFLOW {
6060 OVERFLOWED
6161 } else {
6262 ( stack << BITS_PER_BLOCK ) | kind
6363 }
6464 }
6565
66- pub ( crate ) fn pop_value ( stack : i64 ) -> i64 {
66+ pub ( crate ) const fn pop_value ( stack : i64 ) -> i64 {
6767 stack >> BITS_PER_BLOCK
6868 }
6969
70- pub ( crate ) fn top_of_stack ( stack : i64 ) -> i64 {
70+ pub ( crate ) const fn top_of_stack ( stack : i64 ) -> i64 {
7171 stack & MASK
7272 }
7373
74- fn peek ( stack : i64 , n : u32 ) -> i64 {
74+ const fn peek ( stack : i64 , n : u32 ) -> i64 {
7575 debug_assert ! ( n >= 1 ) ;
7676 ( stack >> ( BITS_PER_BLOCK * ( n - 1 ) ) ) & MASK
7777 }
7878
79- fn stack_swap ( stack : i64 , n : u32 ) -> i64 {
79+ const fn stack_swap ( stack : i64 , n : u32 ) -> i64 {
8080 debug_assert ! ( n >= 1 ) ;
8181 let to_swap = peek ( stack, n) ;
8282 let top = top_of_stack ( stack) ;
@@ -85,7 +85,7 @@ pub(crate) mod stack_analysis {
8585 ( replaced_low & !MASK ) | to_swap
8686 }
8787
88- fn pop_to_level ( mut stack : i64 , level : u32 ) -> i64 {
88+ const fn pop_to_level ( mut stack : i64 , level : u32 ) -> i64 {
8989 if level == 0 {
9090 return EMPTY_STACK ;
9191 }
@@ -97,20 +97,21 @@ pub(crate) mod stack_analysis {
9797 stack
9898 }
9999
100- fn compatible_kind ( from : i64 , to : i64 ) -> bool {
100+ #[ must_use]
101+ const fn compatible_kind ( from : i64 , to : i64 ) -> bool {
101102 if to == 0 {
102- return false ;
103- }
104- if to == Kind :: Object as i64 {
105- return from != Kind :: Null as i64 ;
106- }
107- if to == Kind :: Null as i64 {
108- return true ;
103+ false
104+ } else if to == Kind :: Object as i64 {
105+ from != Kind :: Null as i64
106+ } else if to == Kind :: Null as i64 {
107+ true
108+ } else {
109+ from == to
109110 }
110- from == to
111111 }
112112
113- pub ( crate ) fn compatible_stack ( from_stack : i64 , to_stack : i64 ) -> bool {
113+ #[ must_use]
114+ pub ( crate ) const fn compatible_stack ( from_stack : i64 , to_stack : i64 ) -> bool {
114115 if from_stack < 0 || to_stack < 0 {
115116 return false ;
116117 }
@@ -131,14 +132,17 @@ pub(crate) mod stack_analysis {
131132 to == 0
132133 }
133134
134- pub ( crate ) fn explain_incompatible_stack ( to_stack : i64 ) -> & ' static str {
135+ pub ( crate ) const fn explain_incompatible_stack ( to_stack : i64 ) -> & ' static str {
135136 debug_assert ! ( to_stack != 0 ) ;
137+
136138 if to_stack == OVERFLOWED {
137139 return "stack is too deep to analyze" ;
138140 }
141+
139142 if to_stack == UNINITIALIZED {
140143 return "can't jump into an exception handler, or code may be unreachable" ;
141144 }
145+
142146 match Kind :: from_i64 ( top_of_stack ( to_stack) ) {
143147 Some ( Kind :: Except ) => "can't jump into an 'except' block as there's no exception" ,
144148 Some ( Kind :: Lasti ) => "can't jump into a re-raising block as there's no location" ,
0 commit comments