make compile errors for assignment more like the shell errors

This commit is contained in:
Devyn Cairns 2024-07-09 03:20:23 -07:00
parent fab24f8e75
commit 21f132be9f
2 changed files with 21 additions and 11 deletions

View File

@ -113,7 +113,7 @@ pub(crate) fn compile_assignment(
Expr::Var(var_id) => {
// Double check that the variable is supposed to be mutable
if !working_set.get_variable(var_id).mutable {
return Err(CompileError::ModifyImmutableVariable { span: lhs.span });
return Err(CompileError::AssignmentRequiresMutableVar { span: lhs.span });
}
builder.push(
@ -135,7 +135,7 @@ pub(crate) fn compile_assignment(
) if *var_id == ENV_VARIABLE_ID => {
// This will be an assignment to an environment variable.
let Some(PathMember::String { val: key, .. }) = path.tail.first() else {
return Err(CompileError::InvalidLhsForAssignment { span: lhs.span });
return Err(CompileError::CannotReplaceEnv { span: lhs.span });
};
let key_data = builder.data(key)?;
@ -225,7 +225,7 @@ pub(crate) fn compile_assignment(
}
},
Expr::Garbage => Err(CompileError::Garbage { span: lhs.span }),
_ => Err(CompileError::InvalidLhsForAssignment { span: lhs.span }),
_ => Err(CompileError::AssignmentRequiresVar { span: lhs.span }),
}
}

View File

@ -107,23 +107,33 @@ pub enum CompileError {
span: Span,
},
#[error("Invalid left-hand side for assignment.")]
#[error("Assignment operations require a variable.")]
#[diagnostic(
code(nu::compile::invalid_lhs_for_assignment),
help("try assigning to a variable, environment variable, or sub-path of either")
code(nu::compile::assignment_requires_variable),
help("try assigning to a variable or a cell path of a variable")
)]
InvalidLhsForAssignment {
#[label("this is not an assignable expression")]
AssignmentRequiresVar {
#[label("needs to be a variable")]
span: Span,
},
#[error("Attempted to modify immutable variable.")]
#[diagnostic(
code(nu::compile::modify_immutable_variable),
code(nu::compile::assignment_requires_mutable_variable),
help("declare the variable with `mut`, or shadow it again with `let`")
)]
ModifyImmutableVariable {
#[label("this variable was not declared as mutable")]
AssignmentRequiresMutableVar {
#[label("needs to be a mutable variable")]
span: Span,
},
#[error("Cannot replace environment.")]
#[diagnostic(
code(nu::compile::cannot_replace_env),
help("Assigning a value to '$env' is not allowed.")
)]
CannotReplaceEnv {
#[label("setting '$env' not allowed")]
span: Span,
},