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

@ -18,6 +18,7 @@ const ENV_PATH_NAME: &str = "PATH";
const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS";
#[allow(dead_code)]
enum ConversionResult {
Ok(Value),
ConversionError(ShellError), // Failure during the conversion itself
@ -116,19 +117,19 @@ pub fn env_to_string(
Ok(p) => Ok(p.to_string_lossy().to_string()),
Err(_) => Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
span: value.span(),
}),
}
}
_ => Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
span: value.span(),
}),
}
} else {
Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
span: value.span(),
})
}
}
@ -167,7 +168,7 @@ pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result<Stri
Err(ShellError::GenericError(
"Invalid current directory".to_string(),
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
Some(pwd.span()?),
Some(pwd.span()),
None,
Vec::new()
))
@ -260,7 +261,7 @@ pub fn find_in_dirs_env(
return Err(ShellError::GenericError(
"Invalid current directory".to_string(),
format!("The 'FILE_PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
Some(pwd.span()?),
Some(pwd.span()),
None,
Vec::new()
));
@ -321,18 +322,8 @@ fn get_converted_value(
direction: &str,
) -> ConversionResult {
if let Some(env_conversions) = stack.get_env_var(engine_state, ENV_CONVERSIONS) {
let env_span = match env_conversions.span() {
Ok(span) => span,
Err(e) => {
return ConversionResult::GeneralError(e);
}
};
let val_span = match orig_val.span() {
Ok(span) => span,
Err(e) => {
return ConversionResult::GeneralError(e);
}
};
let env_span = env_conversions.span();
let val_span = orig_val.span();
let path_members = &[
PathMember::String {
@ -421,13 +412,7 @@ fn ensure_path(scope: &mut HashMap<String, Value>, env_path_name: &str) -> Optio
}
Some(val) => {
// All other values are errors
let span = match val.span() {
Ok(sp) => sp,
Err(e) => {
error = error.or(Some(e));
Span::unknown() // FIXME: any better span to use here?
}
};
let span = val.span();
error = error.or_else(|| {
Some(ShellError::GenericError(

View File

@ -93,7 +93,7 @@ pub fn eval_call(
}
let span = if let Some(rest_item) = rest_items.first() {
rest_item.span()?
rest_item.span()
} else {
call.head
};
@ -284,7 +284,7 @@ pub fn eval_expression(
span: expr.span,
}),
Expr::ValueWithUnit(e, unit) => match eval_expression(engine_state, stack, e)? {
Value::Int { val, .. } => Ok(compute(val, unit.item, unit.span)),
Value::Int { val, .. } => compute(val, unit.item, unit.span),
x => Err(ShellError::CantConvert {
to_type: "unit value".into(),
from_type: x.get_type().to_string(),
@ -1155,6 +1155,7 @@ pub fn eval_block(
input = PipelineData::Value(
Value::Error {
error: Box::new(error),
span: Span::unknown(), // FIXME: where does this span come from?
},
None,
)
@ -1247,7 +1248,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
path.push("nushell");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get config directory".into()))
Value::error(
ShellError::IOError("Could not get config directory".into()),
span,
)
},
);
@ -1261,7 +1265,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
path.push("config.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get config directory".into()))
Value::error(
ShellError::IOError("Could not get config directory".into()),
span,
)
},
);
@ -1275,9 +1282,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
path.push("env.nu");
Value::string(path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not find environment path".into(),
))
Value::error(
ShellError::IOError("Could not find environment path".into()),
span,
)
},
);
@ -1296,7 +1304,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
let canon_hist_path = canonicalize_path(engine_state, &path);
Value::string(canon_hist_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not find history path".into()))
Value::error(
ShellError::IOError("Could not find history path".into()),
span,
)
},
);
@ -1308,9 +1319,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
let canon_login_path = canonicalize_path(engine_state, &path);
Value::string(canon_login_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not find login shell path".into(),
))
Value::error(
ShellError::IOError("Could not find login shell path".into()),
span,
)
},
);
@ -1322,9 +1334,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
let canon_plugin_path = canonicalize_path(engine_state, path);
Value::string(canon_plugin_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not get plugin signature location".into(),
))
Value::error(
ShellError::IOError("Could not get plugin signature location".into()),
span,
)
},
);
}
@ -1335,7 +1348,7 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
let canon_home_path = canonicalize_path(engine_state, &path);
Value::string(canon_home_path.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError("Could not get home path".into()))
Value::error(ShellError::IOError("Could not get home path".into()), span)
},
);
@ -1380,9 +1393,10 @@ pub fn eval_nu_variable(engine_state: &EngineState, span: Span) -> Result<Value,
if let Ok(current_exe) = std::env::current_exe() {
Value::string(current_exe.to_string_lossy(), span)
} else {
Value::error(ShellError::IOError(
"Could not get current executable path".to_string(),
))
Value::error(
ShellError::IOError("Could not get current executable path".to_string()),
span,
)
},
);
@ -1416,7 +1430,7 @@ pub fn eval_variable(
}
}
fn compute(size: i64, unit: Unit, span: Span) -> Value {
fn compute(size: i64, unit: Unit, span: Span) -> Result<Value, ShellError> {
unit.to_value(size, span)
}
@ -1461,6 +1475,7 @@ fn collect_profiling_metadata(
Ok((PipelineData::Empty, ..)) => Value::Nothing { span: element_span },
Err(err) => Value::Error {
error: Box::new(err.clone()),
span: element_span,
},
};