Convert remainder of ShellError variants to named fields (#11276)

# Description

Removed variants that are no longer in use:
* `NoFile*`
* `UnexpectedAbbrComponent`

Converted:
* `OutsideSpannedLabeledError`
* `EvalBlockWithInput`
* `Break`
* `Continue`
* `Return`
* `NotAConstant`
* `NotAConstCommand`
* `NotAConstHelp`
* `InvalidGlobPattern`
* `ErrorExpandingGlob`

Fixes #10700 

# User-Facing Changes

None

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A
This commit is contained in:
Eric Hodel
2023-12-09 16:46:21 -08:00
committed by GitHub
parent ca05553fc6
commit 3e5f81ae14
22 changed files with 328 additions and 298 deletions

View File

@ -212,13 +212,13 @@ fn eval_const_call(
let decl = working_set.get_decl(call.decl_id);
if !decl.is_const() {
return Err(ShellError::NotAConstCommand(call.head));
return Err(ShellError::NotAConstCommand { span: call.head });
}
if !decl.is_known_external() && call.named_iter().any(|(flag, _, _)| flag.item == "help") {
// It would require re-implementing get_full_help() for const evaluation. Assuming that
// getting help messages at parse-time is rare enough, we can simply disallow it.
return Err(ShellError::NotAConstHelp(call.head));
return Err(ShellError::NotAConstHelp { span: call.head });
}
decl.run_const(working_set, call, input)
@ -233,7 +233,7 @@ pub fn eval_const_subexpression(
for pipeline in block.pipelines.iter() {
for element in pipeline.elements.iter() {
let PipelineElement::Expression(_, expr) = element else {
return Err(ShellError::NotAConstant(span));
return Err(ShellError::NotAConstant { span });
};
input = eval_constant_with_input(working_set, expr, input)?
@ -288,7 +288,7 @@ impl Eval for EvalConst {
_: String,
span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn eval_var(
@ -299,7 +299,7 @@ impl Eval for EvalConst {
) -> Result<Value, ShellError> {
match working_set.get_variable(var_id).const_val.as_ref() {
Some(val) => Ok(val.clone()),
None => Err(ShellError::NotAConstant(span)),
None => Err(ShellError::NotAConstant { span }),
}
}
@ -322,7 +322,7 @@ impl Eval for EvalConst {
span: Span,
) -> Result<Value, ShellError> {
// TODO: It may be more helpful to give not_a_const_command error
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn eval_subexpression(
@ -346,7 +346,7 @@ impl Eval for EvalConst {
_: bool,
expr_span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(expr_span))
Err(ShellError::NotAConstant { span: expr_span })
}
fn eval_assignment(
@ -358,7 +358,7 @@ impl Eval for EvalConst {
_op_span: Span,
expr_span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(expr_span))
Err(ShellError::NotAConstant { span: expr_span })
}
fn eval_row_condition_or_closure(
@ -367,7 +367,7 @@ impl Eval for EvalConst {
_: usize,
span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn eval_string_interpolation(
@ -376,11 +376,11 @@ impl Eval for EvalConst {
_: &[Expression],
span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn eval_overlay(_: &StateWorkingSet, span: Span) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn eval_glob_pattern(
@ -389,10 +389,10 @@ impl Eval for EvalConst {
_: String,
span: Span,
) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(span))
Err(ShellError::NotAConstant { span })
}
fn unreachable(expr: &Expression) -> Result<Value, ShellError> {
Err(ShellError::NotAConstant(expr.span))
Err(ShellError::NotAConstant { span: expr.span })
}
}

View File

@ -944,14 +944,6 @@ pub enum ShellError {
span: Span,
},
// These three are unused. Remove?
#[error("No file to be removed")]
NoFileToBeRemoved(),
#[error("No file to be moved")]
NoFileToBeMoved(),
#[error("No file to be copied")]
NoFileToBeCopied(),
/// Error while trying to read a file
///
/// ## Resolution
@ -1084,22 +1076,30 @@ pub enum ShellError {
},
/// This is a generic error type used for different situations.
#[error("{1}")]
#[error("{error}")]
#[diagnostic()]
OutsideSpannedLabeledError(#[source_code] String, String, String, #[label("{2}")] Span),
OutsideSpannedLabeledError {
#[source_code]
src: String,
error: String,
msg: String,
#[label("{msg}")]
span: Span,
},
/// Attempted to use a command that has been removed from Nushell.
///
/// ## Resolution
///
/// Check the help for the new suggested command and update your script accordingly.
#[error("Removed command: {0}")]
#[error("Removed command: {removed}")]
#[diagnostic(code(nu::shell::removed_command))]
RemovedCommand(
String,
String,
#[label = "'{0}' has been removed from Nushell. Please use '{1}' instead."] Span,
),
RemovedCommand {
removed: String,
replacement: String,
#[label("'{removed}' has been removed from Nushell. Please use '{replacement}' instead.")]
span: Span,
},
/// Non-Unicode input received.
///
@ -1110,32 +1110,38 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::non_unicode_input))]
NonUnicodeInput,
/// Unexpected abbr component.
///
/// ## Resolution
///
/// Check the path abbreviation to ensure that it is valid.
#[error("Unexpected abbr component `{0}`.")]
#[diagnostic(code(nu::shell::unexpected_path_abbreviateion))]
UnexpectedAbbrComponent(String),
// It should be only used by commands accepts block, and accept inputs from pipeline.
/// Failed to eval block with specific pipeline input.
#[error("Eval block failed with pipeline input")]
#[diagnostic(code(nu::shell::eval_block_with_input))]
EvalBlockWithInput(#[label("source value")] Span, #[related] Vec<ShellError>),
EvalBlockWithInput {
#[label("source value")]
span: Span,
#[related]
sources: Vec<ShellError>,
},
/// Break event, which may become an error if used outside of a loop
#[error("Break used outside of loop")]
Break(#[label = "used outside of loop"] Span),
Break {
#[label("used outside of loop")]
span: Span,
},
/// Continue event, which may become an error if used outside of a loop
#[error("Continue used outside of loop")]
Continue(#[label = "used outside of loop"] Span),
Continue {
#[label("used outside of loop")]
span: Span,
},
/// Return event, which may become an error if used outside of a function
#[error("Return used outside of function")]
Return(#[label = "used outside of function"] Span, Box<Value>),
Return {
#[label("used outside of function")]
span: Span,
value: Box<Value>,
},
/// The code being executed called itself too many times.
///
@ -1206,7 +1212,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
code(nu::shell::not_a_constant),
help("Only a subset of expressions are allowed constants during parsing. Try using the 'const' command or typing the value literally.")
)]
NotAConstant(#[label = "Value is not a parse-time constant"] Span),
NotAConstant {
#[label("Value is not a parse-time constant")]
span: Span,
},
/// Tried running a command that is not const-compatible
///
@ -1219,7 +1228,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
code(nu::shell::not_a_const_command),
help("Only a subset of builtin commands, and custom commands built only from those commands, can run at parse time.")
)]
NotAConstCommand(#[label = "This command cannot run at parse time."] Span),
NotAConstCommand {
#[label("This command cannot run at parse time.")]
span: Span,
},
/// Tried getting a help message at parse time.
///
@ -1231,7 +1243,10 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
code(nu::shell::not_a_const_help),
help("Help messages are currently not supported to be constants.")
)]
NotAConstHelp(#[label = "Cannot get help message at parse time."] Span),
NotAConstHelp {
#[label("This command cannot run at parse time.")]
span: Span,
},
/// Invalid glob pattern
///
@ -1243,7 +1258,11 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
code(nu::shell::invalid_glob_pattern),
help("Refer to xxx for help on nushell glob patterns.")
)]
InvalidGlobPattern(String, #[label = "{0}"] Span),
InvalidGlobPattern {
msg: String,
#[label("{msg}")]
span: Span,
},
/// Error expanding glob pattern
///
@ -1256,7 +1275,11 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
help("Correct glob pattern or file access issue")
)]
//todo: add error detail
ErrorExpandingGlob(String, #[label = "{0}"] Span),
ErrorExpandingGlob {
msg: String,
#[label("{msg}")]
span: Span,
},
/// Tried spreading a non-list inside a list.
///