unify the *-BuiltinVar parser errors (#8944)

# Description

this pr condenses `MutBuiltinVar`, `LetBuiltinVar` and `ConstBuiltinVar`
into one error:
```nu
Error: nu::parser::name_is_builtin_var

  × `in` used as variable name.
   ╭─[entry #69:1:1]
 1 │ let in = 420
   ·     ─┬
   ·      ╰── already a builtin variable
   ╰────
  help: 'in' is the name of a builtin Nushell variable and cannot be used
        as a variable name
```

it also fixes this case which was previously not handled
```nu
let $nu = 420 # this variable would have been 'lost'
```
This commit is contained in:
mike
2023-04-20 20:44:31 +03:00
committed by GitHub
parent d339902dc6
commit fb72da0e82
4 changed files with 40 additions and 29 deletions

View File

@ -142,26 +142,14 @@ pub enum ParseError {
)]
AssignInPipeline(String, String, String, #[label("'{0}' in pipeline")] Span),
#[error("Let used with builtin variable name.")]
#[error("`{0}` used as variable name.")]
#[diagnostic(
code(nu::parser::let_builtin_var),
help("'{0}' is the name of a builtin Nushell variable. `let` cannot assign to it.")
code(nu::parser::name_is_builtin_var),
help(
"'{0}' is the name of a builtin Nushell variable and cannot be used as a variable name"
)
)]
LetBuiltinVar(String, #[label("already a builtin variable")] Span),
#[error("Const used with builtin variable name.")]
#[diagnostic(
code(nu::parser::let_builtin_var),
help("'{0}' is the name of a builtin Nushell variable. `const` cannot assign to it.")
)]
ConstBuiltinVar(String, #[label("already a builtin variable")] Span),
#[error("Mut used with builtin variable name.")]
#[diagnostic(
code(nu::parser::let_builtin_var),
help("'{0}' is the name of a builtin Nushell variable. `mut` cannot assign to it.")
)]
MutBuiltinVar(String, #[label("already a builtin variable")] Span),
NameIsBuiltinVar(String, #[label("already a builtin variable")] Span),
#[error("Incorrect value")]
#[diagnostic(code(nu::parser::incorrect_value), help("{2}"))]
@ -445,9 +433,7 @@ impl ParseError {
ParseError::CantAliasExpression(_, s) => *s,
ParseError::BuiltinCommandInPipeline(_, s) => *s,
ParseError::AssignInPipeline(_, _, _, s) => *s,
ParseError::LetBuiltinVar(_, s) => *s,
ParseError::MutBuiltinVar(_, s) => *s,
ParseError::ConstBuiltinVar(_, s) => *s,
ParseError::NameIsBuiltinVar(_, s) => *s,
ParseError::CaptureOfMutableVar(s) => *s,
ParseError::IncorrectValue(_, s, _) => *s,
ParseError::MultipleRestParams(s) => *s,