refactor(llvm): Phase 286B - Unify error handling with Result<T, LlvmRunError>

This refactoring consolidates 20+ duplicated error handling patterns in LLVM mode
into a unified Result-based error handling system.

New files:
- error.rs: LlvmRunError type with code, msg, and convenience constructors
- report.rs: emit_error_and_exit() - single exit point for all LLVM errors

Changes:
- harness_executor.rs: Returns Result<i32, LlvmRunError> instead of Option<i32>
- pyvm_executor.rs: Returns Result<i32, LlvmRunError> instead of Option<i32>
- fallback_executor.rs: Returns Result<i32, LlvmRunError> instead of i32
- mod.rs: Updated all error handling to use report::emit_error_and_exit()

Benefits:
1. Unified error handling with Result<T, LlvmRunError>
2. Clear separation: Executors return errors, orchestrator handles exit
3. Single exit point guarantees leak report consistency
4. Reduced code duplication (20+ patterns consolidated)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-24 11:10:57 +09:00
parent 13545fd57e
commit 3aba574723
6 changed files with 124 additions and 93 deletions

View File

@ -3,12 +3,13 @@
//! Handles fallback execution when LLVM backends are not available.
use nyash_rust::{mir::MirModule, mir::MirInstruction};
use super::error::LlvmRunError;
/// Fallback executor Box
///
/// **Responsibility**: Execute fallback path (feature check + mock)
/// **Input**: &MirModule
/// **Output**: i32 (exit code)
/// **Output**: Result<i32, LlvmRunError> (Ok(exit_code) on success, Err on failure)
pub struct FallbackExecutorBox;
impl FallbackExecutorBox {
@ -20,18 +21,14 @@ impl FallbackExecutorBox {
///
/// Otherwise, executes mock execution that inspects the MIR
/// and returns a deterministic exit code based on Return instructions.
pub fn execute(module: &MirModule) -> i32 {
pub fn execute(module: &MirModule) -> Result<i32, LlvmRunError> {
// Fail-fast: if the user explicitly requested the llvmlite harness
// but this binary was built without the `llvm-harness` feature,
// do not silently fall back to mock.
if crate::config::env::env_bool("NYASH_LLVM_USE_HARNESS") {
crate::console_println!(
"LLVM harness requested (NYASH_LLVM_USE_HARNESS=1), but this binary was built without `--features llvm` (llvm-harness)."
);
crate::console_println!(
" Fix: cargo build --release --features llvm"
);
return 1;
return Err(LlvmRunError::fatal(
"LLVM harness requested (NYASH_LLVM_USE_HARNESS=1), but this binary was built without `--features llvm` (llvm-harness). Fix: cargo build --release --features llvm"
));
}
crate::console_println!("🔧 Mock LLVM Backend Execution:");
@ -46,11 +43,11 @@ impl FallbackExecutorBox {
match inst {
MirInstruction::Return { value: Some(_) } => {
crate::console_println!("✅ Mock exit code: 42");
return 42;
return Ok(42);
}
MirInstruction::Return { value: None } => {
crate::console_println!("✅ Mock exit code: 0");
return 0;
return Ok(0);
}
_ => {}
}
@ -59,6 +56,6 @@ impl FallbackExecutorBox {
}
crate::console_println!("✅ Mock exit code: 0");
0
Ok(0)
}
}