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

@ -21,6 +21,7 @@ mod calls; // Call system modules (refactored from builder_calls)
mod binding_context; // Phase 136 follow-up (Step 4/7): BindingContext extraction
mod context; // BoxCompilationContext - 箱理論による静的Boxコンパイル時のコンテキスト分離
mod core_context; // Phase 136 follow-up (Step 2/7): CoreContext extraction
mod metadata_context; // Phase 136 follow-up (Step 6/7): MetadataContext extraction
mod variable_context; // Phase 136 follow-up (Step 5/7): VariableContext extraction
mod decls; // declarations lowering split
mod exprs; // expression lowering split
@ -125,6 +126,11 @@ pub struct MirBuilder {
/// Direct field access for backward compatibility (migration in progress).
pub(super) variable_ctx: variable_context::VariableContext,
/// Phase 136 follow-up (Step 6/7): Metadata context
/// Consolidates current_span, source_file, hint_sink, current_region_stack.
/// Direct field access for backward compatibility (migration in progress).
pub(super) metadata_ctx: metadata_context::MetadataContext,
/// [DEPRECATED] Variable name to ValueId mapping (for SSA conversion)
/// Phase 136 Step 5/7: Moved to variable_ctx.variable_map (backward compat wrapper)
/// 注意: compilation_contextがSomeの場合は使用されません
@ -204,8 +210,10 @@ pub struct MirBuilder {
/// Source size snapshot to detect when to rebuild the tail index
pub(super) method_tail_index_source_len: usize,
/// Region 観測用のスタックだよFunctionRegion がルート)。
/// [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>,
/// Phase 200-C: Original function body AST for capture analysis
@ -261,7 +269,9 @@ pub struct MirBuilder {
pub(super) cleanup_allow_return: bool,
pub(super) cleanup_allow_throw: bool,
/// Hint sink (zero-cost guidance; currently no-op)
/// [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)
@ -305,10 +315,14 @@ pub struct MirBuilder {
/// Tracks the depth of build_expression calls to detect infinite loops
pub(super) recursion_depth: usize,
/// Current AST span being lowered (used to annotate MIR instructions)
/// [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,
/// Optional source file hint for metadata/spans
/// [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
@ -349,6 +363,7 @@ impl MirBuilder {
scope_ctx: scope_context::ScopeContext::new(), // Phase 136 Step 3/7: Scope context
binding_ctx: binding_context::BindingContext::new(), // Phase 136 Step 4/7: Binding context
variable_ctx: variable_context::VariableContext::new(), // Phase 136 Step 5/7: Variable context
metadata_ctx: metadata_context::MetadataContext::new(), // Phase 136 Step 6/7: Metadata context
variable_map: BTreeMap::new(), // Phase 25.1: 決定性確保 (backward compat)
lexical_scope_stack: Vec::new(),
pending_phis: Vec::new(),
@ -475,6 +490,25 @@ 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) {
@ -526,17 +560,25 @@ 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.hint_sink.scope_enter(id);
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.hint_sink.scope_leave(id);
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) {
self.hint_sink.join_result(var.into());
let var = var.into();
self.metadata_ctx.hint_join_result(var.clone());
self.hint_sink.join_result(var); // Legacy sync
}
// ----------------------
@ -576,19 +618,27 @@ 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)]
pub fn set_source_file_hint<S: Into<String>>(&mut self, source: S) {
self.source_file = Some(source.into());
let source = source.into();
self.metadata_ctx.set_source_file(source.clone());
self.source_file = Some(source); // Legacy sync
}
/// 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)]
pub fn clear_source_file_hint(&mut self) {
self.source_file = None;
self.metadata_ctx.clear_source_file();
self.source_file = None; // Legacy sync
}
/// Resolve current source file hint (builder field or env fallback).
/// Phase 136 Step 6/7: Delegate to metadata_ctx
fn current_source_file(&self) -> Option<String> {
self.source_file
.clone()
self.metadata_ctx
.current_source_file()
.or_else(|| std::env::var("NYASH_SOURCE_FILE_HINT").ok())
}
@ -1003,7 +1053,8 @@ impl MirBuilder {
}
);
}
block.add_instruction_with_span(instruction.clone(), self.current_span);
// Phase 136 Step 6/7: Use metadata_ctx for span
block.add_instruction_with_span(instruction.clone(), self.metadata_ctx.current_span());
// Drop the mutable borrow of `block` before updating other blocks
}
// Update predecessor sets for branch/jump immediately so that