refactor(mir): Extract MetadataContext from MirBuilder (Phase 136 follow-up 6/7)

## Summary
Extracted metadata and debug information management into dedicated
MetadataContext struct, completing step 6 of 7 in the Context Box
refactoring plan.

## Changes
- NEW: src/mir/builder/metadata_context.rs (MetadataContext struct + helpers)
- Modified: src/mir/builder.rs (added metadata_ctx field + sync helpers)
- Modified: src/mir/hints.rs (added Clone + Debug derives to HintSink)
- Updated: phase-136-context-box-progress.md (6/7 progress)

## Extracted Fields
- current_span: Span → metadata_ctx.current_span
- source_file: Option<String> → metadata_ctx.source_file
- hint_sink: HintSink → metadata_ctx.hint_sink
- current_region_stack: Vec<RegionId> → metadata_ctx.current_region_stack

## Key Features
- Zero-cost type inference hints (HintSink)
- Source position tracking for error messages
- Region observation stack for debug builds
- Phase 135 contract_checks integration

## Tests
- cargo test --release --lib: 1019/1023 passed (4 pre-existing failures)
- phase135_trim_mir_verify.sh: PASS
- Backward compatibility: 100% maintained (deprecated fields synced)

## Progress
Phase 136 Context Extraction: 6/7 complete (86%)
-  Step 1: TypeContext
-  Step 2: CoreContext
-  Step 3: ScopeContext
-  Step 4: BindingContext
-  Step 5: VariableContext
-  Step 6: MetadataContext (this commit)
-  Step 7: CompilationContext

🤖 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-15 22:03:34 +09:00
parent ee2915a6b2
commit 903ab8ef4c
4 changed files with 291 additions and 27 deletions

View File

@ -4,7 +4,7 @@
builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性・テスト容易性を向上させる段階的リファクタリング。
## 完了した Context (5/7)
## 完了した Context (6/7)
### ✅ TypeContext (Step 1) - 完了
@ -166,15 +166,48 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
- JoinIR lowering で `CarrierInfo::from_variable_map()` を使用
- 段階的移行により破壊的変更なし
**コミット**: ee2915a6
### ✅ MetadataContext (Step 6) - 完了
**実装日**: 2025-12-15
**抽出したフィールド** (4個):
- `current_span: Span` - 現在の AST span (命令アノテーション用)
- `source_file: Option<String>` - ソースファイルヒント (メタデータ用)
- `hint_sink: HintSink` - 型推論ヒント (ゼロコストガイダンス)
- `current_region_stack: Vec<RegionId>` - Region 観測用スタック (NYASH_REGION_TRACE=1 デバッグ用)
**ファイル**:
- `/home/tomoaki/git/hakorune-selfhost/src/mir/builder/metadata_context.rs` (新規作成)
- `/home/tomoaki/git/hakorune-selfhost/src/mir/hints.rs` (HintSink に Clone, Debug 追加)
**統合方法**:
- `MirBuilder``metadata_ctx: MetadataContext` フィールドを追加
- 既存フィールドは `#[deprecated]` でマーク(後方互換性維持)
- 同期ヘルパー (`sync_metadata_ctx_to_legacy()`, `sync_legacy_to_metadata_ctx()`) を実装
- Hint メソッド (`hint_scope_enter()`, `hint_scope_leave()`, `hint_join_result()`) が metadata_ctx を SSOT として使用し、legacy フィールドを同期
- Source file メソッド (`set_source_file_hint()`, `clear_source_file_hint()`, `current_source_file()`) を metadata_ctx 経由に更新
- Span 使用箇所 (`add_instruction_with_span()`) を metadata_ctx.current_span() 経由に更新
**特徴**:
- **HintSink**: 型推論最適化への将来対応 (現在は no-op デフォルト)
- **Span**: 命令単位で保持され、エラー報告・デバッグ情報生成に使用
- **source_file**: 関数メタデータに伝播 (FunctionMetadata.source_file)
- **current_region_stack**: 開発用トレース専用 (本番コストゼロ)
**テスト結果**:
-`cargo build --release` 成功 (381 warnings - deprecated フィールド使用)
-`cargo test --release --lib` - 1019/1023 PASS (4 tests 失敗は既存問題)
-`phase135_trim_mir_verify.sh` - PASS
**影響範囲**:
- builder 内で current_span, source_file, hint_sink, current_region_stack を使用中のコードを metadata_ctx 経由に移行
- 段階的移行により破壊的変更なし
**コミット**: [今回のコミット]
## 残りの Context (2/7)
### 5. MetadataContext (計画中)
- `hint_sink: HintSink` - ヒントシンク (zero-cost guidance)
- `current_span: Span` - 現在の AST スパン
- `source_file: Option<String>` - ソースファイルヒント
- `current_region_stack: Vec<RegionId>` - Region 観測スタック
## 残りの Context (1/7)
### 6. CompilationContext (計画中)
- `compilation_context: Option<BoxCompilationContext>` - Box コンパイルコンテキスト
@ -182,9 +215,6 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
- `user_defined_boxes: HashSet<String>` - ユーザー定義 Box 名
- `reserved_value_ids: HashSet<ValueId>` - 予約済み ValueId
### 7. ResourceContext (将来予定)
- リソース管理関連フィールド (将来追加)
## 設計原則
1. **段階的移行** - 全フィールドを一度に移行せず、1-2 Context ずつ
@ -194,9 +224,9 @@ builder.rs の 1219 行を責任ごとに Context Box に分割し、保守性
## 次のステップ
**優先順位 6**: MetadataContext 抽出
- ヒントシンク・スパン・リージョン管理の集約
- コンパイル時メタデータの統一管理
**優先順位 7**: CompilationContext 抽出 (最終ステップ)
- Box コンパイルコンテキストの集約
- 静的 Box 管理・予約済み ValueId の統一管理
## 参考資料