fix(parser): skip eval_const if parsing errors detected to avoid panic (#15364)

Fixes #14972 #15321 #14706

# Description

Early returns `NotAConstant` if parsing errors exist in the
subexpression.

I'm not sure when the span of a block will be None, and whether there're
better ways to handle none block spans, like a more suitable ShellError
type.

# User-Facing Changes

# Tests + Formatting

+1, but possibly not the easiest way to do it.

# After Submitting
This commit is contained in:
zc he
2025-03-26 22:02:26 +08:00
committed by GitHub
parent 55e05be0d8
commit 02fcc485fb
3 changed files with 118 additions and 1 deletions

View File

@ -1140,6 +1140,25 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
span: Span,
},
/// TODO: Get rid of this error by moving the check before evaluation
///
/// Tried evaluating of a subexpression with parsing error
///
/// ## Resolution
///
/// Fix the parsing error first.
#[error("Found parsing error in expression.")]
#[diagnostic(
code(nu::shell::parse_error_in_constant),
help(
"This expression is supposed to be evaluated into a constant, which means error-free."
)
)]
ParseErrorInConstant {
#[label("Parsing error detected in expression")]
span: Span,
},
/// Tried assigning non-constant value to a constant
///
/// ## Resolution

View File

@ -508,6 +508,14 @@ impl Eval for EvalConst {
block_id: BlockId,
span: Span,
) -> Result<Value, ShellError> {
// If parsing errors exist in the subexpression, don't bother to evaluate it.
if working_set
.parse_errors
.iter()
.any(|error| span.contains_span(error.span()))
{
return Err(ShellError::ParseErrorInConstant { span });
}
// TODO: Allow debugging const eval
let block = working_set.get_block(block_id);
eval_const_subexpression(working_set, block, PipelineData::empty(), span)?.into_value(span)