Code quality improvements after Phase 285A1 implementation: **Task 1: Dead Code Cleanup** - Removed unnecessary #[allow(dead_code)] from emit_weak_load() - Function is actively used in weak_to_strong() method handler **Task 2: Unused Import Removal (cargo fix)** - edgecfg/api/mod.rs: Removed seq, if_, loop_, cleanup, verify_frag_invariants - pattern3.rs: Removed BinaryOperator - pattern2/api/mod.rs: Removed PromoteStepResult - jump.rs: Removed EffectMask, Span - Result: 6 unused imports eliminated **Task 3: Deprecated Pattern Removal** - Fixed 4 PlanKind::LoopWithPost deprecated warnings - Updated to Phase 142 P0 architecture (statement-level normalization) - Files: normalized_shadow_suffix_router_box.rs, routing.rs, execute_box.rs, plan.rs - Removed 2 deprecated tests: test_loop_with_post_* **Task 4: WeakFieldValidatorBox Boxification** - Extracted weak field validation logic into dedicated Box - New file: src/mir/builder/weak_field_validator.rs (147 lines) - fields.rs: 277 → 237 lines (-40 lines, -14.4%) - Added 5 unit tests for validation logic - Follows Phase 33 boxification principles (single responsibility, testability, reusability) **Metrics**: - Code reduction: -40 lines in fields.rs - Test coverage: +5 unit tests - Warnings fixed: 4 deprecated warnings - Imports cleaned: 6 unused imports 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Phase 285: Box lifecycle / weakref / finalization / GC conformance
Status: Planned (design-first)
Goal
Box の生存期間(強参照/弱参照/解放/最終化/GC)を SSOT として固定し、移行期間でも意味論が割れない状態にする。
Language-level SSOT:
- Lifecycle/weak/fini/GC policy:
docs/reference/language/lifecycle.md - Truthiness +
null/void:docs/reference/language/types.md
This Phase document is not the language SSOT; it tracks implementation status, backend gaps, and acceptance criteria.
Why now
- JoinIR/Plan/compose の収束が進むほど、実行時の “値の寿命” の揺れが目立つ。
- weakref/finalization は「実装が仕様」になりやすく、後から直すコストが最大級。
- LLVM harness 側は未対応の可能性が高く、差分を “仕様として明文化” しないと再現/調査が難しい。
SSOT References (current code)
- weakref の値表現:
src/value.rs(NyashValue::WeakBox) - finalization:
src/finalization.rs - Box trait:
src/box_trait.rs(SharedNyashBox = Arc<dyn NyashBox>) - Scope tracking:
src/scope_tracker.rs(Box の登録/スコープ)
Snapshot(今わかっていること)
- weakref は
Weak<Mutex<dyn NyashBox>>で保持される(NyashValue::WeakBox) WeakBoxのto_string()はweak_to_strong()を試み、WeakRef(null)表示になりうる(観測可能)src/value.rsに weakref の drop 挙動を固定する unit test がある(test_weak_reference_drop)
Responsibility Map(どこが仕様を決めるか)
- SSOT(意味):
docs/reference/language/*(言語レベルのSSOT) - Conformance: Rust VM / LLVM harness / WASM / JIT など各バックエンド実装
- 観測の固定: fixture/smoke(Phase 285 P2 で作る)
用語(P0で固定する)
- Strong reference: 所有参照(
Arc等で Box を保持) - Weak reference: 非所有参照(
Weak/weak_to_strong()が失敗しうる) - Weak-to-strong: weak → strong の昇格(成功/失敗が意味論)
- Roots: 解放/GC から保護される参照集合(stack/local/global/handle/plugin)
- Finalizer: 解放に伴う最終化処理(もし存在するなら)
P0 decisions (docs-only)
- Weak の観測は
weak_to_strong()で行い、失敗値はnull(= runtimeVoidの別名)。 cleanup(Stage‑3 block-postfix)が「出口で必ず走る」決定的 cleanup を保証する(catchの有無に関係なく、常に実行)。- GC は意味論ではなく補助(GC off で cycle はリークしうる)。
- ByRef (
RefGet/RefSet) は non-owning / non-escaping(寿命・弱参照・GC の道具にしない)。
RUNBOOK caveat (implementation reality)
The runbook assumes WeakRef infrastructure exists in the VM and lowering. If any of the following are missing, treat weak smokes as unsupported and scope to exit-time leak report first:
weak(x)parse/lower- VM handler for MIR WeakRef/WeakNew/WeakLoad
- language-surface
weak_to_strong()on WeakRef
Questions to Answer (P0/P1)
- weakref の “生存判定” は何で観測できるか(
toString/is_alive/weak_to_strongAPI など) - finalizer は存在するか / いつ発火するか(drop 時?GC 時?明示 API?)
- finalizer 内での禁止事項(再入、例外、I/O、allocation)をどうするか
- LLVM harness の扱い(現状未対応なら “未対応として SSOT 化”)
Scope (proposed)
P0(docs-only)
- 用語の固定(strong/weak/roots/finalizer/collection)
- 仕様の固定(weakref の weak_to_strong 成否、finalizer の発火条件、禁止事項)
- “LLVM harness の扱い” を明文化(未対応なら未対応として SSOT に書く)
P1(investigation)
- Rust VM の現状実装の棚卸し(どこで roots が形成され、どこで解放/最終化されるか)
- LLVM harness の現状調査(弱参照/GC が無い場合は差分として記録)
P2(smoke)
- weakref の最小 fixture/smoke を作り、挙動を固定する
- VM: stdout/exit code で固定
- LLVM: 未対応なら “スキップ理由” を smoke で明示
Non-goals
- GC アルゴリズム刷新(RC→tracing 等の設計変更)
- LLVM harness に同等機能を “一気に” 実装(差分の記録→段階導入を優先)
Acceptance criteria (P2+)
- VM と LLVM で、weak が仕様通り動作する(
weak_to_strong()成功/失敗が一致、失敗はnull)。 - 強参照サイクルを意図的に作ったとき、(GC off なら)回収されないことが観測できる。
- 終了時に「強参照が残っている root」をデバッグ出力できる(default-off の診断フラグ)。
- これは意味論ではなく診断であり、ON/OFF でプログラムの意味を変えない。