refactor(joinir): Phase 183-2 Consolidate CarrierInfo initialization
Makes CarrierInfo::from_variable_map() the primary initialization method. Common pattern initializer now delegates to this centralized logic. ## Changes 1. **Primary Method: CarrierInfo::from_variable_map()**: - Now the single source of truth for CarrierInfo construction - Used by both MIR and JoinIR contexts - Documented as primary initialization method (Phase 183-2) 2. **CommonPatternInitializer Refactoring**: - Converted to thin wrapper around `CarrierInfo::from_variable_map()` - Delegates carrier collection to primary method - Only adds pattern-specific exclusion filtering - Reduced code duplication (~30 lines removed) 3. **Documentation Updates**: - `carrier_info.rs`: Added Phase 183-2 section explaining primary role - `common_init.rs`: Documented delegation strategy - Clear separation of concerns between modules 4. **Removed Duplicate Logic**: - Eliminated manual carrier collection in `common_init.rs` - Removed `CarrierVar` import (no longer directly constructed) - Unified sorting and validation in one place ## Benefits - **Single source of truth**: CarrierInfo construction logic in one module - **Consistency**: Same initialization algorithm across MIR/JoinIR - **Maintainability**: Changes to carrier logic only needed once - **Testability**: Primary logic tested in carrier_info module ## Testing ✅ All carrier_info tests pass (7 tests) ✅ All pattern tests pass (124 tests) ✅ No behavioral changes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
//! ## Responsibility
|
||||
//!
|
||||
//! - Extract loop variable from condition AST
|
||||
//! - Build CarrierInfo from variable_map
|
||||
//! - Build CarrierInfo from variable_map (delegates to `CarrierInfo::from_variable_map`)
|
||||
//! - Support pattern-specific carrier exclusions (e.g., Pattern 2 excludes break-triggered vars)
|
||||
//!
|
||||
//! ## Usage
|
||||
@ -26,11 +26,16 @@
|
||||
//! - **Testability**: Can be tested independently
|
||||
//! - **Maintainability**: Changes to initialization only need to happen once
|
||||
//! - **Reduces duplication**: Eliminates 80 lines across Pattern 1-4
|
||||
//!
|
||||
//! # Phase 183-2: Delegation to CarrierInfo
|
||||
//!
|
||||
//! This module is now a thin wrapper around `CarrierInfo::from_variable_map()`.
|
||||
//! The primary logic lives in `carrier_info.rs` for consistency across MIR and JoinIR contexts.
|
||||
|
||||
use crate::mir::builder::MirBuilder;
|
||||
use crate::mir::ValueId;
|
||||
use crate::ast::ASTNode;
|
||||
use crate::mir::join_ir::lowering::carrier_info::{CarrierInfo, CarrierVar};
|
||||
use crate::mir::join_ir::lowering::carrier_info::CarrierInfo;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub struct CommonPatternInitializer;
|
||||
@ -89,30 +94,23 @@ impl CommonPatternInitializer {
|
||||
)
|
||||
})?;
|
||||
|
||||
// Step 2: Collect carriers from variable_map
|
||||
let mut carriers = Vec::new();
|
||||
for (var_name, &var_id) in variable_map {
|
||||
if var_name != &loop_var_name {
|
||||
// Check exclusions (Pattern 2 specific, or other pattern-specific needs)
|
||||
if let Some(excluded) = exclude_carriers {
|
||||
if excluded.contains(&var_name.as_str()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
carriers.push(CarrierVar {
|
||||
name: var_name.clone(),
|
||||
host_id: var_id,
|
||||
join_id: None, // Phase 177-STRUCT-1
|
||||
});
|
||||
}
|
||||
// Phase 183-2: Delegate to CarrierInfo::from_variable_map for consistency
|
||||
// Convert BTreeMap to HashMap for compatibility
|
||||
let mut variable_map_hashmap = std::collections::HashMap::new();
|
||||
for (k, v) in variable_map {
|
||||
variable_map_hashmap.insert(k.clone(), *v);
|
||||
}
|
||||
|
||||
let carrier_info = CarrierInfo {
|
||||
loop_var_name: loop_var_name.clone(),
|
||||
loop_var_id,
|
||||
carriers,
|
||||
trim_helper: None, // Phase 171-C-5: No Trim pattern by default
|
||||
};
|
||||
// Step 2: Use CarrierInfo::from_variable_map as primary initialization method
|
||||
let mut carrier_info = CarrierInfo::from_variable_map(
|
||||
loop_var_name.clone(),
|
||||
&variable_map_hashmap,
|
||||
)?;
|
||||
|
||||
// Step 3: Apply exclusions if provided (Pattern 2 specific)
|
||||
if let Some(excluded) = exclude_carriers {
|
||||
carrier_info.carriers.retain(|c| !excluded.contains(&c.name.as_str()));
|
||||
}
|
||||
|
||||
Ok((loop_var_name, loop_var_id, carrier_info))
|
||||
}
|
||||
|
||||
@ -5,6 +5,15 @@
|
||||
//! without hardcoded variable names or ValueIds.
|
||||
//!
|
||||
//! Phase 193-2: Enhanced builder methods for flexible construction
|
||||
//!
|
||||
//! # Phase 183-2: Primary CarrierInfo Construction
|
||||
//!
|
||||
//! This module is the single source of truth for CarrierInfo initialization.
|
||||
//! Both MIR and JoinIR contexts use `CarrierInfo::from_variable_map()` as the
|
||||
//! primary construction method.
|
||||
//!
|
||||
//! - MIR context: `common_init.rs` delegates to this module
|
||||
//! - JoinIR context: Uses `from_variable_map()` directly
|
||||
|
||||
use crate::mir::ValueId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
Reference in New Issue
Block a user