Spanned Value step 1: span all value cases (#10042)

# Description

This doesn't really do much that the user could see, but it helps get us
ready to do the steps of the refactor to split the span off of Value, so
that values can be spanless. This allows us to have top-level values
that can hold both a Value and a Span, without requiring that all values
have them.

We expect to see significant memory reduction by removing so many
unnecessary spans from values. For example, a table of 100,000 rows and
5 columns would have a savings of ~8megs in just spans that are almost
always duplicated.

# User-Facing Changes

Nothing yet

# 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 -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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:
JT
2023-08-25 08:48:05 +12:00
committed by GitHub
parent 8da27a1a09
commit 1e3e034021
234 changed files with 1153 additions and 868 deletions

View File

@ -90,8 +90,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value {
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -256,8 +256,9 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value {
exp_input_type: "integer, filesize, string, date, duration, binary or bool".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.expect_span(),
src_span: other.span(),
}),
span,
},
}
}

View File

@ -160,8 +160,9 @@ fn operate(value: Value, head: Span, signed: bool, number_size: NumberBytes) ->
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
},
}

View File

@ -90,8 +90,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value {
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -117,6 +117,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -146,8 +147,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -121,6 +121,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -150,8 +151,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -129,6 +129,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -140,6 +141,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -169,8 +171,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -119,6 +119,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -130,6 +131,7 @@ where
None,
Vec::new(),
)),
span,
},
}
}
@ -159,8 +161,9 @@ fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: Num
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -89,8 +89,9 @@ fn operate(value: Value, target: i64, head: Span) -> Value {
exp_input_type: "integer".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -91,8 +91,9 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
exp_input_type: "float , integer or filesize".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.expect_span(),
src_span: other.span(),
}),
span,
},
}
}

View File

@ -38,7 +38,7 @@ fn vertical_rotate_value(
}
_ => Err(ShellError::TypeMismatch {
err_message: "list".to_string(),
span: value.span()?,
span: value.span(),
}),
}
}
@ -85,7 +85,7 @@ fn horizontal_rotate_value(
}
_ => Err(ShellError::TypeMismatch {
err_message: "record".to_string(),
span: value.span()?,
span: value.span(),
}),
}
}

View File

@ -251,7 +251,10 @@ fn process_cell(
redirect_stderr,
) {
Ok(pd) => pd.into_value(span),
Err(e) => Value::Error { error: Box::new(e) },
Err(e) => Value::Error {
error: Box::new(e),
span,
},
}
}

View File

@ -89,6 +89,7 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
head,
span,
)),
span,
}
}
}
@ -98,8 +99,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value {
head,
span,
)),
span,
}
}
}
@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -90,6 +90,7 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
head,
span,
)),
span,
}
}
}
@ -99,8 +100,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -77,8 +77,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -88,8 +88,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value {
head,
span,
)),
span: head,
}
}
}
@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -97,8 +97,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -79,8 +79,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -85,8 +85,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -79,6 +79,7 @@ fn operate(value: Value, head: Span) -> Value {
head,
span,
)),
span,
}
}
}
@ -88,8 +89,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -97,8 +97,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -79,8 +79,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -95,8 +95,9 @@ fn operate(value: Value, head: Span, use_degrees: bool) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -78,8 +78,9 @@ fn operate(value: Value, head: Span) -> Value {
exp_input_type: "numeric".into(),
wrong_type: other.get_type().to_string(),
dst_span: head,
src_span: other.expect_span(),
src_span: other.span(),
}),
span: head,
},
}
}

View File

@ -140,6 +140,7 @@ fn operate(
if let Err(error) = r {
return Value::Error {
error: Box::new(error),
span: head,
};
}
}
@ -170,6 +171,7 @@ fn action(
"please supply foreground and/or background color parameters".into(),
span: command_span,
}),
span: command_span,
}
}
(None, None, None, Some(bg_end)) => {
@ -295,8 +297,9 @@ fn action(
Value::Error {
error: Box::new(ShellError::TypeMismatch {
err_message: got,
span: other.span().unwrap_or(command_span),
span: other.span(),
}),
span: other.span(),
}
}
}

View File

@ -98,7 +98,7 @@ fn operate(
if column_paths.is_empty() {
input.map(
move |v| process_value(&v, &text, command_span),
move |v| process_value(&v, &text),
engine_state.ctrlc.clone(),
)
} else {
@ -116,20 +116,18 @@ fn process_each_path(
command_span: Span,
) -> Value {
for path in column_paths {
let ret = value.update_cell_path(
&path.members,
Box::new(|v| process_value(v, text, command_span)),
);
let ret = value.update_cell_path(&path.members, Box::new(|v| process_value(v, text)));
if let Err(error) = ret {
return Value::Error {
error: Box::new(error),
span: command_span,
};
}
}
value
}
fn process_value(value: &Value, text: &Option<String>, command_span: Span) -> Value {
fn process_value(value: &Value, text: &Option<String>) -> Value {
match value {
Value::String { val, span } => {
let text = text.as_deref().unwrap_or(val.as_str());
@ -142,8 +140,9 @@ fn process_value(value: &Value, text: &Option<String>, command_span: Span) -> Va
Value::Error {
error: Box::new(ShellError::TypeMismatch {
err_message: got,
span: other.span().unwrap_or(command_span),
span: other.span(),
}),
span: other.span(),
}
}
}

View File

@ -113,8 +113,9 @@ fn action(
"value originates from here".into(),
command_span,
// This line requires the Value::Error {} match above.
input.expect_span(),
input.span(),
)),
span: command_span,
},
},
Value::String { val, .. } => {
@ -125,8 +126,9 @@ fn action(
"value originates from here".into(),
command_span,
// This line requires the Value::Error {} match above.
input.expect_span(),
input.span(),
)),
span: command_span,
},
ActionType::Decode => match hex_decode(val.as_ref()) {
@ -139,6 +141,7 @@ fn action(
None,
Vec::new(),
)),
span: command_span,
},
Err(HexDecodingError::InvalidDigit(index, digit)) => Value::Error {
error: Box::new(ShellError::GenericError(
@ -148,6 +151,7 @@ fn action(
None,
Vec::new(),
)),
span: command_span,
},
},
}
@ -155,8 +159,9 @@ fn action(
other => Value::Error {
error: Box::new(ShellError::TypeMismatch {
err_message: format!("string or binary, not {}", other.get_type()),
span: other.span().unwrap_or(command_span),
span: other.span(),
}),
span: other.span(),
},
}
}

View File

@ -55,7 +55,7 @@ impl Command for Format {
Err(e) => Err(e),
Ok(pattern) => {
let string_pattern = pattern.as_string()?;
let string_span = pattern.span()?;
let string_span = pattern.span();
// the string span is start as `"`, we don't need the character
// to generate proper span for sub expression.
let ops = extract_formatting_operations(
@ -233,13 +233,13 @@ fn format(
}
}
}
Value::Error { error } => return Err(*error.clone()),
Value::Error { error, .. } => return Err(*error.clone()),
_ => {
return Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".to_string(),
wrong_type: val.get_type().to_string(),
dst_span: head_span,
src_span: val.expect_span(),
src_span: val.span(),
})
}
}
@ -252,12 +252,12 @@ fn format(
}
// Unwrapping this ShellError is a bit unfortunate.
// Ideally, its Span would be preserved.
Value::Error { error } => Err(*error),
Value::Error { error, .. } => Err(*error),
_ => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record".to_string(),
wrong_type: data_as_value.get_type().to_string(),
dst_span: head_span,
src_span: data_as_value.expect_span(),
src_span: data_as_value.span(),
}),
}
}

View File

@ -67,8 +67,9 @@ where
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.expect_span(),
src_span: input.span(),
}),
span: head,
},
}
}