refactor(mir): Remove CoreContext legacy fields (Phase 2-2/7)

完全移行→削除の安全順序(Option C)に従い、CoreContext の
deprecated フィールドと sync helpers を完全削除。

## Changes
- Migrated all access sites to core_ctx.* (SSOT allocators)
  - utils.rs: value_gen (3 sites), block_gen (1 site), temp_slot_counter (1 site)
  - loop_api.rs: block_gen (1 site)
  - phi_helpers.rs: value_gen (1 site)
  - builder.rs: sync helpers (2 methods), test assertions (2 sites)
- Removed 5 deprecated fields from builder.rs
  - value_gen: ValueIdGenerator
  - block_gen: BasicBlockIdGenerator
  - next_binding_id: u32
  - temp_slot_counter: u32
  - debug_join_counter: u32
- Removed sync helper calls (allocate_binding_id, debug_next_join_id)
- Removed field initializations from MirBuilder::new()
- Maintained Phase 136 P0 SSOT structure (next_value_id() remains as high-level API)

## Tests
- cargo build --release: PASS
- cargo test --release --lib: 1029 passed (4 pre-existing failures)
- Deprecation warnings: 435 → 456 (+21)
  Note: Warnings increased due to remaining deprecated field *uses* being exposed
  after removing the field *definitions*. This is expected during migration.

## Code metrics
- Net reduction: 40 lines (-56 removed, +16 modifications)
- Files changed: 4 (builder.rs, utils.rs, loop_api.rs, phi_helpers.rs)

Phase 2 Progress: 2/7 contexts complete (MetadataContext , CoreContext )
This commit is contained in:
nyash-codex
2025-12-15 23:05:36 +09:00
parent a898e99310
commit 2db8ff72d0
4 changed files with 16 additions and 56 deletions

View File

@ -91,15 +91,6 @@ pub struct MirBuilder {
/// Direct field access for backward compatibility (migration in progress). /// Direct field access for backward compatibility (migration in progress).
pub(super) core_ctx: core_context::CoreContext, pub(super) core_ctx: core_context::CoreContext,
/// [DEPRECATED] Value ID generator
/// Phase 136: Moved to core_ctx.value_gen (backward compat wrapper)
#[deprecated(note = "Use core_ctx.value_gen instead")]
pub(super) value_gen: ValueIdGenerator,
/// [DEPRECATED] Basic block ID generator
/// Phase 136: Moved to core_ctx.block_gen (backward compat wrapper)
#[deprecated(note = "Use core_ctx.block_gen instead")]
pub(super) block_gen: BasicBlockIdGenerator,
/// [DEPRECATED] 箱理論: Static boxコンパイル時のコンテキスト分離 /// [DEPRECATED] 箱理論: Static boxコンパイル時のコンテキスト分離
/// Phase 136 Step 7/7: Moved to comp_ctx.compilation_context (backward compat wrapper) /// Phase 136 Step 7/7: Moved to comp_ctx.compilation_context (backward compat wrapper)
@ -259,11 +250,6 @@ pub struct MirBuilder {
#[deprecated(note = "Use comp_ctx.reserved_value_ids instead")] #[deprecated(note = "Use comp_ctx.reserved_value_ids instead")]
pub(super) reserved_value_ids: HashSet<ValueId>, pub(super) reserved_value_ids: HashSet<ValueId>,
/// [DEPRECATED] Phase 74: BindingId allocation counter (parallel to ValueId)
/// Phase 136: Moved to core_ctx.next_binding_id (backward compat wrapper)
#[deprecated(note = "Use core_ctx.next_binding_id instead")]
pub next_binding_id: u32,
/// [DEPRECATED] Phase 74: BindingId mapping for lexical variable bindings /// [DEPRECATED] Phase 74: BindingId mapping for lexical variable bindings
/// Phase 136: Moved to binding_ctx.binding_map (backward compat wrapper) /// Phase 136: Moved to binding_ctx.binding_map (backward compat wrapper)
#[deprecated(note = "Use binding_ctx.binding_map instead")] #[deprecated(note = "Use binding_ctx.binding_map instead")]
@ -301,10 +287,6 @@ pub struct MirBuilder {
pub(super) cleanup_allow_return: bool, pub(super) cleanup_allow_return: bool,
pub(super) cleanup_allow_throw: bool, pub(super) cleanup_allow_throw: bool,
/// [DEPRECATED] Internal counter for temporary pin slots (block-crossing ephemeral values)
/// Phase 136: Moved to core_ctx.temp_slot_counter (backward compat wrapper)
#[deprecated(note = "Use core_ctx.temp_slot_counter instead")]
temp_slot_counter: u32,
/// If true, skip entry materialization of pinned slots on the next start_new_block call. /// If true, skip entry materialization of pinned slots on the next start_new_block call.
suppress_pin_entry_copy_next: bool, suppress_pin_entry_copy_next: bool,
@ -315,10 +297,6 @@ pub struct MirBuilder {
/// Phase 136: Moved to scope_ctx.debug_scope_stack (backward compat wrapper) /// Phase 136: Moved to scope_ctx.debug_scope_stack (backward compat wrapper)
#[deprecated(note = "Use scope_ctx.debug_scope_stack instead")] #[deprecated(note = "Use scope_ctx.debug_scope_stack instead")]
debug_scope_stack: Vec<String>, debug_scope_stack: Vec<String>,
/// [DEPRECATED] Monotonic counter for region IDs (deterministic across a run).
/// Phase 136: Moved to core_ctx.debug_join_counter (backward compat wrapper)
#[deprecated(note = "Use core_ctx.debug_join_counter instead")]
debug_join_counter: u32,
/// Local SSA cache: ensure per-block materialization for critical operands (e.g., recv) /// Local SSA cache: ensure per-block materialization for critical operands (e.g., recv)
/// Key: (bb, original ValueId, kind) -> local ValueId /// Key: (bb, original ValueId, kind) -> local ValueId
@ -374,10 +352,6 @@ impl MirBuilder {
// Phase 136 Step 2/7: Core context (new SSOT) // Phase 136 Step 2/7: Core context (new SSOT)
core_ctx, core_ctx,
// Legacy fields (kept for backward compatibility, synced with core_ctx)
value_gen: ValueIdGenerator::new(),
block_gen: BasicBlockIdGenerator::new(),
compilation_context: None, // 箱理論: デフォルトは従来モード compilation_context: None, // 箱理論: デフォルトは従来モード
type_ctx: type_context::TypeContext::new(), // Phase 136: Type context type_ctx: type_context::TypeContext::new(), // Phase 136: Type context
scope_ctx: scope_context::ScopeContext::new(), // Phase 136 Step 3/7: Scope context scope_ctx: scope_context::ScopeContext::new(), // Phase 136 Step 3/7: Scope context
@ -408,7 +382,6 @@ impl MirBuilder {
fn_body_ast: None, // Phase 200-C: Initialize to None fn_body_ast: None, // Phase 200-C: Initialize to None
reserved_value_ids: HashSet::new(), // Phase 201-A: Initialize to empty reserved_value_ids: HashSet::new(), // Phase 201-A: Initialize to empty
next_binding_id: 0, // Phase 74: Initialize BindingId counter (legacy)
binding_map: BTreeMap::new(), // Phase 74: Initialize BindingId mapping binding_map: BTreeMap::new(), // Phase 74: Initialize BindingId mapping
loop_header_stack: Vec::new(), loop_header_stack: Vec::new(),
@ -422,12 +395,10 @@ impl MirBuilder {
in_cleanup_block: false, in_cleanup_block: false,
cleanup_allow_return: false, cleanup_allow_return: false,
cleanup_allow_throw: false, cleanup_allow_throw: false,
temp_slot_counter: 0, // Legacy (synced with core_ctx)
suppress_pin_entry_copy_next: false, suppress_pin_entry_copy_next: false,
// Debug scope context // Debug scope context
debug_scope_stack: Vec::new(), debug_scope_stack: Vec::new(),
debug_join_counter: 0, // Legacy (synced with core_ctx)
local_ssa_map: HashMap::new(), local_ssa_map: HashMap::new(),
schedule_mat_map: HashMap::new(), schedule_mat_map: HashMap::new(),
@ -549,12 +520,9 @@ impl MirBuilder {
/// // next_value_id() -> ValueId(20) /// // next_value_id() -> ValueId(20)
/// // } /// // }
/// ``` /// ```
#[allow(deprecated)]
pub fn allocate_binding_id(&mut self) -> super::BindingId { pub fn allocate_binding_id(&mut self) -> super::BindingId {
// Phase 136 Step 2/7: Use core_ctx as SSOT, sync legacy field // Phase 136 Step 2/7 + Phase 2-2: Use core_ctx as SSOT (no sync needed)
let id = self.core_ctx.next_binding(); self.core_ctx.next_binding()
self.next_binding_id = self.core_ctx.next_binding_id;
id
} }
// ---- Hint helpers (no-op by default) ---- // ---- Hint helpers (no-op by default) ----
@ -576,12 +544,9 @@ impl MirBuilder {
// Debug scope helpers (region_id for DebugHub events) // Debug scope helpers (region_id for DebugHub events)
// ---------------------- // ----------------------
#[inline] #[inline]
#[allow(deprecated)]
pub(crate) fn debug_next_join_id(&mut self) -> u32 { pub(crate) fn debug_next_join_id(&mut self) -> u32 {
// Phase 136 Step 2/7: Use core_ctx as SSOT, sync legacy field // Phase 136 Step 2/7 + Phase 2-2: Use core_ctx as SSOT (no sync needed)
let id = self.core_ctx.next_debug_join(); self.core_ctx.next_debug_join()
self.debug_join_counter = self.core_ctx.debug_join_counter;
id
} }
#[inline] #[inline]
@ -1317,7 +1282,7 @@ mod binding_id_tests {
#[allow(deprecated)] #[allow(deprecated)]
fn test_binding_map_initialization() { fn test_binding_map_initialization() {
let builder = MirBuilder::new(); let builder = MirBuilder::new();
assert_eq!(builder.next_binding_id, 0); assert_eq!(builder.core_ctx.next_binding_id, 0);
// Phase 136 Step 4/7: Check both binding_ctx (SSOT) and legacy field // Phase 136 Step 4/7: Check both binding_ctx (SSOT) and legacy field
assert!(builder.binding_ctx.is_empty()); assert!(builder.binding_ctx.is_empty());
assert!(builder.binding_map.is_empty()); assert!(builder.binding_map.is_empty());
@ -1333,7 +1298,7 @@ mod binding_id_tests {
assert_eq!(bid0.raw(), 0); assert_eq!(bid0.raw(), 0);
assert_eq!(bid1.raw(), 1); assert_eq!(bid1.raw(), 1);
assert_eq!(bid2.raw(), 2); assert_eq!(bid2.raw(), 2);
assert_eq!(builder.next_binding_id, 3); assert_eq!(builder.core_ctx.next_binding_id, 3);
} }
#[test] #[test]

View File

@ -41,10 +41,8 @@ impl super::MirBuilder {
let candidate = if let Some(ref mut f) = self.current_function { let candidate = if let Some(ref mut f) = self.current_function {
f.next_value_id() // Function context f.next_value_id() // Function context
} else { } else {
// Phase 136 Step 2/7: Use core_ctx as SSOT, sync legacy field // Phase 136 Step 2/7 + Phase 2-2: Use core_ctx as SSOT (no sync needed)
let id = self.core_ctx.next_value(); self.core_ctx.next_value()
self.value_gen = self.core_ctx.value_gen.clone();
id
}; };
// Phase 201-A: Skip reserved PHI dst ValueIds // Phase 201-A: Skip reserved PHI dst ValueIds
@ -60,13 +58,10 @@ impl super::MirBuilder {
/// Allocate a new BasicBlockId /// Allocate a new BasicBlockId
/// ///
/// Phase 136 Step 2/7: Uses core_ctx as SSOT, syncs legacy field. /// Phase 136 Step 2/7 + Phase 2-2: Uses core_ctx as SSOT (no sync needed).
#[inline] #[inline]
#[allow(deprecated)]
pub(crate) fn next_block_id(&mut self) -> super::BasicBlockId { pub(crate) fn next_block_id(&mut self) -> super::BasicBlockId {
let id = self.core_ctx.next_block(); self.core_ctx.next_block()
self.block_gen = self.core_ctx.block_gen.clone();
id
} }
// ---- LocalSSA convenience (readability helpers) ---- // ---- LocalSSA convenience (readability helpers) ----
@ -442,13 +437,13 @@ impl super::MirBuilder {
v: super::ValueId, v: super::ValueId,
hint: &str, hint: &str,
) -> Result<super::ValueId, String> { ) -> Result<super::ValueId, String> {
self.temp_slot_counter = self.temp_slot_counter.wrapping_add(1); let slot_id = self.core_ctx.next_temp_slot();
let slot_name = format!("__pin${}${}", self.temp_slot_counter, hint); let slot_name = format!("__pin${}${}", slot_id, hint);
// Phase 25.1b: Use function-local ID allocator to avoid SSA verification failures // Phase 25.1b: Use function-local ID allocator to avoid SSA verification failures
let dst = if let Some(ref mut f) = self.current_function { let dst = if let Some(ref mut f) = self.current_function {
f.next_value_id() // Function context: use local ID f.next_value_id() // Function context: use local ID
} else { } else {
self.value_gen.next() // Module context: use global ID self.core_ctx.next_value() // Module context: use core_ctx SSOT
}; };
self.emit_instruction(super::MirInstruction::Copy { dst, src: v })?; self.emit_instruction(super::MirInstruction::Copy { dst, src: v })?;
if super::utils::builder_debug_enabled() if super::utils::builder_debug_enabled()
@ -479,7 +474,7 @@ impl super::MirBuilder {
let dst = if let Some(ref mut f) = self.current_function { let dst = if let Some(ref mut f) = self.current_function {
f.next_value_id() // Function context: use local ID f.next_value_id() // Function context: use local ID
} else { } else {
self.value_gen.next() // Module context: use global ID self.core_ctx.next_value() // Module context: use core_ctx SSOT
}; };
self.emit_instruction(super::MirInstruction::Copy { dst, src: v })?; self.emit_instruction(super::MirInstruction::Copy { dst, src: v })?;
// Propagate metadata (type/origin) from source to the new local copy // Propagate metadata (type/origin) from source to the new local copy

View File

@ -83,7 +83,7 @@ pub fn build_simple_loop<L: LoopBuilderApi>(
// === Legacy wiring: implement LoopBuilderApi for mir::builder::MirBuilder === // === Legacy wiring: implement LoopBuilderApi for mir::builder::MirBuilder ===
impl LoopBuilderApi for super::builder::MirBuilder { impl LoopBuilderApi for super::builder::MirBuilder {
fn new_block(&mut self) -> BasicBlockId { fn new_block(&mut self) -> BasicBlockId {
self.block_gen.next() self.core_ctx.next_block()
} }
fn current_block(&self) -> Result<BasicBlockId, String> { fn current_block(&self) -> Result<BasicBlockId, String> {
self.current_block self.current_block

View File

@ -66,7 +66,7 @@ impl MirBuilder {
let phi_val = if let Some(ref mut f) = self.current_function { let phi_val = if let Some(ref mut f) = self.current_function {
f.next_value_id() // Function context: use local ID allocator f.next_value_id() // Function context: use local ID allocator
} else { } else {
self.value_gen.next() // Module context: use global ID allocator self.core_ctx.next_value() // Module context: use core_ctx SSOT
}; };
// 統一された挿入ロジック(既存パターンと完全互換) // 統一された挿入ロジック(既存パターンと完全互換)