fix: panic of if command as a constant expr by bringing back Type::Block (#16122)

Fixes #16110. Alternative to #16120 

# Description

# User-Facing Changes

no more panic

# Tests + Formatting

+1

# After Submitting
This commit is contained in:
zc he
2025-07-08 20:45:35 +08:00
committed by GitHub
parent a674ce2dbc
commit 4da755895d
8 changed files with 24 additions and 7 deletions

View File

@ -60,11 +60,13 @@ impl Command for If {
) -> Result<PipelineData, ShellError> {
let call = call.assert_ast_call()?;
let cond = call.positional_nth(0).expect("checked through parser");
let then_block = call
.positional_nth(1)
.expect("checked through parser")
let then_expr = call.positional_nth(1).expect("checked through parser");
let then_block = then_expr
.as_block()
.expect("internal error: missing block");
.ok_or_else(|| ShellError::TypeMismatch {
err_message: "expected block".into(),
span: then_expr.span,
})?;
let else_case = call.positional_nth(2);
if eval_constant(working_set, cond)?.as_bool()? {