refactor(joinir): Phase 223 cleanup - impl consolidation & comment cleanup

Improves code organization and readability after Phase 223-3 implementation.

## Task R-1: impl Block Consolidation

**loop_body_cond_promoter.rs**:
- Merged 2 separate impl blocks into single unified impl
- Added section headers for better organization:
  - Public API (extract_continue_condition, try_promote_for_condition)
  - Private Helpers (contains_continue)
- Improved logical flow and discoverability

## Task R-2: Phase Comment Cleanup

**pattern4_with_continue.rs**:
- Simplified Phase 223-3 comments (removed redundant phase numbers)
- Added concise section header explaining LoopBodyLocal promotion
- Clarified inline comments for better understanding
- Maintained implementation intent while reducing noise

## Impact

- No functional changes
- All tests PASS (5 cond_promoter tests, 8 pattern4 tests)
- Better code organization for future maintenance
- Cleaner git history for Phase 223

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-10 15:07:08 +09:00
parent 89a769198a
commit 9b3d2bf001
3 changed files with 250 additions and 39 deletions

View File

@ -209,16 +209,18 @@ impl MirBuilder {
// Phase 195: Use unified trace
trace::trace().varmap("pattern4_start", &self.variable_map);
// Phase 223-3: LoopBodyCondPromoter integration
// Check for LoopBodyLocal in continue condition and attempt promotion
// Phase 223-3: LoopBodyLocal Condition Promotion
//
// Check for LoopBodyLocal in loop/continue conditions and attempt promotion.
// Safe Trim patterns (Category A-3: skip_whitespace) are promoted to carriers.
{
use crate::mir::loop_pattern_detection::loop_condition_scope::LoopConditionScopeBox;
use crate::mir::loop_pattern_detection::loop_body_cond_promoter::{
LoopBodyCondPromoter, ConditionPromotionRequest, ConditionPromotionResult,
};
// Phase 223-3: Extract continue condition from body (if present)
// This handles skip_whitespace pattern: if ch == " " || ... { continue }
// Extract continue condition from body (if present)
// Handles skip_whitespace pattern: if ch == " " || ... { continue }
let continue_cond = LoopBodyCondPromoter::extract_continue_condition(body_to_analyze);
// Analyze both header condition and continue condition for LoopBodyLocal
@ -235,7 +237,7 @@ impl MirBuilder {
);
if cond_scope.has_loop_body_local() {
// Phase 223-3: Try promotion using LoopBodyCondPromoter
// Try promotion using LoopBodyCondPromoter
let promotion_req = ConditionPromotionRequest {
loop_param_name: &loop_var_name,
cond_scope: &cond_scope,
@ -256,7 +258,7 @@ impl MirBuilder {
promoted_var, carrier_name
);
// Phase 223-3: Merge promoted carrier into existing CarrierInfo
// Merge promoted carrier into existing CarrierInfo
carrier_info.merge_from(&promoted_carrier);
eprintln!(
@ -265,7 +267,7 @@ impl MirBuilder {
carrier_info.carrier_count()
);
// Phase 223-3: Check if this is a safe Trim pattern
// Check if this is a safe Trim pattern
if let Some(helper) = carrier_info.trim_helper() {
if helper.is_safe_trim() {
eprintln!(
@ -275,8 +277,7 @@ impl MirBuilder {
"[pattern4/cond_promoter] Carrier: '{}', original var: '{}', whitespace chars: {:?}",
helper.carrier_name, helper.original_var, helper.whitespace_chars
);
// Phase 223-3: Continue with Pattern4 lowering
// (fall through to normal lowering path)
// Continue with Pattern4 lowering (fall through)
} else {
return Err(format!(
"[cf_loop/pattern4] Trim pattern detected but not safe: carrier='{}', whitespace_count={}",
@ -285,11 +286,10 @@ impl MirBuilder {
));
}
}
// Phase 223-3: Continue with normal Pattern4 lowering
// The carrier has been promoted and merged, lowering can proceed
// Carrier promoted and merged, proceed with normal lowering
}
ConditionPromotionResult::CannotPromote { reason, vars } => {
// Phase 223-3: Fail-Fast on promotion failure
// Fail-Fast on promotion failure
return Err(format!(
"[cf_loop/pattern4] Cannot promote LoopBodyLocal variables {:?}: {}",
vars, reason

View File

@ -85,6 +85,10 @@ pub enum ConditionPromotionResult {
pub struct LoopBodyCondPromoter;
impl LoopBodyCondPromoter {
// ========================================================================
// Public API
// ========================================================================
/// Extract continue condition from loop body
///
/// Finds the first if statement with continue in then-branch and returns its condition.
@ -123,33 +127,6 @@ impl LoopBodyCondPromoter {
None
}
/// Check if statements contain a continue statement
fn contains_continue(stmts: &[ASTNode]) -> bool {
for stmt in stmts {
match stmt {
ASTNode::Continue { .. } => return true,
ASTNode::If {
then_body,
else_body,
..
} => {
if Self::contains_continue(then_body) {
return true;
}
if let Some(else_stmts) = else_body {
if Self::contains_continue(else_stmts) {
return true;
}
}
}
_ => {}
}
}
false
}
}
impl LoopBodyCondPromoter {
/// Try to promote LoopBodyLocal variables in conditions
///
/// ## P0 Requirements (Category A-3)
@ -223,6 +200,35 @@ impl LoopBodyCondPromoter {
}
}
}
// ========================================================================
// Private Helpers
// ========================================================================
/// Check if statements contain a continue statement
fn contains_continue(stmts: &[ASTNode]) -> bool {
for stmt in stmts {
match stmt {
ASTNode::Continue { .. } => return true,
ASTNode::If {
then_body,
else_body,
..
} => {
if Self::contains_continue(then_body) {
return true;
}
if let Some(else_stmts) = else_body {
if Self::contains_continue(else_stmts) {
return true;
}
}
}
_ => {}
}
}
false
}
}
#[cfg(test)]