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>
This commit is contained in:
nyash-codex
2025-11-18 06:11:17 +09:00
parent f92779cfe8
commit 0f43bc6b53
11 changed files with 203 additions and 88 deletions

View File

@ -40,6 +40,23 @@ static box TestExitPhi {
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) {

View File

@ -1,10 +1,13 @@
use crate::ast::ASTNode;
use crate::mir::{MirCompiler, MirVerifier};
use crate::mir::printer::MirPrinter;
use crate::parser::NyashParser;
fn ensure_stage3_env() {
std::env::set_var("NYASH_PARSER_STAGE3", "1");
std::env::set_var("NYASH_PARSER_ALLOW_SEMICOLON", "1");
std::env::set_var("NYASH_ENABLE_USING", "1");
std::env::set_var("HAKO_ENABLE_USING", "1");
}
/// Minimal Stage1 using resolver harness resembling Stage1UsingResolverBox.resolve_for_source.
@ -68,12 +71,116 @@ static box Stage1UsingResolverMini {
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for e in &errors {
eprintln!("[mir-verify] {}", e);
eprintln!("[rust-mir-verify] {}", e);
}
panic!("MIR verification failed for Stage1UsingResolverMini");
}
}
/// Full-featured Stage1UsingResolverBox._collect_using_entries test with obj_end/path_idx logic.
/// This more closely resembles the actual implementation in lang/src/compiler/entry/using_resolver_box.hako.
/// Tests complex loop with nested conditions and `me` receiver usage without external using dependencies.
#[test]
fn mir_stage1_using_resolver_full_collect_entries_verifies() {
ensure_stage3_env();
// Use LoopForm PHI v2 for this test to exercise the new SSOT経路
std::env::set_var("NYASH_LOOPFORM_PHI_V2", "1");
let src = r#"
static box Stage1UsingResolverFull {
// Simplified helper to find substring index (replaces JsonFragBox.index_of_from)
_find_from(text, pattern, start_pos) {
local text_len = text.length()
local pattern_len = pattern.length()
local i = start_pos
loop(i < text_len) {
if i + pattern_len > text_len { return -1 }
local matches = 1
local j = 0
loop(j < pattern_len) {
local text_ch = text.substring(i + j, i + j + 1)
local pat_ch = pattern.substring(j, j + 1)
if text_ch != pat_ch {
matches = 0
break
}
j = j + 1
}
if matches == 1 { return i }
i = i + 1
}
return -1
}
// Simplified helper to read string after quote (replaces JsonFragBox.read_string_after)
_read_string_after(text, start_pos) {
local text_len = text.length()
local i = start_pos
local result = ""
loop(i < text_len) {
local ch = text.substring(i, i + 1)
if ch == "\"" { break }
result = result + ch
i = i + 1
}
return result
}
collect_entries(src_unused) {
// Simulate realistic JSON with both name and optional path
local json = "[{\"name\":\"A\",\"path\":\"x\"},{\"name\":\"B\"}]"
local out = new ArrayBox()
local pos = 0
local n = json.length()
loop(pos < n) {
local name_idx = me._find_from(json, "\"name\":\"", pos)
if name_idx < 0 { break }
local name = me._read_string_after(json, name_idx + 8)
local obj_end = me._find_from(json, "}", name_idx)
if obj_end < 0 { obj_end = n }
local path = null
local path_idx = me._find_from(json, "\"path\":\"", name_idx)
if path_idx >= 0 && path_idx < obj_end {
path = me._read_string_after(json, path_idx + 8)
}
local entry = new MapBox()
entry.set("name", name)
if path != null { entry.set("path", path) }
out.push(entry)
pos = obj_end + 1
}
return out
}
main() {
local entries = me.collect_entries("")
if entries == null { return 0 }
return entries.length()
}
}
"#;
let ast: ASTNode = NyashParser::parse_from_string(src).expect("parse ok");
let mut mc = MirCompiler::with_options(false);
let cr = mc.compile(ast).expect("compile");
// Dump MIR for analysis
let printer = MirPrinter::verbose();
let mir_output = printer.print_module(&cr.module);
println!("=== MIR Dump ===");
println!("{}", mir_output);
println!("=== End MIR Dump ===");
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for e in &errors {
eprintln!("[rust-mir-verify] {}", e);
}
panic!("MIR verification failed for Stage1UsingResolverFull");
}
}
/// Verify MIR/SSA for ParserBox.parse_program2 in isolation by compiling a small wrapper.
#[test]
fn mir_parserbox_parse_program2_harness_parses_minimal_source() {
@ -99,7 +206,7 @@ static box ParserBoxHarness {
let mut verifier = MirVerifier::new();
if let Err(errors) = verifier.verify_module(&cr.module) {
for e in &errors {
eprintln!("[mir-verify] {}", e);
eprintln!("[rust-mir-verify] {}", e);
}
panic!("MIR verification failed for ParserBoxHarness");
}