@@ -165,16 +165,21 @@ impl ConstantPool {
165165 . expect ( "constant key canonicalization only fails on allocation error" )
166166 }
167167
168+ /// Index of an already-stored constant equal to `constant`, if any.
169+ /// _PyCode_ConstantKey() keeps NaN-bearing constants distinct because
170+ /// Python-level NaN keys do not compare equal.
171+ fn find_existing ( & self , constant : & ConstantData ) -> Option < usize > {
172+ if Self :: constant_contains_nan ( constant) {
173+ return None ;
174+ }
175+ self . constants
176+ . iter ( )
177+ . position ( |existing| Self :: constant_key_eq ( existing, constant) )
178+ }
179+
168180 pub fn insert_full ( & mut self , constant : ConstantData ) -> ( usize , bool ) {
169181 let constant = Self :: canonicalize_constant_key_infallible ( constant) ;
170- // CPython's _PyCode_ConstantKey() keeps NaN-bearing constants distinct
171- // because Python-level NaN keys do not compare equal.
172- if !Self :: constant_contains_nan ( & constant)
173- && let Some ( idx) = self
174- . constants
175- . iter ( )
176- . position ( |existing| Self :: constant_key_eq ( existing, & constant) )
177- {
182+ if let Some ( idx) = self . find_existing ( & constant) {
178183 return ( idx, false ) ;
179184 }
180185 let idx = self . constants . len ( ) ;
@@ -184,14 +189,7 @@ impl ConstantPool {
184189
185190 fn try_insert_full ( & mut self , constant : ConstantData ) -> crate :: InternalResult < ( usize , bool ) > {
186191 let constant = Self :: canonicalize_constant_key ( constant) ?;
187- // CPython's _PyCode_ConstantKey() keeps NaN-bearing constants distinct
188- // because Python-level NaN keys do not compare equal.
189- if !Self :: constant_contains_nan ( & constant)
190- && let Some ( idx) = self
191- . constants
192- . iter ( )
193- . position ( |existing| Self :: constant_key_eq ( existing, & constant) )
194- {
192+ if let Some ( idx) = self . find_existing ( & constant) {
195193 return Ok ( ( idx, false ) ) ;
196194 }
197195 self . constants
@@ -6623,21 +6621,19 @@ fn get_max_label(blocks: &Blocks) -> i32 {
66236621}
66246622
66256623/// flowgraph.c make_except_stack
6626- #[ allow( clippy:: unnecessary_wraps) ]
6627- fn make_except_stack ( ) -> crate :: InternalResult < CfgExceptStack > {
6624+ fn make_except_stack ( ) -> CfgExceptStack {
66286625 let handlers = [ BlockIdx :: NULL ; CO_MAXBLOCKS + 2 ] ;
66296626 debug_assert_eq ! ( handlers[ 0 ] , BlockIdx :: NULL ) ;
6630- Ok ( CfgExceptStack { handlers, depth : 0 } )
6627+ CfgExceptStack { handlers, depth : 0 }
66316628}
66326629
66336630/// flowgraph.c copy_except_stack
6634- #[ allow( clippy:: unnecessary_wraps) ]
6635- fn copy_except_stack ( stack : & CfgExceptStack ) -> crate :: InternalResult < CfgExceptStack > {
6631+ fn copy_except_stack ( stack : & CfgExceptStack ) -> CfgExceptStack {
66366632 debug_assert ! ( stack. depth <= CO_MAXBLOCKS + 1 ) ;
6637- Ok ( CfgExceptStack {
6633+ CfgExceptStack {
66386634 handlers : stack. handlers ,
66396635 depth : stack. depth ,
6640- } )
6636+ }
66416637}
66426638
66436639/// flowgraph.c except_stack_top
@@ -6689,7 +6685,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
66896685
66906686 todo. push ( BlockIdx ( 0 ) ) ;
66916687 blocks[ 0 ] . visited = true ;
6692- blocks[ 0 ] . except_stack = Some ( make_except_stack ( ) ? ) ;
6688+ blocks[ 0 ] . except_stack = Some ( make_except_stack ( ) ) ;
66936689
66946690 while let Some ( block_idx) = todo. pop ( ) {
66956691 let bi = block_idx. idx ( ) ;
@@ -6716,7 +6712,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
67166712 if !blocks[ target] . visited {
67176713 blocks[ target] . except_stack = Some ( copy_except_stack (
67186714 stack. as_ref ( ) . expect ( "active exception stack" ) ,
6719- ) ? ) ;
6715+ ) ) ;
67206716 todo. push ( target) ;
67216717 blocks[ target] . visited = true ;
67226718 }
@@ -6740,7 +6736,7 @@ pub(crate) fn label_exception_targets(blocks: &mut Blocks) -> crate::InternalRes
67406736 if bb_has_fallthrough ( & blocks[ bi] ) {
67416737 blocks[ target] . except_stack = Some ( copy_except_stack (
67426738 stack. as_ref ( ) . expect ( "active exception stack" ) ,
6743- ) ? ) ;
6739+ ) ) ;
67446740 } else {
67456741 blocks[ target] . except_stack = stack. take ( ) ;
67466742 stack_transferred = true ;
@@ -7095,7 +7091,7 @@ mod tests {
70957091
70967092 #[ test]
70977093 fn except_stack_tracks_cpython_depth_and_handler_slots ( ) {
7098- let mut stack = make_except_stack ( ) . unwrap ( ) ;
7094+ let mut stack = make_except_stack ( ) ;
70997095 assert_eq ! ( stack. depth, 0 ) ;
71007096 assert_eq ! ( stack. handlers. len( ) , CO_MAXBLOCKS + 2 ) ;
71017097 assert_eq ! ( stack. handlers[ 0 ] , BlockIdx :: NULL ) ;
@@ -7119,7 +7115,7 @@ mod tests {
71197115 assert ! ( handler. preserve_lasti) ;
71207116 assert ! ( blocks[ 1 ] . preserve_lasti) ;
71217117
7122- let copy = copy_except_stack ( & stack) . unwrap ( ) ;
7118+ let copy = copy_except_stack ( & stack) ;
71237119 assert_eq ! ( copy. depth, stack. depth) ;
71247120 assert_eq ! ( copy. handlers, stack. handlers) ;
71257121
0 commit comments