feat(mir): Phase 137-5 - Decision Policy SSOT化完了

## 目的
Canonicalizer の RoutingDecision.chosen を「lowerer 選択の最終結果」にする
(構造クラス名ではなく ExitContract ベースの決定)

## 実装内容

### 1. Canonicalizer の決定ロジック修正
- `src/mir/loop_canonicalizer/mod.rs`
  - `skip_whitespace` パターン認識で ExitContract (has_break=true) を考慮
  - Pattern3IfPhi → Pattern2Break に修正(構造は似ているが break あり)
  - 単体テスト更新(Pattern2Break 期待に変更)

### 2. Parity 検証テスト修正
- `src/mir/builder/control_flow/joinir/routing.rs`
  - `test_parity_check_mismatch_detected` → `test_parity_check_skip_whitespace_match`
  - Canonicalizer と Router の一致を検証(ミスマッチ検出からマッチ検証へ)
  - Phase 137-5 の SSOT 原則を反映

### 3. ドキュメント更新
- `docs/development/current/main/design/loop-canonicalizer.md`
  - Phase 137-5: Decision Policy SSOT セクション追加
  - ExitContract 優先の原則を明記
  - skip_whitespace の例を追加

- `docs/development/current/main/phases/phase-137/README.md`
  - Phase 4 完了マーク追加
  - Phase 5 完了セクション追加(実装・検証・効果)

## 検証結果
-  単体テスト: `cargo test --release --lib loop_canonicalizer::tests` (11/11 passed)
-  Parity テスト: `cargo test --release --lib 'routing::tests::test_parity'` (2/2 passed)
-  Strict モード: `HAKO_JOINIR_STRICT=1` で skip_whitespace parity OK

## 効果
- Router と Canonicalizer の pattern 選択が一致
- ExitContract が pattern 決定の SSOT として明確化
- 構造的特徴(if-else 等)は notes に記録(将来拡張に備える)

🤖 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-16 06:17:03 +09:00
parent 9ea15e8417
commit 58f66e3fa2
4 changed files with 61 additions and 15 deletions

View File

@ -351,7 +351,7 @@ mod tests {
}
#[test]
fn test_parity_check_mismatch_detected() {
fn test_parity_check_skip_whitespace_match() {
use crate::mir::loop_canonicalizer::canonicalize_loop_expr;
use crate::mir::builder::control_flow::joinir::patterns::ast_feature_extractor as ast_features;
@ -373,18 +373,20 @@ mod tests {
let features = ast_features::extract_features(condition, body, has_continue, has_break);
let actual_pattern = crate::mir::loop_pattern_detection::classify(&features);
// Verify mismatch
// Canonicalizer: Pattern3IfPhi (recognizes if-else structure)
// Router: Pattern2Break (sees has_break)
// Phase 137-5: Verify MATCH (ExitContract policy fix)
// Both canonicalizer and router should agree on Pattern2Break
// because has_break=true (ExitContract determines pattern choice)
assert_eq!(
canonical_pattern,
crate::mir::loop_pattern_detection::LoopPatternKind::Pattern3IfPhi
crate::mir::loop_pattern_detection::LoopPatternKind::Pattern2Break,
"Canonicalizer should choose Pattern2Break for has_break=true"
);
assert_eq!(
actual_pattern,
crate::mir::loop_pattern_detection::LoopPatternKind::Pattern2Break
crate::mir::loop_pattern_detection::LoopPatternKind::Pattern2Break,
"Router should classify as Pattern2Break for has_break=true"
);
assert_ne!(canonical_pattern, actual_pattern, "Phase 137-4: This test verifies mismatch detection");
assert_eq!(canonical_pattern, actual_pattern, "Phase 137-5: Canonicalizer and router should agree (SSOT policy)");
}
#[test]