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

@ -219,6 +219,7 @@ fn detect_columns(
let err = processing_error("could not find range index", name_span);
return Value::Error {
error: Box::new(err),
span: name_span,
};
}
}

View File

@ -99,12 +99,12 @@ fn action(
Some(config_character_set.span),
None,
Vec::new(),
))}
)), span:config_character_set.span}
};
match input {
// Propagate existing errors.
Value::Error { .. } => input.clone(),
Value::Binary { val, .. } => match base64_config.action_type {
Value::Binary { val, span } => match base64_config.action_type {
ActionType::Encode => {
let mut enc_vec = Vec::new();
enc_vec.resize(val.len() * 4 / 3 + 4, 0);
@ -115,10 +115,11 @@ fn action(
error: Box::new(ShellError::GenericError(
"Error encoding data".into(),
err.to_string(),
Some(Span::unknown()),
Some(*span),
None,
Vec::new(),
)),
span: *span,
}
}
};
@ -131,8 +132,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 {
@ -167,6 +169,7 @@ fn action(
Some("consider using the `--binary` flag".to_owned()),
Vec::new(),
)),
span: *value_span,
},
}
}
@ -182,6 +185,7 @@ fn action(
None,
Vec::new(),
)),
span: command_span,
},
}
}
@ -190,8 +194,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

@ -77,12 +77,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
PipelineData::Value(v, ..) => match v {
Value::Binary { val: bytes, .. } => super::encoding::decode(head, encoding, &bytes)
.map(|val| val.into_pipeline_data()),
Value::Error { error } => Err(*error),
Value::Error { error, .. } => Err(*error),
_ => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "binary".into(),
wrong_type: v.get_type().to_string(),
dst_span: head,
src_span: v.expect_span(),
src_span: v.span(),
}),
},
// This should be more precise, but due to difficulties in getting spans

View File

@ -100,12 +100,12 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
super::encoding::encode(head, encoding, &s, span, ignore_errors)
.map(|val| val.into_pipeline_data())
}
Value::Error { error } => Err(*error),
Value::Error { error, .. } => Err(*error),
_ => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: v.get_type().to_string(),
dst_span: head,
src_span: v.expect_span(),
src_span: v.span(),
}),
},
// This should be more precise, but due to difficulties in getting spans

View File

@ -135,6 +135,7 @@ where
err_message: "invalid format".to_string(),
span,
}),
span,
},
}
}
@ -155,6 +156,7 @@ fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span:
value.debug_value(),
head_span,
)),
span: head_span,
},
}
}
@ -180,6 +182,7 @@ fn format_helper_rfc2822(value: Value, span: Span) -> Value {
}
_ => Value::Error {
error: Box::new(ShellError::DatetimeParseError(value.debug_value(), span)),
span,
},
}
}

View File

@ -137,7 +137,10 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value {
}
}
}
Err(e) => Value::Error { error: Box::new(e) },
Err(e) => Value::Error {
error: Box::new(e),
span: *inner_span,
},
}
}
Value::Error { .. } => val.clone(),
@ -146,8 +149,9 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value {
exp_input_type: "filesize".into(),
wrong_type: val.get_type().to_string(),
dst_span: span,
src_span: val.expect_span(),
src_span: val.span(),
}),
span,
},
}
}

View File

@ -115,8 +115,9 @@ fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value {
exp_input_type: "filesize".into(),
wrong_type: val.get_type().to_string(),
dst_span: span,
src_span: val.expect_span(),
src_span: val.span(),
}),
span,
},
}
}

View File

@ -186,7 +186,7 @@ fn operate(
}
};
let v_span = v.span()?;
let v_span = v.span();
let record = columns
.iter()
.zip(captures.iter().skip(1))
@ -203,7 +203,7 @@ fn operate(
return Err(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: head,
src_span: v.span()?,
src_span: v.span(),
})
}
}
@ -346,14 +346,15 @@ impl Iterator for ParseStreamer {
error: Box::new(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: self.span,
src_span: v.span().unwrap_or(self.span),
src_span: v.span(),
}),
span: v.span(),
})
};
let parsed = stream_helper(
self.regex.clone(),
v.span().unwrap_or(self.span),
v.span(),
s,
self.columns.clone(),
&mut self.excess,
@ -403,6 +404,7 @@ impl Iterator for ParseStreamerExternal {
Some(Err(err)) => {
return Some(Value::Error {
error: Box::new(err),
span: self.span,
})
}
_ => return None,
@ -415,6 +417,7 @@ impl Iterator for ParseStreamerExternal {
dst_span: self.span,
src_span: self.span,
}),
span: self.span,
})
};
@ -445,10 +448,11 @@ fn stream_helper(
error: Box::new(ShellError::GenericError(
"Error with regular expression captures".into(),
e.to_string(),
None,
None,
Some(span),
Some(e.to_string()),
Vec::new(),
)),
span,
})
}
};

View File

@ -122,9 +122,14 @@ fn size(
input.map(
move |v| {
// First, obtain the span. If this fails, propagate the error that results.
let value_span = match v.span() {
Err(v) => return Value::Error { error: Box::new(v) },
Ok(v) => v,
let value_span = match &v {
Value::Error { error, span } => {
return Value::Error {
error: error.clone(),
span: *span,
}
}
v => v.span(),
};
// Now, check if it's a string.
match v.as_string() {
@ -135,6 +140,7 @@ fn size(
dst_span: span,
src_span: value_span,
}),
span,
},
}
},

View File

@ -118,8 +118,13 @@ fn split_chars(
}
fn split_chars_helper(v: &Value, name: Span, graphemes: bool) -> Value {
match v.span() {
Ok(v_span) => {
match v {
Value::Error { error, span } => Value::Error {
error: error.clone(),
span: *span,
},
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
Value::List {
vals: if graphemes {
@ -144,12 +149,10 @@ fn split_chars_helper(v: &Value, name: Span, graphemes: bool) -> Value {
dst_span: name,
src_span: v_span,
}),
span: name,
}
}
}
Err(error) => Value::Error {
error: Box::new(error),
},
}
}

View File

@ -201,17 +201,24 @@ fn split_column_helper(
}
vec![Value::record(record, head)]
} else {
match v.span() {
Ok(span) => vec![Value::Error {
error: Box::new(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: head,
src_span: span,
}),
}],
Err(error) => vec![Value::Error {
error: Box::new(error),
}],
match v {
Value::Error { error, .. } => {
vec![Value::Error {
error: Box::new(*error.clone()),
span: head,
}]
}
v => {
let span = v.span();
vec![Value::Error {
error: Box::new(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: head,
src_span: span,
}),
span,
}]
}
}
}
}

View File

@ -169,8 +169,8 @@ impl Matcher {
"Error with regular expression".into(),
err.to_string(),
match lhs {
Value::Error { error: _ } => None,
_ => Some(lhs.expect_span()),
Value::Error { .. } => None,
_ => Some(lhs.span()),
},
None,
Vec::new(),

View File

@ -146,8 +146,16 @@ fn split_row(
}
fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Span) -> Vec<Value> {
match v.span() {
Ok(v_span) => {
match v {
Value::Error { error, span } => {
vec![Value::Error {
error: Box::new(*error.clone()),
span: *span,
}]
}
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
match max_split {
Some(max_split) => regex
@ -166,12 +174,10 @@ fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Sp
dst_span: name,
src_span: v_span,
}),
span: name,
}]
}
}
Err(error) => vec![Value::Error {
error: Box::new(error),
}],
}
}

View File

@ -153,8 +153,13 @@ fn split_words_helper(v: &Value, word_length: Option<usize>, span: Span, graphem
// Let's go with the unicode one in hopes that it works on more than just ascii characters
let regex_replace = Regex::new(r"[^\p{L}\']").expect("regular expression error");
match v.span() {
Ok(v_span) => {
match v {
Value::Error { error, span } => Value::Error {
error: Box::new(*error.clone()),
span: *span,
},
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
// let splits = s.unicode_words();
// let words = trim_to_words(s);
@ -195,12 +200,10 @@ fn split_words_helper(v: &Value, word_length: Option<usize>, span: Span, graphem
dst_span: span,
src_span: v_span,
}),
span,
}
}
}
Err(error) => Value::Error {
error: Box::new(error),
},
}
}

View File

@ -100,6 +100,7 @@ fn operate(
if let Err(error) = r {
return Value::Error {
error: Box::new(error),
span: head,
};
}
}
@ -122,8 +123,9 @@ fn action(input: &Value, head: Span) -> Value {
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,
},
}
}

View File

@ -111,6 +111,7 @@ fn operate(
if let Err(error) = r {
return Value::Error {
error: Box::new(error),
span: head,
};
}
}
@ -133,8 +134,9 @@ fn action(input: &Value, head: Span) -> Value {
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,
},
}
}

View File

@ -61,8 +61,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,
},
}
}

View File

@ -80,6 +80,7 @@ fn operate(
if let Err(error) = r {
return Value::Error {
error: Box::new(error),
span: head,
};
}
}
@ -102,8 +103,9 @@ fn action(input: &Value, head: Span) -> Value {
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,
},
}
}

View File

@ -187,8 +187,9 @@ fn action(
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,
},
}
}

View File

@ -116,8 +116,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
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,
},
}
}

View File

@ -110,8 +110,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
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,
},
}
}

View File

@ -197,14 +197,11 @@ impl Command for SubCommand {
let is_path = call.has_flag("path");
input.map(
move |v| {
let value_span = match v.span() {
Err(v) => return Value::Error { error: Box::new(v) },
Ok(v) => v,
};
let value_span = v.span();
match v.as_string() {
Ok(s) => {
let contents = if is_path { s.replace('\\', "\\\\") } else { s };
str_expand(&contents, span, v.expect_span())
str_expand(&contents, span, v.span())
}
Err(_) => Value::Error {
error: Box::new(ShellError::PipelineMismatch {
@ -212,6 +209,7 @@ impl Command for SubCommand {
dst_span: span,
src_span: value_span,
}),
span,
},
}
},
@ -236,7 +234,9 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
},
Err(e) => match e {
bracoxide::ExpansionError::NumConversionFailed(s) => Value::Error { error:
Box::new(ShellError::GenericError("Number Conversion Failed".to_owned(), format!("Number conversion failed at {s}."), Some(value_span), Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".to_owned()), vec![])) },
Box::new(ShellError::GenericError("Number Conversion Failed".to_owned(), format!("Number conversion failed at {s}."), Some(value_span), Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".to_owned()), vec![])),
span,
},
},
}
},
@ -255,12 +255,15 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
ParsingError::ExtraOBra(s) => ShellError::GenericError("Extra Opening Brace".to_owned(), format!("Used extra opening brace at {s}."), Some(value_span), Some("To escape opening brace use backslash, e.g. `\\{`".to_owned()), vec![]),
ParsingError::NothingInBraces(s) => ShellError::GenericError("Nothing In Braces".to_owned(), format!("Nothing found inside braces at {s}."), Some(value_span), Some("Please provide valid content within the braces. Additionally, you can safely remove it, not needed.".to_owned()), vec![]),
}
) }
),
span,
}
}
},
Err(e) => match e {
TokenizationError::EmptyContent => Value::Error {
error: Box::new(ShellError::PipelineEmpty { dst_span: value_span }),
span: value_span,
},
TokenizationError::FormatNotSupported => Value::Error {
error: Box::new(
@ -270,10 +273,12 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
Some(value_span),
Some("In brace expansion syntax, it is important to have an equal number of opening (`{`) and closing (`}`) braces. Please ensure that you provide a balanced pair of braces in your brace expansion pattern.".to_owned()),
vec![]
))
)),
span: value_span,
},
TokenizationError::NoBraces => Value::Error {
error: Box::new(ShellError::GenericError("No Braces".to_owned(), "At least one `{}` brace expansion expected.".to_owned(), Some(value_span), Some("Please, examine the examples.".to_owned()), vec![]))
error: Box::new(ShellError::GenericError("No Braces".to_owned(), "At least one `{}` brace expansion expected.".to_owned(), Some(value_span), Some("Please, examine the examples.".to_owned()), vec![])),
span: value_span,
}
},
}

View File

@ -163,6 +163,7 @@ fn action(
let err = processing_error("could not find `index-of`", head);
return Value::Error {
error: Box::new(err),
span: head,
};
}
}
@ -202,8 +203,9 @@ fn action(
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,
},
}
}

View File

@ -55,7 +55,7 @@ impl Command for StrJoin {
for value in input {
match value {
Value::Error { error } => {
Value::Error { error, .. } => {
return Err(*error);
}
value => {

View File

@ -117,8 +117,9 @@ fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
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,
},
}
}

View File

@ -276,6 +276,7 @@ fn action(
val_span: find.span,
call_span: head,
}),
span: find.span,
},
}
}
@ -286,8 +287,9 @@ fn action(
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,
},
}
}

View File

@ -89,8 +89,9 @@ fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
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,
},
}
}

View File

@ -123,8 +123,9 @@ fn action(
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,
},
}
}

View File

@ -149,6 +149,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
err_message: "End must be greater than or equal to Start".to_string(),
span: head,
}),
span: head,
},
Ordering::Less => Value::String {
val: {
@ -195,8 +196,9 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
format!("input type: {:?}", other.get_type()),
head,
// This line requires the Value::Error match above.
other.expect_span(),
other.span(),
)),
span: head,
},
}
}

View File

@ -208,8 +208,9 @@ fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
format!("input type: {:?}", other.get_type()),
head,
// This line requires the Value::Error match above.
other.expect_span(),
other.span(),
)),
span: head,
}
}
},