@@ -1639,7 +1639,6 @@ fn handle_inlined_functions_helper(
16391639}
16401640
16411641fn handle_const_arguments ( program : & mut Program ) {
1642- // TODO this doesnt suupport const functions calling other const functions
16431642 let mut new_functions = BTreeMap :: < String , Function > :: new ( ) ;
16441643 let constant_functions = program
16451644 . functions
@@ -1648,9 +1647,45 @@ fn handle_const_arguments(program: &mut Program) {
16481647 . map ( |( name, func) | ( name. clone ( ) , func. clone ( ) ) )
16491648 . collect :: < BTreeMap < _ , _ > > ( ) ;
16501649
1650+ // First pass: process non-const functions that call const functions
16511651 for func in program. functions . values_mut ( ) {
1652- handle_const_arguments_helper ( & mut func. body , & constant_functions, & mut new_functions) ;
1652+ if !func. has_const_arguments ( ) {
1653+ handle_const_arguments_helper ( & mut func. body , & constant_functions, & mut new_functions) ;
1654+ }
16531655 }
1656+
1657+ // Process newly created const functions recursively until no more changes
1658+ let mut changed = true ;
1659+ while changed {
1660+ changed = false ;
1661+ let mut additional_functions = BTreeMap :: new ( ) ;
1662+
1663+ // Collect all function names to process
1664+ let function_names: Vec < String > = new_functions. keys ( ) . cloned ( ) . collect ( ) ;
1665+
1666+ for name in function_names {
1667+ if let Some ( func) = new_functions. get_mut ( & name) {
1668+ let initial_count = additional_functions. len ( ) ;
1669+ handle_const_arguments_helper (
1670+ & mut func. body ,
1671+ & constant_functions,
1672+ & mut additional_functions,
1673+ ) ;
1674+ if additional_functions. len ( ) > initial_count {
1675+ changed = true ;
1676+ }
1677+ }
1678+ }
1679+
1680+ // Add any newly discovered functions
1681+ for ( name, func) in additional_functions {
1682+ if let std:: collections:: btree_map:: Entry :: Vacant ( e) = new_functions. entry ( name) {
1683+ e. insert ( func) ;
1684+ changed = true ;
1685+ }
1686+ }
1687+ }
1688+
16541689 for ( name, func) in new_functions {
16551690 assert ! ( !program. functions. contains_key( & name) , ) ;
16561691 program. functions . insert ( name, func) ;
0 commit comments