Phase 138 Documentation: - Created docs/development/current/main/phases/phase-138/README.md - Goal: Extract return lowering to shared Box for SSOT - SSOT: common/return_value_lowerer_box.rs - Supported: Variable, Integer literal, Add expression (x + 2, 5 + 3) - Implementation: New files, modified files, call sites - Tests: 5 unit tests + all regressions PASS - Architecture Impact: Code reduction, maintainability, testability - Phase 139 準備: Next step clearly indicated (post_if_post_k.rs unification) 10-Now.md Updates: - Added Phase 138 entry (ReturnValueLowererBox SSOT) - Implementation details and test coverage - Design decision: Phase 138 P0 migrated loop paths only - Link to Phase 138 README 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
5.8 KiB
Phase 138: ReturnValueLowererBox - Return Lowering SSOT
Date: 2025-12-18 Status: DONE ✅ Scope: Extract return lowering to shared Box (behavior unchanged)
Goal
Establish SSOT for return value lowering by extracting logic to ReturnValueLowererBox:
- Extract
lower_return_value_to_vid()fromloop_true_break_once.rs - Create shared
ReturnValueLowererBoxfor Normalized shadow paths - Keep 挙動不変: Phase 136/137 behavior unchanged
- Keep Phase 139 準備: Prepare for post_if_post_k.rs unification
Scope
✅ In Scope (Phase 138 P0)
- Boxification: Extract return lowering to
common/return_value_lowerer_box.rs - Migration:
loop_true_break_once.rsuses Box (2 call sites) - Tests: 5 unit tests for all supported patterns
- Regression: All Phase 136/137 smokes unchanged
❌ Out of Scope (Phase 139)
- post_if_post_k.rs: Not modified (different responsibility)
- Unification planned for Phase 139 P0
SSOT Location
File: src/mir/control_tree/normalized_shadow/common/return_value_lowerer_box.rs
Function: ReturnValueLowererBox::lower_to_value_id()
Supported Patterns (Phase 136-137)
- Variable:
return x→ env lookup → ValueId - Integer literal:
return 7→ Const generation → ValueId - Add expression:
return x + 2orreturn 5 + 3→ BinOp(Add, lhs, rhs) → ValueId
Out-of-Scope Patterns
- Other operators:
return x - 2→Ok(None)(fallback) - Variable + variable:
return x + y→Ok(None)(fallback) - Nested expressions:
return (x + 2) + 3→Ok(None)(fallback)
Implementation
New Files
-
src/mir/control_tree/normalized_shadow/common/return_value_lowerer_box.rs(~300 lines)- Main function:
lower_to_value_id() - Helper:
lower_binary_op()for Add expressions - 5 comprehensive unit tests
- Main function:
-
src/mir/control_tree/normalized_shadow/common/mod.rs- Module export for common utilities
Modified Files
-
src/mir/control_tree/normalized_shadow/mod.rs- Added
pub mod common;
- Added
-
src/mir/control_tree/normalized_shadow/loop_true_break_once.rs- Removed
lower_return_value_to_vid()method (~115 lines) - Added import:
use super::common::return_value_lowerer_box::ReturnValueLowererBox; - Updated 2 call sites:
- Line 417: post_k return processing
- Line 481: k_exit return processing
- Updated SSOT documentation (lines 29-43)
- Removed
Call Sites (Phase 138 P0)
File: loop_true_break_once.rs
- post_k return processing (line 417):
match ReturnValueLowererBox::lower_to_value_id(
value_ast,
&mut post_k_func.body,
&mut next_value_id,
&env_post_k
)? {
Some(vid) => { post_k_func.body.push(JoinInst::Ret { value: Some(vid) }); }
None => { return Ok(None); }
}
- k_exit return processing (line 481):
match ReturnValueLowererBox::lower_to_value_id(
value_ast,
&mut k_exit_func.body,
&mut next_value_id,
&env_k_exit
)? {
Some(vid) => { k_exit_func.body.push(JoinInst::Ret { value: Some(vid) }); }
None => { return Ok(None); }
}
Unit Tests (5 tests)
File: src/mir/control_tree/normalized_shadow/common/return_value_lowerer_box.rs
test_lower_variable- Variable lookup in envtest_lower_integer_literal- Const instruction generationtest_lower_add_var_plus_int-x + 2patterntest_lower_add_int_plus_int-5 + 3constant foldingtest_out_of_scope_subtract- Fallback for unsupported operators
Verification
# Build
cargo build --release -p nyash-rust --features llvm
# Unit tests
cargo test --lib
# Phase 137 regression (6 tests)
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_return_add_vm.sh
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_return_add_const_vm.sh
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_post_return_add_vm.sh
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_return_add_llvm_exe.sh
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_return_add_const_llvm_exe.sh
bash tools/smokes/v2/profiles/integration/apps/phase137_loop_true_break_once_post_return_add_llvm_exe.sh
# Phase 97 regression (2 tests)
bash tools/smokes/v2/profiles/integration/apps/phase97_next_non_ws_llvm_exe.sh
bash tools/smokes/v2/profiles/integration/apps/phase97_json_loader_escape_llvm_exe.sh
# Phase 131/135/136 regression
bash tools/smokes/v2/profiles/integration/apps/phase131_loop_true_break_once_vm.sh
bash tools/smokes/v2/profiles/integration/apps/phase135_loop_true_break_once_post_empty_return_vm.sh
bash tools/smokes/v2/profiles/integration/apps/phase136_loop_true_break_once_return_literal_vm.sh
Acceptance Criteria
- ✅ cargo test --lib: 1194 tests PASS (+5 new unit tests)
- ✅ Phase 137 regression: 6/6 PASS
- ✅ Phase 97 regression: 2/2 PASS
- ✅ Phase 131/135/136 regression: 3/3 PASS
- ✅ Behavior unchanged: All Phase 136/137 fixtures/smokes PASS
- ✅ SSOT established: ReturnValueLowererBox is single source of truth
- ✅ post_if_post_k.rs unchanged: Phase 139 P0 scope
Boxification Trigger
Condition: When 2+ files need identical return lowering logic
Result: Phase 138 P0 achieved this trigger with loop_true_break_once.rs migration
Next Step: Phase 139 P0 - Migrate post_if_post_k.rs to ReturnValueLowererBox
Current Status
Phase 138 - DONE ✅ (2025-12-18)
SSOT established: common/return_value_lowerer_box.rs
Return lowering unified for loop paths, ready for if-with-post paths in Phase 139.
Architecture Impact
- Code Reduction: ~115 lines removed from loop_true_break_once.rs
- Maintainability: Single location for return lowering improvements
- Testability: Isolated unit tests for return lowering logic
- Extensibility: Easy to add new return patterns in one location