Files
hakorune/src/tests/mir_loopform_exit_phi.rs
nyash-codex 0f43bc6b53 fix(mir): LoopForm v2完全緑化 - ValueId(0)予約 & unreachable block許容
## 🎯 完了タスク
 Task 1: LoopForm v2 最小ユニットテスト全緑化(4/4パス)
 Task 2: program_v0 PHI trace スクリプト全緑化(5/5パス)
 Task 3: Stage-B 風ループ Rust テスト全緑化(2/2パス)
🔧 Task 4: Stage-1 using resolver (1/3パス、UsingStatement対応完了)

## 📝 主要修正

### 1. ValueId(0)を無効値として予約
- **src/mir/function.rs**: MirFunction::new() で next_value_id を1から開始
- **src/mir/builder/stmts.rs**: build_local_statement で next_value_id() 使用
- **理由**: LoopForm v2 が ValueId(0) を無効値の sentinel として使用
- **効果**: SSA 構築時の ValueId 衝突を完全に防止

### 2. Unreachable block 許容をデフォルト化
- **src/mir/verification/cfg.rs**: 到達可能性チェック削除
- **src/config/env.rs**: NYASH_VERIFY_ALLOW_UNREACHABLE 環境変数削除
- **src/tests/mir_loopform_exit_phi.rs**: 環境変数設定削除
- **理由**: break/continue/return の後の unreachable block は正当
  - switch_to_unreachable_block_with_void() で意図的に作成
  - LLVM IR の `unreachable` 命令と同じ標準的手法
  - 削除は DCE (Dead Code Elimination) パスの仕事
- **効果**: 環境変数を減らしてシンプル化

### 3. UsingStatement の MIR Builder 対応
- **src/mir/builder/exprs.rs**: UsingStatement → void 変換を追加
- **理由**: namespace 解決は parser/runner レベルで完了済み
- **効果**: using 文を含むコードが MIR コンパイル可能に

### 4. スモークテストスクリプト修正
- **tools/smokes/v2/profiles/quick/core/phase2034/*.sh**: 5ファイル
- **修正内容**: 二重コマンド置換のシンタックスエラー修正
  - 誤: `out="$(out="$(COMMAND)"; rc=$?`
  - 正: `out="$(COMMAND)"; rc=$?`

## 🧪 テスト結果
- mir_loopform_exit_phi: 4/4パス 
- program_v0_*_phi_trace_vm: 5/5パス 
- mir_stageb_loop_break_continue: 2/2パス 
- mir_stage1_using_resolver: 1/3パス (残り2つは dominator violation)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 06:11:17 +09:00

192 lines
5.4 KiB
Rust

/*!
* Unit tests for LoopForm v2 exit PHI generation
*
* Tests the build_exit_phis() implementation in loopform_builder.rs
* Focus: predecessor tracking and PHI input generation for break statements
*/
use crate::parser::NyashParser;
use crate::mir::{MirCompiler, MirVerifier};
#[test]
fn test_loopform_exit_phi_single_break() {
// Enable LoopForm PHI v2 and MIR verification
std::env::set_var("NYASH_LOOPFORM_PHI_V2", "1");
std::env::set_var("NYASH_VM_VERIFY_MIR", "1");
std::env::set_var("NYASH_LOOPFORM_DEBUG", "1");
std::env::set_var("NYASH_PARSER_STAGE3", "1");
std::env::set_var("NYASH_PARSER_ALLOW_SEMICOLON", "1");
let src = r#"
static box TestExitPhi {
test() {
local i = 0
loop(i < 10) {
if i == 5 { break }
i = i + 1
}
return i
}
}
"#;
println!("=== Test: Single break statement ===");
// Parse
let ast = NyashParser::parse_from_string(src)
.expect("parse failed");
// Compile
let mut mc = MirCompiler::with_options(false);
let cr = mc.compile(ast).expect("compile failed");
// DEBUG: Dump MIR structure
for (fname, func) in &cr.module.functions {
eprintln!("=== Function: {} ===", fname);
eprintln!("Entry block: {:?}", func.entry_block);
eprintln!("Total blocks: {}", func.blocks.len());
for (bid, block) in &func.blocks {
eprintln!(" Block {:?}: {} instructions, successors={:?}",
bid, block.instructions.len(), block.successors);
if *bid == crate::mir::BasicBlockId(10) {
eprintln!(" BB10 instructions:");
for inst in &block.instructions {
eprintln!(" {:?}", inst);
}
}
}
}
// MIR verification
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for err in &errors {
eprintln!("❌ MIR verification error: {}", err);
}
panic!("❌ MIR verification failed with {} errors", errors.len());
}
println!("✅ MIR verification passed");
}
#[test]
fn test_loopform_exit_phi_multiple_breaks() {
std::env::set_var("NYASH_LOOPFORM_PHI_V2", "1");
std::env::set_var("NYASH_VM_VERIFY_MIR", "1");
std::env::set_var("NYASH_LOOPFORM_DEBUG", "1");
std::env::set_var("NYASH_PARSER_STAGE3", "1");
std::env::set_var("NYASH_PARSER_ALLOW_SEMICOLON", "1");
let src = r#"
static box TestMultiBreak {
test() {
local i = 0
loop(i < 10) {
if i == 3 { break }
if i == 5 { break }
i = i + 1
}
return i
}
}
"#;
println!("=== Test: Multiple break statements ===");
let ast = NyashParser::parse_from_string(src).expect("parse failed");
let mut mc = MirCompiler::with_options(false);
let cr = mc.compile(ast).expect("compile failed");
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for err in &errors {
eprintln!("❌ MIR verification error: {}", err);
}
panic!("❌ MIR verification failed with {} errors", errors.len());
}
println!("✅ MIR verification passed");
}
#[test]
fn test_loopform_exit_phi_nested_if_break() {
std::env::set_var("NYASH_LOOPFORM_PHI_V2", "1");
std::env::set_var("NYASH_VM_VERIFY_MIR", "1");
std::env::set_var("NYASH_LOOPFORM_DEBUG", "1");
std::env::set_var("NYASH_PARSER_STAGE3", "1");
std::env::set_var("NYASH_PARSER_ALLOW_SEMICOLON", "1");
let src = r#"
static box TestNestedBreak {
test() {
local i = 0
local found = 0
loop(i < 10) {
if i > 5 {
if i == 7 {
found = 1
break
}
}
i = i + 1
}
return found
}
}
"#;
println!("=== Test: Nested if with break ===");
let ast = NyashParser::parse_from_string(src).expect("parse failed");
let mut mc = MirCompiler::with_options(false);
let cr = mc.compile(ast).expect("compile failed");
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for err in &errors {
eprintln!("❌ MIR verification error: {}", err);
}
panic!("❌ MIR verification failed with {} errors", errors.len());
}
println!("✅ MIR verification passed");
}
#[test]
fn test_loopform_exit_phi_multiple_vars() {
std::env::set_var("NYASH_LOOPFORM_PHI_V2", "1");
std::env::set_var("NYASH_VM_VERIFY_MIR", "1");
std::env::set_var("NYASH_LOOPFORM_DEBUG", "1");
std::env::set_var("NYASH_PARSER_STAGE3", "1");
std::env::set_var("NYASH_PARSER_ALLOW_SEMICOLON", "1");
let src = r#"
static box TestMultiVars {
test() {
local i = 0
local sum = 0
local product = 1
loop(i < 10) {
if sum > 20 { break }
sum = sum + i
product = product * 2
i = i + 1
}
return sum
}
}
"#;
println!("=== Test: Multiple variables with break ===");
let ast = NyashParser::parse_from_string(src).expect("parse failed");
let mut mc = MirCompiler::with_options(false);
let cr = mc.compile(ast).expect("compile failed");
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for err in &errors {
eprintln!("❌ MIR verification error: {}", err);
}
panic!("❌ MIR verification failed with {} errors", errors.len());
}
println!("✅ MIR verification passed");
}