## 修正内容
### Core MIR/PHI (5ファイル)
- builder.rs: variable_map, value_types, value_origin_newbox
- context.rs: 3つのマップ
- loop_builder.rs: 3箇所
- loop_snapshot_manager.rs: snapshot マップ
- loop_snapshot_merge.rs: 2箇所
### MIR関連 (4ファイル)
- function.rs: FunctionMetadata.value_types
- resolver.rs: CalleeResolverBox
- guard.rs: CalleeGuardBox
- loop_common.rs: apply_increment_before_continue
### JSON Bridge (5ファイル)
- json_v0_bridge/lowering.rs
- json_v0_bridge/lowering/expr.rs
- json_v0_bridge/lowering/if_else.rs
- json_v0_bridge/lowering/merge.rs
- json_v0_bridge/lowering/try_catch.rs
- json_v0_bridge/mod.rs
### Printer & Providers (4ファイル)
- printer.rs, printer_helpers.rs
- host_providers/mir_builder.rs
- backend/mir_interpreter/handlers/extern_provider.rs
### Tests (3ファイル)
- phi_core/conservative.rs
- tests/json_program_loop.rs
- tests/mir_stage1_using_resolver_verify.rs (2テスト有効化)
## テスト結果
- mir_stage1_using_resolver_resolve_with_modules_map_verifies: 80%成功率
- 完全な決定性は未達成 (HashMap 86箇所、HashSet 63箇所が残存)
🐱 Generated with Claude Code
104 lines
3.5 KiB
Rust
104 lines
3.5 KiB
Rust
//! BoxCompilationContext - 箱理論による静的Box コンパイル時のコンテキスト分離
|
||
//!
|
||
//! 設計原則:
|
||
//! - 各static boxコンパイルごとに独立したコンテキストを作成
|
||
//! - グローバル状態への依存を排除し、汚染を構造的に不可能にする
|
||
//! - コンテキストのライフタイムでリソース管理を自動化
|
||
|
||
use crate::mir::{MirType, ValueId};
|
||
use std::collections::BTreeMap; // Phase 25.1: 決定性確保
|
||
|
||
/// 静的Boxコンパイル時のコンテキスト
|
||
///
|
||
/// 箱理論の原則に従い、各static boxのコンパイルは独立したコンテキストで実行されます。
|
||
/// これにより、using文や前のboxからのメタデータ汚染を構造的に防止します。
|
||
///
|
||
/// # 使用例
|
||
/// ```rust,ignore
|
||
/// let mut ctx = BoxCompilationContext::new();
|
||
/// // ctx を使ってメソッドをコンパイル
|
||
/// // スコープを抜けると自動的にクリーンアップ
|
||
/// ```
|
||
#[derive(Debug, Clone, Default)]
|
||
pub struct BoxCompilationContext {
|
||
/// 変数名 → ValueId マッピング
|
||
/// 例: "args" → ValueId(0), "result" → ValueId(42)
|
||
/// Phase 25.1: HashMap → BTreeMap(PHI生成の決定性確保)
|
||
#[allow(dead_code)]
|
||
pub variable_map: BTreeMap<String, ValueId>,
|
||
|
||
/// ValueId → 起源Box名 マッピング
|
||
/// NewBox命令で生成されたValueIdがどのBox型から来たかを追跡
|
||
/// 例: ValueId(10) → "ParserBox"
|
||
/// Phase 25.1: HashMap → BTreeMap(決定性確保)
|
||
#[allow(dead_code)]
|
||
pub value_origin_newbox: BTreeMap<ValueId, String>,
|
||
|
||
/// ValueId → MIR型 マッピング
|
||
/// 各ValueIdの型情報を保持
|
||
/// 例: ValueId(5) → MirType::Integer
|
||
/// Phase 25.1: HashMap → BTreeMap(決定性確保)
|
||
#[allow(dead_code)]
|
||
pub value_types: BTreeMap<ValueId, MirType>,
|
||
}
|
||
|
||
impl BoxCompilationContext {
|
||
/// 新しい空のコンテキストを作成
|
||
pub fn new() -> Self {
|
||
Self::default()
|
||
}
|
||
|
||
/// コンテキストが空(未使用)かどうかを判定
|
||
#[allow(dead_code)]
|
||
pub fn is_empty(&self) -> bool {
|
||
self.variable_map.is_empty()
|
||
&& self.value_origin_newbox.is_empty()
|
||
&& self.value_types.is_empty()
|
||
}
|
||
|
||
/// デバッグ用:コンテキストのサイズ情報を取得
|
||
#[allow(dead_code)]
|
||
pub fn size_info(&self) -> (usize, usize, usize) {
|
||
(
|
||
self.variable_map.len(),
|
||
self.value_origin_newbox.len(),
|
||
self.value_types.len(),
|
||
)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_context_creation() {
|
||
let ctx = BoxCompilationContext::new();
|
||
assert!(ctx.is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn test_context_isolation() {
|
||
let mut ctx1 = BoxCompilationContext::new();
|
||
ctx1.variable_map.insert("x".to_string(), ValueId::new(1));
|
||
|
||
let ctx2 = BoxCompilationContext::new();
|
||
assert!(ctx2.is_empty(), "新しいコンテキストは空であるべき");
|
||
assert!(!ctx1.is_empty(), "ctx1は変更されたまま");
|
||
}
|
||
|
||
#[test]
|
||
fn test_size_info() {
|
||
let mut ctx = BoxCompilationContext::new();
|
||
ctx.variable_map.insert("a".to_string(), ValueId::new(1));
|
||
ctx.value_origin_newbox
|
||
.insert(ValueId::new(2), "StringBox".to_string());
|
||
ctx.value_types.insert(ValueId::new(3), MirType::Integer);
|
||
|
||
let (vars, origins, types) = ctx.size_info();
|
||
assert_eq!(vars, 1);
|
||
assert_eq!(origins, 1);
|
||
assert_eq!(types, 1);
|
||
}
|
||
}
|