fix(mir/builder): use function-local ValueId throughout MIR builder

Phase 25.1b: Complete SSA fix - eliminate all global ValueId usage in function contexts.

Root cause: ~75 locations throughout MIR builder were using global value
generator (self.value_gen.next()) instead of function-local allocator
(f.next_value_id()), causing SSA verification failures and runtime
"use of undefined value" errors.

Solution:
- Added next_value_id() helper that automatically chooses correct allocator
- Fixed 19 files with ~75 occurrences of ValueId allocation
- All function-context allocations now use function-local IDs

Files modified:
- src/mir/builder/utils.rs: Added next_value_id() helper, fixed 8 locations
- src/mir/builder/builder_calls.rs: 17 fixes
- src/mir/builder/ops.rs: 8 fixes
- src/mir/builder/stmts.rs: 7 fixes
- src/mir/builder/emission/constant.rs: 6 fixes
- src/mir/builder/rewrite/*.rs: 10 fixes
- + 13 other files

Verification:
- cargo build --release: SUCCESS
- Simple tests with NYASH_VM_VERIFY_MIR=1: Zero undefined errors
- Multi-parameter static methods: All working

Known remaining: ValueId(22) in Stage-B (separate issue to investigate)

🤖 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-17 00:48:18 +09:00
parent 35842503e6
commit eadde8d1dd
59 changed files with 1603 additions and 370 deletions

View File

@ -0,0 +1,48 @@
# Phase 25.1c — Env / Extern / BoxIntrospect Structural Cleanup
Status: planning構造整理フェーズ・挙動は変えない
## ゴール
- `env.*` / `hostbridge.*` / `env.box_introspect.*` の責務と経路を整理し、型システムまわりの「正しい入口」を 1 箇所に揃える。
- Box 型情報 API`env.box_introspect.kind` + `BoxTypeInspectorBox`)を **コア型システム**として扱えるようにするplugins の有無に依存しない)。
- i64 / MapBox / ArrayBox の unwrap ロジックを SSOT に寄せ、MirBuilder / JsonEmit / LoopOpts / BoxHelpers が同じ前提で動くようにする。
## スコープ(何をここで扱うか)
- 対象:
- Rust 側: `extern_registry.rs` / `handlers/externals.rs` / `handlers/extern_provider.rs` / `runtime/plugin_loader_v2/*`
- Hako 側: `BoxTypeInspectorBox` / `BoxHelpers` / `JsonEmitBox` / `MirSchemaBox` / `LoopOptsBox`
- ドキュメント: `docs/specs`env externs / box_introspect / numeric view の設計メモ)
- 非対象:
- 新しい言語機能や VM 命令の追加Phase 25 ポリシーに従い、仕様拡張はしない)。
- MirBuilder の意味論変更multicarrier や LoopForm の設計は Phase 25.1b の範囲に留める)。
## やりたい整理(タスクリスト)
1. **env.* extern の SSOT を決める**
- `env.get` / `env.mirbuilder.emit` / `env.codegen.emit_object` / `env.codegen.link_object` / `env.box_introspect.kind` を一覧化し、仕様引数・戻り値・MIR 形)を `docs/specs/env_externs.md`(仮)に明文化する。
- JSON v0 → MIR ブリッジ(`MapVars::resolve` / loweringで、上記が必ず `ExternCall("env.*", ..)` に落ちることを確認・修正する。
2. **hostbridge.extern_invoke を「互換レイヤ」に押し込める**
- 方針: 「`env.*` で表現できるものは ExternCall を正義とし、`hostbridge.extern_invoke` は互換用ラッパに限定する」。
- Hako 側: `hostbridge.extern_invoke("env.*", ..)` は内部で `env.*` を呼ぶだけにする(新規コードは直接 `env.*` を使う)。
- Rust 側: `"hostbridge.extern_invoke"` の実装は、`extern_provider_dispatch("env.*", ..)` に委譲する薄いブリッジに整理する。
3. **BoxIntrospect をコア型システムに昇格させる**
- `env.box_introspect.kind` の実装を plugin loader v2 直下ではなく、コア runtime例: `runtime/box_introspect.rs`)に寄せる。
- コア型MapBox / ArrayBox / StringBox / IntegerBox / BoolBox / NullBoxは runtime 側で `build_box_info` を定義し、plugin loader は「ユーザー Box の拡張」だけを担当する。
- `BoxTypeInspectorBox``env.box_introspect.kind(value)` を唯一の情報源として扱い、repr ベースの fallback は「plugins も env.* も使えないデバッグ環境のみ」で使うことをコメントで明示する。
4. **Numeric viewi64 unwrapの SSOT 化**
- Hako 側: `string_helpers` / `BoxHelpers` / `MirSchemaBox` / `JsonEmitBox` / `LoopOptsBox` に散っている i64 unwrap ロジックを、小さなユーティリティ(仮: `box_numeric_view.hako`)に寄せる。
- Rust 側: `NyashBox` から i64 を取り出す `as_i64` 的な関数を 1 箇所に置き、extern / BoxIntrospect 経路からはそれを使う。
## 進め方メモ
- 先にドキュメントを書くenv extern / BoxIntrospect / numeric view の仕様を `docs/specs` 配下に整理)→ そのあとで Bridge / VM / Hako を小さく揃える。
- 既存フェーズとの関係:
- Phase 25.1b: selfhost builder / multicarrier / BoxTypeInspector 実装フェーズ(機能側)。
- Phase 25.1c: そのうち「env.* / hostbridge.* / BoxIntrospect の構造と責務」を整理するメタフェーズ(構造側)。
- 挙動を変えないことFailFast / default path は現状維持)を前提に、小さな差分で進める。