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

@ -96,12 +96,12 @@ fn can_catch_infinite_recursion() {
#[test]
fn exit_code_available_in_catch_env() {
let actual = nu!("try { nu -c 'exit 42'; null } catch { $env.LAST_EXIT_CODE }");
let actual = nu!("try { nu -c 'exit 42' } catch { $env.LAST_EXIT_CODE }");
assert_eq!(actual.out, "42");
}
#[test]
fn exit_code_available_in_catch() {
let actual = nu!("try { nu -c 'exit 42'; null } catch { |e| $e.exit_code }");
let actual = nu!("try { nu -c 'exit 42' } catch { |e| $e.exit_code }");
assert_eq!(actual.out, "42");
}