diff --git a/src/mir/function.rs b/src/mir/function.rs index b7062b0b..d49160f6 100644 --- a/src/mir/function.rs +++ b/src/mir/function.rs @@ -77,15 +77,36 @@ impl MirFunction { let mut blocks = HashMap::new(); blocks.insert(entry_block, BasicBlock::new(entry_block)); - // 📦 Hotfix 1: Reserve ValueIds for function parameters at creation time + // 📦 Hotfix 1 + 4: Reserve ValueIds for function parameters at creation time // Parameter ValueIds are %0, %1, ..., %N-1 where N = signature.params.len() - // next_value_id must start at N to avoid overwriting parameters + // + // 📦 Hotfix 4: Static method receiver reservation + // Static methods (e.g., Stage1UsingResolverFull.collect_entries/1) have implicit 'me' receiver + // that's not included in signature.params but needs ValueId reservation. + // + // Detection: If function name contains "." (box method or static method) AND + // params[0] is NOT a Box type → static method → needs +1 for receiver let param_count = signature.params.len() as u32; - let initial_counter = param_count.max(1); // At least 1 to reserve ValueId(0) as sentinel + let has_implicit_receiver = if signature.name.contains('.') { + // Box method or static method + if signature.params.is_empty() { + // No params → static method with only receiver + true + } else { + // Check if first param is Box type (instance method) or not (static method) + !matches!(signature.params[0], MirType::Box(_)) + } + } else { + false + }; + + let receiver_count = if has_implicit_receiver { 1 } else { 0 }; + let total_value_ids = param_count + receiver_count; + let initial_counter = total_value_ids.max(1); // At least 1 to reserve ValueId(0) as sentinel if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { - eprintln!("[MirFunction::new] fn='{}' params={}, initial_counter={}", - signature.name, param_count, initial_counter); + eprintln!("[MirFunction::new] fn='{}' params={}, receiver={}, total={}, initial_counter={}", + signature.name, param_count, receiver_count, total_value_ids, initial_counter); } Self {