diff --git a/src/mir/builder.rs b/src/mir/builder.rs index 0b446bbd..647728c9 100644 --- a/src/mir/builder.rs +++ b/src/mir/builder.rs @@ -356,17 +356,13 @@ impl MirBuilder { } /// Push/pop helpers for If merge context (best-effort; optional usage) - #[allow(deprecated)] pub(super) fn push_if_merge(&mut self, bb: BasicBlockId) { - // Phase 136 Step 3/7: Update both scope_ctx (SSOT) and legacy field (backward compat) + // Phase 2-4: Use scope_ctx only (legacy field removed) self.scope_ctx.push_if_merge(bb); - self.if_merge_stack.push(bb); } - #[allow(deprecated)] pub(super) fn pop_if_merge(&mut self) { - // Phase 136 Step 3/7: Update both scope_ctx (SSOT) and legacy field (backward compat) + // Phase 2-4: Use scope_ctx only (legacy field removed) let _ = self.scope_ctx.pop_if_merge(); - let _ = self.if_merge_stack.pop(); } /// Suppress entry pin copy for the next start_new_block (used for merge blocks). @@ -427,20 +423,16 @@ impl MirBuilder { } #[inline] - #[allow(deprecated)] pub(crate) fn debug_push_region>(&mut self, region: S) { - // Phase 136 Step 3/7: Update both scope_ctx (SSOT) and legacy field (backward compat) + // Phase 2-4: Use scope_ctx only (legacy field removed) let region = region.into(); - self.scope_ctx.debug_push_region(region.clone()); - self.debug_scope_stack.push(region); + self.scope_ctx.debug_push_region(region); } #[inline] - #[allow(deprecated)] pub(crate) fn debug_pop_region(&mut self) { - // Phase 136 Step 3/7: Update both scope_ctx (SSOT) and legacy field (backward compat) + // Phase 2-4: Use scope_ctx only (legacy field removed) self.scope_ctx.debug_pop_region(); - let _ = self.debug_scope_stack.pop(); } #[inline] @@ -704,7 +696,7 @@ impl MirBuilder { // Precompute debug metadata to avoid borrow conflicts later let _dbg_fn_name = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.clone()); let _dbg_region_id = self.debug_current_region_id(); @@ -755,7 +747,7 @@ impl MirBuilder { } } - if let Some(ref mut function) = self.current_function { + if let Some(ref mut function) = self.scope_ctx.current_function { // Pre-capture branch/jump targets for predecessor update after we finish // mutably borrowing the current block. let (then_t, else_t, jump_t) = match &instruction { @@ -918,7 +910,7 @@ impl MirBuilder { phi_id: ValueId, new_inputs: Vec<(BasicBlockId, ValueId)>, ) -> Result<(), String> { - if let Some(ref mut function) = self.current_function { + if let Some(ref mut function) = self.scope_ctx.current_function { if let Some(block_data) = function.get_block_mut(block) { // Find PHI instruction with matching dst for inst in &mut block_data.instructions { @@ -1076,7 +1068,7 @@ impl MirBuilder { /// Check if the current basic block is terminated fn is_current_block_terminated(&self) -> bool { - if let (Some(block_id), Some(ref function)) = (self.current_block, &self.current_function) { + if let (Some(block_id), Some(ref function)) = (self.current_block, &self.scope_ctx.current_function) { if let Some(block) = function.get_block(block_id) { return block.is_terminated(); } diff --git a/src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs b/src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs index dd161d51..9fe24e45 100644 --- a/src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs +++ b/src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs @@ -223,7 +223,7 @@ impl LoopHeaderPhiBuilder { // Get the header block from current function let current_func = builder - .current_function + .scope_ctx.current_function .as_mut() .ok_or("Phase 33-16: No current function when finalizing header PHIs")?; diff --git a/src/mir/builder/control_flow/joinir/routing.rs b/src/mir/builder/control_flow/joinir/routing.rs index 178002da..a408bc24 100644 --- a/src/mir/builder/control_flow/joinir/routing.rs +++ b/src/mir/builder/control_flow/joinir/routing.rs @@ -28,7 +28,7 @@ impl MirBuilder { ) -> Result, String> { // Get current function name let func_name = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.clone()) .unwrap_or_default(); diff --git a/src/mir/builder/if_form.rs b/src/mir/builder/if_form.rs index 6a4084a8..1f5977b0 100644 --- a/src/mir/builder/if_form.rs +++ b/src/mir/builder/if_form.rs @@ -222,7 +222,7 @@ impl MirBuilder { // 関数名ガードチェック let func_name = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()) .unwrap_or(""); diff --git a/src/mir/builder/method_call_handlers.rs b/src/mir/builder/method_call_handlers.rs index 2dd27a10..02893962 100644 --- a/src/mir/builder/method_call_handlers.rs +++ b/src/mir/builder/method_call_handlers.rs @@ -24,7 +24,7 @@ impl MeCallPolicyBox { ) -> Result, String> { // Instance box: prefer enclosing box method (lowered function) if存在 let enclosing_cls: Option = builder - .current_function + .scope_ctx.current_function .as_ref() .and_then(|f| f.signature.name.split('.').next().map(|s| s.to_string())); diff --git a/src/mir/builder/observe/resolve.rs b/src/mir/builder/observe/resolve.rs index 61c7e922..dc3a93d8 100644 --- a/src/mir/builder/observe/resolve.rs +++ b/src/mir/builder/observe/resolve.rs @@ -25,7 +25,7 @@ fn sample_every() -> usize { /// Dev‑only: emit a resolve.try event(candidates inspection)。 pub(crate) fn emit_try(builder: &MirBuilder, meta: serde_json::Value) { let fn_name = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()); let region = builder.debug_current_region_id(); @@ -35,7 +35,7 @@ pub(crate) fn emit_try(builder: &MirBuilder, meta: serde_json::Value) { /// Dev‑only: emit a resolve.choose event(decision)。 pub(crate) fn emit_choose(builder: &MirBuilder, meta: serde_json::Value) { let fn_name = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()); let region = builder.debug_current_region_id(); diff --git a/src/mir/builder/observe/ssa.rs b/src/mir/builder/observe/ssa.rs index dae262fe..b4ceaa86 100644 --- a/src/mir/builder/observe/ssa.rs +++ b/src/mir/builder/observe/ssa.rs @@ -35,7 +35,7 @@ pub(crate) fn emit_phi(builder: &MirBuilder, dst: ValueId, inputs: &Vec<(BasicBl "decided_origin": decided_o, }); let fn_name = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()); let region = builder.debug_current_region_id(); diff --git a/src/mir/builder/ops.rs b/src/mir/builder/ops.rs index 1211f81b..a9696a81 100644 --- a/src/mir/builder/ops.rs +++ b/src/mir/builder/ops.rs @@ -48,7 +48,7 @@ impl super::MirBuilder { BinaryOpType::Arithmetic(op) => { // Dev: Lower '+' を演算子ボックス呼び出しに置換(既定OFF) let in_add_op = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.starts_with("AddOperator.apply/")) .unwrap_or(false); @@ -121,7 +121,7 @@ impl super::MirBuilder { }; if !name.is_empty() { let in_guard = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.starts_with(guard_prefix)) .unwrap_or(false); @@ -252,7 +252,7 @@ impl super::MirBuilder { BinaryOpType::Comparison(op) => { // Dev: Lower 比較 を演算子ボックス呼び出しに置換(既定OFF) let in_cmp_op = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.starts_with("CompareOperator.apply/")) .unwrap_or(false); @@ -541,7 +541,7 @@ impl super::MirBuilder { }; if !name.is_empty() { let in_guard = self - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.starts_with(guard_prefix)) .unwrap_or(false); diff --git a/src/mir/builder/receiver.rs b/src/mir/builder/receiver.rs index 3f4d95d8..33dd822a 100644 --- a/src/mir/builder/receiver.rs +++ b/src/mir/builder/receiver.rs @@ -23,7 +23,7 @@ pub fn finalize_method_receiver(builder: &mut MirBuilder, callee: &mut Callee) { // Optional dev trace for receiver aliases if std::env::var("NYASH_BUILDER_TRACE_RECV").ok().as_deref() == Some("1") { let current_fn = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.clone()) .unwrap_or_else(|| "".to_string()); diff --git a/src/mir/builder/scope_context.rs b/src/mir/builder/scope_context.rs index 4ad61aa1..2a5dddca 100644 --- a/src/mir/builder/scope_context.rs +++ b/src/mir/builder/scope_context.rs @@ -20,7 +20,7 @@ pub(in crate::mir::builder) use super::vars::lexical_scope::LexicalScopeFrame; /// Scope and control flow context for MIR building #[derive(Debug)] -pub(super) struct ScopeContext { +pub(in crate::mir) struct ScopeContext { // ---- Lexical scope management ---- /// Stack of lexical scopes for block-scoped `local` declarations /// Tracks per-block shadowing so variables restore on scope exit @@ -41,11 +41,11 @@ pub(super) struct ScopeContext { // ---- Function context ---- /// Current function being built - pub(super) current_function: Option, + pub(in crate::mir) current_function: Option, /// Parameter names for current function /// Same lifecycle as current_function - pub(super) function_param_names: HashSet, + pub(in crate::mir) function_param_names: HashSet, // ---- Debug scope ---- /// Stack of region identifiers (e.g., "loop#1/header", "join#3/join") diff --git a/src/mir/builder/vars/lexical_scope.rs b/src/mir/builder/vars/lexical_scope.rs index 21baa9cf..51bd601d 100644 --- a/src/mir/builder/vars/lexical_scope.rs +++ b/src/mir/builder/vars/lexical_scope.rs @@ -35,27 +35,18 @@ impl Drop for LexicalScopeGuard { } impl super::super::MirBuilder { - #[allow(deprecated)] pub(in crate::mir::builder) fn push_lexical_scope(&mut self) { - // Phase 136 Step 3/7: Update both scope_ctx (SSOT) and legacy field (backward compat) + // Phase 2-4: Use scope_ctx only (legacy field removed) self.scope_ctx.push_lexical_scope(); - self.lexical_scope_stack.push(LexicalScopeFrame::new()); } - #[allow(deprecated)] pub(in crate::mir::builder) fn pop_lexical_scope(&mut self) { - // Phase 136 Step 3/7: Pop from scope_ctx (SSOT) + // Phase 2-4: Use scope_ctx only (legacy field removed) let frame = self .scope_ctx .pop_lexical_scope() .expect("COMPILER BUG: pop_lexical_scope without push_lexical_scope"); - // Sync to legacy field - let _ = self - .lexical_scope_stack - .pop() - .expect("COMPILER BUG: legacy stack out of sync"); - // Restore ValueId mappings for (name, previous) in frame.restore { match previous { @@ -82,13 +73,12 @@ impl super::super::MirBuilder { } } - #[allow(deprecated)] pub(in crate::mir::builder) fn declare_local_in_current_scope( &mut self, name: &str, value: ValueId, ) -> Result<(), String> { - // Phase 136 Step 3/7: Use scope_ctx (SSOT) + // Phase 2-4: Use scope_ctx (SSOT) let Some(frame) = self.scope_ctx.current_scope_mut() else { return Err("COMPILER BUG: local declaration outside lexical scope".to_string()); }; diff --git a/src/mir/loop_api.rs b/src/mir/loop_api.rs index 68ba09ae..b63e7cbb 100644 --- a/src/mir/loop_api.rs +++ b/src/mir/loop_api.rs @@ -102,7 +102,7 @@ impl LoopBuilderApi for super::builder::MirBuilder { } fn add_predecessor(&mut self, block: BasicBlockId, pred: BasicBlockId) -> Result<(), String> { - if let Some(ref mut f) = self.current_function { + if let Some(ref mut f) = self.scope_ctx.current_function { if let Some(bb) = f.get_block_mut(block) { bb.add_predecessor(pred); Ok(()) @@ -115,7 +115,7 @@ impl LoopBuilderApi for super::builder::MirBuilder { } fn seal_block(&mut self, block: BasicBlockId) -> Result<(), String> { - if let Some(ref mut f) = self.current_function { + if let Some(ref mut f) = self.scope_ctx.current_function { if let Some(bb) = f.get_block_mut(block) { bb.seal(); Ok(()) @@ -133,7 +133,7 @@ impl LoopBuilderApi for super::builder::MirBuilder { dst: ValueId, inputs: Vec<(BasicBlockId, ValueId)>, ) -> Result<(), String> { - if let Some(ref mut f) = self.current_function { + if let Some(ref mut f) = self.scope_ctx.current_function { crate::mir::ssot::cf_common::insert_phi_at_head_spanned( f, block, diff --git a/src/mir/region/observer.rs b/src/mir/region/observer.rs index 819ef8ad..68b09a6f 100644 --- a/src/mir/region/observer.rs +++ b/src/mir/region/observer.rs @@ -37,7 +37,7 @@ pub fn observe_control_form(builder: &mut MirBuilder, form: &ControlForm) { } let func_name = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()) .unwrap_or(""); @@ -94,7 +94,7 @@ pub fn observe_function_region(builder: &mut MirBuilder) { } let func_name = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.signature.name.as_str()) .unwrap_or(""); @@ -107,7 +107,7 @@ pub fn observe_function_region(builder: &mut MirBuilder) { let id = RegionId(NEXT_REGION_ID.fetch_add(1, Ordering::Relaxed)); let entry_block = builder - .current_function + .scope_ctx.current_function .as_ref() .map(|f| f.entry_block) .unwrap_or_else(|| crate::mir::BasicBlockId::new(0)); diff --git a/src/mir/utils/control_flow.rs b/src/mir/utils/control_flow.rs index ebbe5d13..6b68aa42 100644 --- a/src/mir/utils/control_flow.rs +++ b/src/mir/utils/control_flow.rs @@ -22,7 +22,7 @@ pub fn is_current_block_terminated(builder: &MirBuilder) -> Result .current_block .ok_or_else(|| "No current block".to_string())?; - if let Some(ref function) = builder.current_function { + if let Some(ref function) = builder.scope_ctx.current_function { if let Some(bb) = function.get_block(cur_id) { Ok(bb.is_terminated()) } else { diff --git a/src/mir/utils/phi_helpers.rs b/src/mir/utils/phi_helpers.rs index c073a9d1..3dbcdb72 100644 --- a/src/mir/utils/phi_helpers.rs +++ b/src/mir/utils/phi_helpers.rs @@ -14,7 +14,7 @@ * // Before (5-7行) * let phi_val = self.value_gen.next(); * let inputs = vec![(pred1, val1), (pred2, val2)]; - * if let (Some(func), Some(cur_bb)) = (self.current_function.as_mut(), self.current_block) { + * if let (Some(func), Some(cur_bb)) = (self.scope_ctx.current_function.as_mut(), self.current_block) { * crate::mir::ssot::cf_common::insert_phi_at_head(func, cur_bb, phi_val, inputs); * } else { * self.emit_instruction(MirInstruction::Phi { dst: phi_val, inputs })?; @@ -63,14 +63,14 @@ impl MirBuilder { // Phase 25.1b fix: Use function-local ID allocator to avoid SSA verification failures // This prevents PHI dst ValueIds from colliding with function-local IDs allocated later. // Same pattern as pin_to_slot() and the loop builder fix in e2d061d1. - let phi_val = if let Some(ref mut f) = self.current_function { + let phi_val = if let Some(ref mut f) = self.scope_ctx.current_function { f.next_value_id() // Function context: use local ID allocator } else { self.core_ctx.next_value() // Module context: use core_ctx SSOT }; // 統一された挿入ロジック(既存パターンと完全互換) - if let (Some(func), Some(cur_bb)) = (self.current_function.as_mut(), self.current_block) { + if let (Some(func), Some(cur_bb)) = (self.scope_ctx.current_function.as_mut(), self.current_block) { // CFG経由の正規化挿入(predecessor順序の正規化を含む) crate::mir::ssot::cf_common::insert_phi_at_head_spanned( func, @@ -105,7 +105,7 @@ impl MirBuilder { /// `dst`は必ず関数コンテキストに適したアロケーターで確保すること: /// ```rust /// // ✅ 正しい: 関数ローカルアロケーター使用 - /// let result_val = if let Some(ref mut f) = self.current_function { + /// let result_val = if let Some(ref mut f) = self.scope_ctx.current_function { /// f.next_value_id() /// } else { /// self.value_gen.next() @@ -118,7 +118,7 @@ impl MirBuilder { /// /// ## 例 /// ```rust - /// let result_val = if let Some(ref mut f) = self.current_function { + /// let result_val = if let Some(ref mut f) = self.scope_ctx.current_function { /// f.next_value_id() // 関数コンテキスト: ローカルID /// } else { /// self.value_gen.next() // モジュールコンテキスト: グローバルID @@ -136,7 +136,7 @@ impl MirBuilder { inputs: Vec<(BasicBlockId, ValueId)>, ) -> Result<(), String> { // 統一された挿入ロジック(既存パターンと完全互換) - if let (Some(func), Some(cur_bb)) = (self.current_function.as_mut(), self.current_block) { + if let (Some(func), Some(cur_bb)) = (self.scope_ctx.current_function.as_mut(), self.current_block) { // CFG経由の正規化挿入(predecessor順序の正規化を含む) crate::mir::ssot::cf_common::insert_phi_at_head_spanned( func,