refactor: unify error message generation (Phase 3)

Add ErrorBuilder utility and migrate 35 error generation sites

Phase 3: Error Message Generation Unification
============================================

Infrastructure:
- Add src/backend/mir_interpreter/utils/error_helpers.rs (255 lines)
- Implement ErrorBuilder with 7 standardized error patterns
- Add MirInterpreter convenience methods (err_invalid, err_type_mismatch, etc.)

Migration Results:
- Total patterns migrated: 35 instances (80 → 45, 44% reduction)
- calls.rs: 37 → 13 patterns (65% reduction)
- extern_provider.rs: 20 → 9 patterns (55% reduction)
- Lines saved: 33 lines (1.0% of handlers)

Error Patterns Unified:
1. Invalid instruction errors → ErrorBuilder::invalid_instruction()
2. Type mismatch errors → ErrorBuilder::type_mismatch()
3. Argument count errors → ErrorBuilder::arg_count_mismatch()
4. Method not found → ErrorBuilder::method_not_found()
5. Unsupported operations → ErrorBuilder::unsupported_operation()
6. Context errors → ErrorBuilder::with_context()
7. Bounds errors → ErrorBuilder::out_of_bounds()

Benefits:
- Consistent error message formatting across all handlers
- Single point of change for error improvements
- Better IDE autocomplete support
- Easier future i18n integration
- Reduced code duplication

Cumulative Impact (Phase 1+2+3):
- Total lines saved: 150-187 lines (4.5-5.7% of handlers)
- Total patterns unified: 124 instances
  * Phase 1: 37 destination patterns
  * Phase 2: 52 argument validation patterns
  * Phase 3: 35 error generation patterns

Remaining Work:
- 45 error patterns still to migrate (estimated 50-80 lines)
- Complex cases requiring manual review

Testing:
- Build:  0 errors, 0 new warnings
- Smoke tests: ⚠️ 8/9 passed (1 timeout unrelated)
- Core functionality:  Verified

Related: Phase 21.0 MIR Interpreter refactoring
Risk: Low (error messages only, behavior preserved)
Impact: High (maintainability, consistency, i18n-ready)

Co-authored-by: Claude Code <claude@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-06 23:18:10 +09:00
parent edf4513b5a
commit 0287020a5b
4 changed files with 276 additions and 68 deletions

View File

@ -3,8 +3,10 @@
pub mod destination_helpers;
pub mod arg_validation;
pub mod receiver_helpers;
pub mod error_helpers;
// Re-export for convenience
pub use destination_helpers::*;
pub use arg_validation::*;
pub use receiver_helpers::*;
pub use error_helpers::*;