Fix IR for try (#13811)

# Description
Fixes a bug in the IR for `try` to match that of the regular evaluator
(continuing from #13515):
```nushell
# without IR:
try { ^false } catch { 'caught' } # == 'caught'

# with IR:
try { ^false } catch { 'caught' } # error, non-zero exit code
```

In this PR, both now evaluate to `caught`. For the implementation, I had
to add another instruction, and feel free to suggest better
alternatives. In the future, it might be possible to get rid of this
extra instruction.

# User-Facing Changes
Bug fix, `try { ^false } catch { 'caught' }` now works in IR.
This commit is contained in:
Ian Manske
2024-09-09 19:44:04 -07:00
committed by GitHub
parent 6600b3edfb
commit 493850b1bf
7 changed files with 36 additions and 5 deletions

View File

@ -94,6 +94,9 @@ impl<'a> fmt::Display for FmtInstruction<'a> {
Instruction::Drain { src } => {
write!(f, "{:WIDTH$} {src}", "drain")
}
Instruction::WriteToOutDests { src } => {
write!(f, "{:WIDTH$} {src}", "write-to-out-dests")
}
Instruction::LoadVariable { dst, var_id } => {
let var = FmtVar::new(self.engine_state, *var_id);
write!(f, "{:WIDTH$} {dst}, {var}", "load-variable")

View File

@ -123,6 +123,9 @@ pub enum Instruction {
/// code, and invokes any available error handler with Empty, or if not available, returns an
/// exit-code-only stream, leaving the block.
Drain { src: RegId },
/// Write to the output destinations in the stack
// TODO: see if it's possible to remove this
WriteToOutDests { src: RegId },
/// Load the value of a variable into the `dst` register
LoadVariable { dst: RegId, var_id: VarId },
/// Store the value of a variable from the `src` register
@ -287,6 +290,7 @@ impl Instruction {
Instruction::Span { src_dst } => Some(src_dst),
Instruction::Drop { .. } => None,
Instruction::Drain { .. } => None,
Instruction::WriteToOutDests { .. } => None,
Instruction::LoadVariable { dst, .. } => Some(dst),
Instruction::StoreVariable { .. } => None,
Instruction::DropVariable { .. } => None,