Allow $env and mutable records to be mutated by = (closes #7110) (#7318)

# Description

Closes #7110. ~~Note that unlike "real" `mut` vars, $env can be deeply
mutated via stuff like `$env.PYTHON_IO_ENCODING = utf8` or
`$env.config.history.max_size = 2000`. So, it's a slightly awkward
special case, arguably justifiable because of what $env represents (the
environment variables of your system, which is essentially "outside"
normal Nushell regulations).~~
EDIT: Now allows all `mut` vars to be deeply mutated using `=`, on
request.

# User-Facing Changes

See above.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Leon
2022-12-07 03:51:55 +10:00
committed by GitHub
parent 48ade4993d
commit 9b41f9ecb8
6 changed files with 129 additions and 59 deletions

View File

@ -376,6 +376,18 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
AccessBeyondEnd(usize, #[label = "index too large (max: {0})"] Span),
/// You attempted to insert data at a list position higher than the end.
///
/// ## Resolution
///
/// To insert data into a list, assign to the last used index + 1.
#[error("Inserted at wrong row number (should be {0}).")]
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
InsertAfterNextFreeIndex(
usize,
#[label = "can't insert at index (the next available index is {0})"] Span,
),
/// You attempted to access an index when it's empty.
///
/// ## Resolution

View File

@ -878,10 +878,12 @@ impl Value {
Value::List { vals, .. } => {
if let Some(v) = vals.get_mut(*row_num) {
v.upsert_data_at_cell_path(&cell_path[1..], new_val)?
} else if vals.is_empty() {
return Err(ShellError::AccessEmptyContent(*span));
} else if vals.len() == *row_num && cell_path.len() == 1 {
// If the upsert is at 1 + the end of the list, it's OK.
// Otherwise, it's prohibited.
vals.push(new_val);
} else {
return Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span));
return Err(ShellError::InsertAfterNextFreeIndex(vals.len(), *span));
}
}
v => return Err(ShellError::NotAList(*span, v.span()?)),
@ -1266,10 +1268,12 @@ impl Value {
Value::List { vals, .. } => {
if let Some(v) = vals.get_mut(*row_num) {
v.insert_data_at_cell_path(&cell_path[1..], new_val)?
} else if vals.is_empty() {
return Err(ShellError::AccessEmptyContent(*span));
} else if vals.len() == *row_num && cell_path.len() == 1 {
// If the insert is at 1 + the end of the list, it's OK.
// Otherwise, it's prohibited.
vals.push(new_val);
} else {
return Err(ShellError::AccessBeyondEnd(vals.len() - 1, *span));
return Err(ShellError::InsertAfterNextFreeIndex(vals.len(), *span));
}
}
v => return Err(ShellError::NotAList(*span, v.span()?)),