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

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

## Changes
- Migrated all access sites to metadata_ctx.* (builder methods)
  - current_span → metadata_ctx.current_span() / set_current_span()
  - source_file → metadata_ctx.current_source_file() / set_source_file() / clear_source_file()
  - hint_sink → metadata_ctx.hint_scope_enter/leave/join_result()
  - current_region_stack → metadata_ctx.push_region/pop_region/current_region_stack()
- Removed 4 deprecated fields from MirBuilder
  - current_span, source_file, hint_sink, current_region_stack
- Removed 2 sync helpers
  - sync_metadata_ctx_to_legacy(), sync_legacy_to_metadata_ctx()
- Updated 10 files with direct field accesses

## Files Modified
- src/mir/builder.rs: -56 lines (fields + sync helpers + initialization)
- src/mir/builder/exprs.rs: metadata_ctx.set_current_span()
- src/mir/builder/exprs_peek.rs: metadata_ctx.current_span()
- src/mir/builder/if_form.rs: metadata_ctx.current_span()
- src/mir/builder/phi.rs: metadata_ctx.current_span()
- src/mir/builder/stmts.rs: metadata_ctx.set_current_span()
- src/mir/builder/utils.rs: metadata_ctx.current_span()
- src/mir/loop_api.rs: metadata_ctx.current_span()
- src/mir/region/observer.rs: metadata_ctx.push/pop_region()
- src/mir/utils/phi_helpers.rs: metadata_ctx.current_span()

## Tests
- cargo build --release: SUCCESS (1m 10s)
- cargo test --release --lib: 1029 PASS, 4 FAIL (pre-existing)
- Deprecation warnings: 469 → 435 (-34 warnings)

## Verification
 No direct access to removed fields (grep count: 0)
 No sync helper calls (grep count: 0)
 Build success
 MetadataContext is now complete SSOT (二重管理解消)

Phase 2 Progress: 1/7 contexts complete (MetadataContext )

🎉 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:44:38 +09:00
parent 905a2b97fe
commit a898e99310
10 changed files with 16 additions and 72 deletions

View File

@ -244,12 +244,6 @@ pub struct MirBuilder {
#[deprecated(note = "Use comp_ctx.method_tail_index_source_len instead")]
pub(super) method_tail_index_source_len: usize,
/// [DEPRECATED] Region 観測用のスタックだよFunctionRegion がルート)。
/// Phase 136 Step 6/7: Moved to metadata_ctx.current_region_stack (backward compat wrapper)
/// - NYASH_REGION_TRACE=1 のときだけ使われる開発用メタデータだよ。
#[deprecated(note = "Use metadata_ctx.current_region_stack instead")]
pub(super) current_region_stack: Vec<RegionId>,
/// [DEPRECATED] Phase 200-C: Original function body AST for capture analysis
/// Phase 136 Step 7/7: Moved to comp_ctx.fn_body_ast (backward compat wrapper)
/// Stored temporarily during function lowering to support FunctionScopeCaptureAnalyzer.
@ -307,11 +301,6 @@ pub struct MirBuilder {
pub(super) cleanup_allow_return: bool,
pub(super) cleanup_allow_throw: bool,
/// [DEPRECATED] Hint sink (zero-cost guidance; currently no-op)
/// Phase 136 Step 6/7: Moved to metadata_ctx.hint_sink (backward compat wrapper)
#[deprecated(note = "Use metadata_ctx.hint_sink instead")]
pub(super) hint_sink: crate::mir::hints::HintSink,
/// [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")]
@ -353,16 +342,6 @@ pub struct MirBuilder {
/// Tracks the depth of build_expression calls to detect infinite loops
pub(super) recursion_depth: usize,
/// [DEPRECATED] Current AST span being lowered (used to annotate MIR instructions)
/// Phase 136 Step 6/7: Moved to metadata_ctx.current_span (backward compat wrapper)
#[deprecated(note = "Use metadata_ctx.current_span instead")]
pub(super) current_span: Span,
/// [DEPRECATED] Optional source file hint for metadata/spans
/// Phase 136 Step 6/7: Moved to metadata_ctx.source_file (backward compat wrapper)
#[deprecated(note = "Use metadata_ctx.source_file instead")]
pub(super) source_file: Option<String>,
/// Root lowering mode: how to treat top-level Program
/// - None: not decided yet (lower_root not called)
/// - Some(true): App mode (static box Main.main is entry)
@ -426,7 +405,6 @@ impl MirBuilder {
method_tail_index: std::collections::HashMap::new(),
method_tail_index_source_len: 0,
current_region_stack: Vec::new(),
fn_body_ast: None, // Phase 200-C: Initialize to None
reserved_value_ids: HashSet::new(), // Phase 201-A: Initialize to empty
@ -444,7 +422,6 @@ impl MirBuilder {
in_cleanup_block: false,
cleanup_allow_return: false,
cleanup_allow_throw: false,
hint_sink: crate::mir::hints::HintSink::new(),
temp_slot_counter: 0, // Legacy (synced with core_ctx)
suppress_pin_entry_copy_next: false,
@ -458,8 +435,6 @@ impl MirBuilder {
in_unified_boxcall_fallback: false,
recursion_depth: 0,
current_span: Span::unknown(),
source_file: None,
root_is_app_mode: None,
static_box_singletons: HashMap::new(), // Phase 21.7: methodization support
}
@ -532,25 +507,6 @@ impl MirBuilder {
self.variable_ctx.variable_map = self.variable_map.clone();
}
// ---- Phase 136 Step 6/7: MetadataContext synchronization helpers ----
/// Sync metadata_ctx changes back to legacy fields (backward compatibility)
#[allow(deprecated)]
fn sync_metadata_ctx_to_legacy(&mut self) {
self.current_span = self.metadata_ctx.current_span;
self.source_file = self.metadata_ctx.source_file.clone();
self.hint_sink = self.metadata_ctx.hint_sink.clone();
self.current_region_stack = self.metadata_ctx.current_region_stack.clone();
}
/// Sync legacy field changes to metadata_ctx (backward compatibility)
#[allow(deprecated)]
fn sync_legacy_to_metadata_ctx(&mut self) {
self.metadata_ctx.current_span = self.current_span;
self.metadata_ctx.source_file = self.source_file.clone();
self.metadata_ctx.hint_sink = self.hint_sink.clone();
self.metadata_ctx.current_region_stack = self.current_region_stack.clone();
}
/// Push/pop helpers for If merge context (best-effort; optional usage)
#[allow(deprecated)]
pub(super) fn push_if_merge(&mut self, bb: BasicBlockId) {
@ -604,23 +560,16 @@ impl MirBuilder {
// ---- Hint helpers (no-op by default) ----
// Phase 136 Step 6/7: Delegate to metadata_ctx with legacy sync
#[inline]
#[allow(deprecated)]
pub(crate) fn hint_scope_enter(&mut self, id: u32) {
self.metadata_ctx.hint_scope_enter(id);
self.hint_sink.scope_enter(id); // Legacy sync
}
#[inline]
#[allow(deprecated)]
pub(crate) fn hint_scope_leave(&mut self, id: u32) {
self.metadata_ctx.hint_scope_leave(id);
self.hint_sink.scope_leave(id); // Legacy sync
}
#[inline]
#[allow(deprecated)]
pub(crate) fn hint_join_result<S: Into<String>>(&mut self, var: S) {
let var = var.into();
self.metadata_ctx.hint_join_result(var.clone());
self.hint_sink.join_result(var); // Legacy sync
self.metadata_ctx.hint_join_result(var);
}
// ----------------------
@ -660,20 +609,15 @@ impl MirBuilder {
}
/// Hint for downstream metadata: set the logical source file name/path for the next build.
/// Phase 136 Step 6/7: Delegate to metadata_ctx with legacy sync
#[allow(deprecated)]
/// Phase 136 Step 6/7: Delegate to metadata_ctx
pub fn set_source_file_hint<S: Into<String>>(&mut self, source: S) {
let source = source.into();
self.metadata_ctx.set_source_file(source.clone());
self.source_file = Some(source); // Legacy sync
self.metadata_ctx.set_source_file(source);
}
/// Clear the source file hint (used when reusing the builder across modules).
/// Phase 136 Step 6/7: Delegate to metadata_ctx with legacy sync
#[allow(deprecated)]
/// Phase 136 Step 6/7: Delegate to metadata_ctx
pub fn clear_source_file_hint(&mut self) {
self.metadata_ctx.clear_source_file();
self.source_file = None; // Legacy sync
}
/// Resolve current source file hint (builder field or env fallback).

View File

@ -9,7 +9,7 @@ impl super::MirBuilder {
// Main expression dispatcher
pub(super) fn build_expression_impl(&mut self, ast: ASTNode) -> Result<ValueId, String> {
// Track current source span for downstream instruction emission
self.current_span = ast.span();
self.metadata_ctx.set_current_span(ast.span());
if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() {
if matches!(ast, ASTNode::Loop { .. }) {
eprintln!("[build_expression_impl] === ENTRY === processing Loop node");

View File

@ -124,7 +124,7 @@ impl super::MirBuilder {
cur_bb,
result_val,
phi_inputs,
self.current_span,
self.metadata_ctx.current_span(),
);
} else {
self.emit_instruction(super::MirInstruction::Phi {

View File

@ -30,7 +30,7 @@ impl<'a> PhiBuilderOps for ToplevelOps<'a> {
block,
dst,
inputs,
self.0.current_span,
self.0.metadata_ctx.current_span(),
);
} else {
self.0.emit_instruction(MirInstruction::Phi {

View File

@ -85,7 +85,7 @@ impl MirBuilder {
cur_bb,
merged,
inputs,
self.current_span,
self.metadata_ctx.current_span(),
);
} else {
self.emit_instruction(MirInstruction::Phi {

View File

@ -220,7 +220,7 @@ impl super::MirBuilder {
/// - Expression としての If値を使うは build_expression 経由のまま
pub(super) fn build_statement(&mut self, node: ASTNode) -> Result<ValueId, String> {
// Align current_span to this statement node before lowering expressions under it.
self.current_span = node.span();
self.metadata_ctx.set_current_span(node.span());
match node {
// Phase 212.5: Statement としての If 処理
ASTNode::If {

View File

@ -510,7 +510,7 @@ impl super::MirBuilder {
// Propagate effects on the block
block.insert_spanned_after_phis(SpannedInstruction {
inst: super::MirInstruction::Copy { dst, src },
span: self.current_span,
span: self.metadata_ctx.current_span(),
});
// Lightweight metadata propagation (unified)
crate::mir::builder::metadata::propagate::propagate(self, src, dst);

View File

@ -139,7 +139,7 @@ impl LoopBuilderApi for super::builder::MirBuilder {
block,
dst,
inputs,
self.current_span,
self.metadata_ctx.current_span(),
);
Ok(())
} else {

View File

@ -66,7 +66,7 @@ pub fn observe_control_form(builder: &mut MirBuilder, form: &ControlForm) {
classify_slots_from_variable_map(builder)
};
let parent = builder.current_region_stack.last().copied();
let parent = builder.metadata_ctx.current_region_stack().last().copied();
let region = Region {
id,
@ -121,7 +121,7 @@ pub fn observe_function_region(builder: &mut MirBuilder) {
slots: Vec::new(),
};
builder.current_region_stack.push(id);
builder.metadata_ctx.push_region(id);
get_global_ring0().log.debug(&format!(
"[region/observe] fn={} id={:?} kind={:?} entry={:?} exits={:?} slots={:?}",
@ -134,7 +134,7 @@ pub fn pop_function_region(builder: &mut MirBuilder) {
if !is_region_trace_on() {
return;
}
let _ = builder.current_region_stack.pop();
let _ = builder.metadata_ctx.pop_region();
}
fn classify_slots_from_registry(reg: &mut FunctionSlotRegistry) -> Vec<SlotMetadata> {

View File

@ -77,7 +77,7 @@ impl MirBuilder {
cur_bb,
phi_val,
inputs,
self.current_span,
self.metadata_ctx.current_span(),
);
} else {
// フォールバック: 直接emit主にテストや特殊ケース用
@ -143,7 +143,7 @@ impl MirBuilder {
cur_bb,
dst,
inputs,
self.current_span,
self.metadata_ctx.current_span(),
);
} else {
// フォールバック: 直接emit主にテストや特殊ケース用