From 4cbe412c22347a00a2c29f690689d7dc25e31cfe Mon Sep 17 00:00:00 2001 From: nyash-codex Date: Mon, 8 Dec 2025 18:38:30 +0900 Subject: [PATCH] refactor(joinir): Replace magic numbers with named constants Phase 179-A Step 3: Improve code maintainability by replacing hardcoded magic values with descriptive named constants. Changes: - instruction_rewriter.rs: K_EXIT_FUNC_NAME constant for "join_func_2" - pattern3_with_if_phi.rs: PATTERN3_K_EXIT_SUM_FINAL_ID for ValueId(18) Benefits: - Self-documenting code (names explain the meaning) - Easier to maintain (change in one place) - Prevents typos and inconsistencies --- .../control_flow/joinir/merge/instruction_rewriter.rs | 9 +++++++-- .../control_flow/joinir/patterns/pattern3_with_if_phi.rs | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs b/src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs index 24f79d3c..c2f9c61c 100644 --- a/src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs +++ b/src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs @@ -5,6 +5,7 @@ //! //! Phase 4 Extraction: Separated from merge_joinir_mir_blocks (lines 260-546) //! Phase 33-17: Further modularization - extracted TailCallClassifier and MergeResult +//! Phase 179-A Step 3: Named constants for magic values use crate::mir::{BasicBlock, BasicBlockId, MirInstruction, MirModule, ValueId}; use crate::mir::builder::joinir_id_remapper::JoinIrIdRemapper; @@ -14,6 +15,10 @@ use super::tail_call_classifier::{TailCallKind, classify_tail_call}; use super::merge_result::MergeResult; use std::collections::HashMap; +/// Phase 179-A: Exit continuation function name (MIR convention) +/// This is the standard name for k_exit continuations in JoinIR → MIR lowering +const K_EXIT_FUNC_NAME: &str = "join_func_2"; + /// Phase 4: Merge ALL functions and rewrite instructions /// /// Returns: @@ -88,8 +93,8 @@ pub(super) fn merge_and_rewrite( // Phase 33-15: Identify continuation functions (join_func_2 = k_exit, etc.) // Continuation functions receive values from Jump args, not as independent sources // We should NOT collect their Return values for exit_phi_inputs - // Note: MIR uses "join_func_N" naming, where N=2 is typically k_exit - let is_continuation_func = func_name == "join_func_2" || func_name.ends_with("k_exit"); + // Phase 179-A: Use named constant for k_exit function name + let is_continuation_func = func_name == K_EXIT_FUNC_NAME || func_name.ends_with("k_exit"); if debug { eprintln!( diff --git a/src/mir/builder/control_flow/joinir/patterns/pattern3_with_if_phi.rs b/src/mir/builder/control_flow/joinir/patterns/pattern3_with_if_phi.rs index e6d42357..9634f10c 100644 --- a/src/mir/builder/control_flow/joinir/patterns/pattern3_with_if_phi.rs +++ b/src/mir/builder/control_flow/joinir/patterns/pattern3_with_if_phi.rs @@ -5,6 +5,10 @@ use crate::mir::builder::MirBuilder; use crate::mir::ValueId; use super::super::trace; +/// Phase 179-A: Expected ValueId for k_exit parameter (sum_final) in Pattern 3 +/// This corresponds to the exit PHI input in the JoinIR lowering for loop_with_if_phi_minimal +const PATTERN3_K_EXIT_SUM_FINAL_ID: ValueId = ValueId(18); + /// Phase 194: Detection function for Pattern 3 /// /// Phase 192: Updated to structure-based detection @@ -116,8 +120,8 @@ impl MirBuilder { // The loop variable is handled separately via boundary.loop_var_name LoopExitBinding { carrier_name: "sum".to_string(), - join_exit_value: ValueId(18), // k_exit's parameter (sum_final) - host_slot: sum_var_id, // variable_map["sum"] + join_exit_value: PATTERN3_K_EXIT_SUM_FINAL_ID, // k_exit's parameter (sum_final) + host_slot: sum_var_id, // variable_map["sum"] } ]) .with_loop_var_name(Some(loop_var_name.clone())) // Phase 33-16: Enable header PHI generation for SSA correctness