Replace &Span with Span since Span is Copy (#9770)

# Description
`Span` is `Copy`, so we probably should not be passing references of
`Span` around. This PR replaces all instances of `&Span` with `Span`,
copying spans where necessary.

# User-Facing Changes
This alters some public functions to take `Span` instead of `&Span` as
input. Namely, `EngineState::get_span_contents`,
`nu_protocol::extract_value`, a bunch of the math commands, and
`Gstat::gstat`.
This commit is contained in:
Ian Manske
2023-07-31 19:47:46 +00:00
committed by GitHub
parent 94bec72079
commit 583ef8674e
35 changed files with 356 additions and 365 deletions

View File

@ -267,9 +267,9 @@ impl Value {
if let Ok(b) = &$vals[$index].as_bool() {
config.$setting = *b;
} else {
invalid!(Some(*$span), "should be a bool");
invalid!(Some($span), "should be a bool");
// Reconstruct
$vals[$index] = Value::bool(config.$setting, *$span);
$vals[$index] = Value::bool(config.$setting, $span);
}
};
}
@ -278,9 +278,9 @@ impl Value {
if let Ok(b) = &$vals[$index].as_int() {
config.$setting = *b;
} else {
invalid!(Some(*$span), "should be an int");
invalid!(Some($span), "should be an int");
// Reconstruct
$vals[$index] = Value::int(config.$setting, *$span);
$vals[$index] = Value::int(config.$setting, $span);
}
};
}
@ -311,6 +311,7 @@ impl Value {
// the current use_ls_colors config setting is converted to a Value::Boolean and inserted in the
// record in place of the 2.
if let Value::Record { cols, vals, span } = self {
let span = *span;
// Because this whole algorithm removes while iterating, this must iterate in reverse.
for index in (0..cols.len()).rev() {
let value = &vals[index];
@ -319,6 +320,7 @@ impl Value {
// Grouped options
"ls" => {
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -350,10 +352,10 @@ impl Value {
vals[index] = Value::record(
vec!["use_ls_colors".into(), "clickable_links".into()],
vec![
Value::bool(config.use_ls_colors, *span),
Value::bool(config.show_clickable_links_in_ls, *span),
Value::bool(config.use_ls_colors, span),
Value::bool(config.show_clickable_links_in_ls, span),
],
*span,
span,
);
}
}
@ -364,7 +366,7 @@ impl Value {
let key2 = cols[index].as_str();
match key2 {
"abbreviations" => {
try_bool!(cols, vals, index, span, cd_with_abbreviations)
try_bool!(cols, vals, index, *span, cd_with_abbreviations)
}
x => {
invalid_key!(
@ -383,10 +385,10 @@ impl Value {
vals[index] = Value::record(
vec!["use_ls_colors".into(), "clickable_links".into()],
vec![
Value::bool(config.use_ls_colors, *span),
Value::bool(config.show_clickable_links_in_ls, *span),
Value::bool(config.use_ls_colors, span),
Value::bool(config.show_clickable_links_in_ls, span),
],
*span,
span,
);
}
}
@ -397,7 +399,7 @@ impl Value {
let key2 = cols[index].as_str();
match key2 {
"always_trash" => {
try_bool!(cols, vals, index, span, rm_always_trash)
try_bool!(cols, vals, index, *span, rm_always_trash)
}
x => {
invalid_key!(
@ -415,8 +417,8 @@ impl Value {
// Reconstruct
vals[index] = Value::record(
vec!["always_trash".into()],
vec![Value::bool(config.rm_always_trash, *span)],
*span,
vec![Value::bool(config.rm_always_trash, span)],
span,
);
}
}
@ -428,11 +430,12 @@ impl Value {
HistoryFileFormat::Sqlite => "sqlite",
HistoryFileFormat::PlainText => "plaintext",
},
*$span,
$span,
)
};
}
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -459,7 +462,7 @@ impl Value {
HistoryFileFormat::PlainText
}
_ => {
invalid!(Some(*span),
invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'sqlite' or 'plaintext'"
);
// Reconstruct
@ -468,7 +471,7 @@ impl Value {
}
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = reconstruct_history_file_format!(span);
}
@ -495,12 +498,12 @@ impl Value {
"isolation".into(),
],
vec![
Value::bool(config.sync_history_on_enter, *span),
Value::int(config.max_history_size, *span),
Value::bool(config.sync_history_on_enter, span),
Value::int(config.max_history_size, span),
reconstruct_history_file_format!(span),
Value::bool(config.history_isolation, *span),
Value::bool(config.history_isolation, span),
],
*span,
span,
);
}
}
@ -510,10 +513,10 @@ impl Value {
if let Some(block) = config.external_completer {
Value::Block {
val: block,
span: *$span,
span: $span,
}
} else {
Value::Nothing { span: *$span }
Value::Nothing { span: $span }
}
};
}
@ -522,15 +525,16 @@ impl Value {
Value::record(
vec!["max_results".into(), "completer".into(), "enable".into()],
vec![
Value::int(config.max_external_completion_results, *$span),
Value::int(config.max_external_completion_results, $span),
reconstruct_external_completer!($span),
Value::bool(config.enable_external_completion, *$span),
Value::bool(config.enable_external_completion, $span),
],
*$span,
$span,
)
};
}
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -550,22 +554,22 @@ impl Value {
config.completion_algorithm = val_str
}
_ => {
invalid!( Some(*span),
invalid!( Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'prefix' or 'fuzzy'"
);
// Reconstruct
vals[index] = Value::string(
config.completion_algorithm.clone(),
*span,
span,
);
}
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = Value::string(
config.completion_algorithm.clone(),
*span,
span,
);
}
}
@ -581,6 +585,7 @@ impl Value {
"external" => {
if let Value::Record { cols, vals, span } = &mut vals[index]
{
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key3 = cols[index].as_str();
@ -602,7 +607,7 @@ impl Value {
Value::Nothing { .. } => {}
_ => {
invalid!(
Some(*span),
Some(span),
"should be a block or null"
);
// Reconstruct
@ -634,7 +639,7 @@ impl Value {
}
}
} else {
invalid!(Some(*span), "should be a record");
invalid!(Some(span), "should be a record");
// Reconstruct
vals[index] = reconstruct_external!(span);
}
@ -662,13 +667,13 @@ impl Value {
"external".into(),
],
vec![
Value::bool(config.quick_completions, *span),
Value::bool(config.partial_completions, *span),
Value::string(config.completion_algorithm.clone(), *span),
Value::bool(config.case_sensitive_completions, *span),
Value::bool(config.quick_completions, span),
Value::bool(config.partial_completions, span),
Value::string(config.completion_algorithm.clone(), span),
Value::bool(config.case_sensitive_completions, span),
reconstruct_external!(span),
],
*span,
span,
);
}
}
@ -684,11 +689,12 @@ impl Value {
NuCursorShape::BlinkBlock => "blink_block",
NuCursorShape::BlinkUnderScore => "blink_underscore",
},
*$span,
$span,
)
};
}
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -722,7 +728,7 @@ impl Value {
NuCursorShape::BlinkUnderScore;
}
_ => {
invalid!(Some(*span),
invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'"
);
// Reconstruct
@ -733,7 +739,7 @@ impl Value {
}
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = reconstruct_cursor_shape!(
config.cursor_shape_vi_insert,
@ -770,7 +776,7 @@ impl Value {
NuCursorShape::BlinkUnderScore;
}
_ => {
invalid!(Some(*span),
invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'"
);
// Reconstruct
@ -781,7 +787,7 @@ impl Value {
}
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = reconstruct_cursor_shape!(
config.cursor_shape_vi_normal,
@ -817,7 +823,7 @@ impl Value {
NuCursorShape::BlinkUnderScore;
}
_ => {
invalid!(Some(*span),
invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'"
);
// Reconstruct
@ -828,7 +834,7 @@ impl Value {
}
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = reconstruct_cursor_shape!(
config.cursor_shape_emacs,
@ -857,7 +863,7 @@ impl Value {
reconstruct_cursor_shape!(config.cursor_shape_vi_normal, span),
reconstruct_cursor_shape!(config.cursor_shape_emacs, span),
],
*span,
span,
);
}
}
@ -870,7 +876,7 @@ impl Value {
TableIndexMode::Never => "never",
TableIndexMode::Auto => "auto",
},
*$span,
$span,
)
};
}
@ -883,29 +889,30 @@ impl Value {
"wrapping_try_keep_words".into(),
],
vec![
Value::string("wrapping", *$span),
Value::bool(*try_to_keep_words, *$span),
Value::string("wrapping", $span),
Value::bool(*try_to_keep_words, $span),
],
*$span,
$span,
),
TrimStrategy::Truncate { suffix } => Value::record(
vec!["methodology".into(), "truncating_suffix".into()],
match suffix {
Some(s) => vec![
Value::string("truncating", *$span),
Value::string(s.clone(), *$span),
Value::string("truncating", $span),
Value::string(s.clone(), $span),
],
None => vec![
Value::string("truncating", *$span),
Value::Nothing { span: *span },
Value::string("truncating", $span),
Value::Nothing { span: $span },
],
},
*$span,
$span,
),
}
};
}
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -914,9 +921,9 @@ impl Value {
if let Ok(v) = value.as_string() {
config.table_mode = v;
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
vals[index] =
Value::string(config.table_mode.clone(), *span);
Value::string(config.table_mode.clone(), span);
}
}
"index_mode" => {
@ -933,14 +940,14 @@ impl Value {
config.table_index_mode = TableIndexMode::Auto
}
_ => {
invalid!( Some(*span),
invalid!( Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'never', 'always' or 'auto'"
);
vals[index] = reconstruct_index_mode!(span);
}
}
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
vals[index] = reconstruct_index_mode!(span);
}
}
@ -979,17 +986,18 @@ impl Value {
"show_empty".into(),
],
vec![
Value::string(config.table_mode.clone(), *span),
Value::string(config.table_mode.clone(), span),
reconstruct_index_mode!(span),
reconstruct_trim_strategy!(span),
Value::bool(config.table_show_empty, *span),
Value::bool(config.table_show_empty, span),
],
*span,
span,
)
}
}
"filesize" => {
if let Value::Record { cols, vals, span } = &mut vals[index] {
let span = *span;
for index in (0..cols.len()).rev() {
let value = &vals[index];
let key2 = cols[index].as_str();
@ -1001,12 +1009,10 @@ impl Value {
if let Ok(v) = value.as_string() {
config.filesize_format = v.to_lowercase();
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = Value::string(
config.filesize_format.clone(),
*span,
);
vals[index] =
Value::string(config.filesize_format.clone(), span);
}
}
x => {
@ -1026,10 +1032,10 @@ impl Value {
vals[index] = Value::record(
vec!["metric".into(), "format".into()],
vec![
Value::bool(config.filesize_metric, *span),
Value::string(config.filesize_format.clone(), *span),
Value::bool(config.filesize_metric, span),
Value::string(config.filesize_format.clone(), span),
],
*span,
span,
);
}
}
@ -1039,7 +1045,7 @@ impl Value {
} else {
invalid!(vals[index].span().ok(), "should be a record");
// Reconstruct
vals[index] = Value::record_from_hashmap(&config.explore, *span);
vals[index] = Value::record_from_hashmap(&config.explore, span);
}
}
// Misc. options
@ -1049,7 +1055,7 @@ impl Value {
} else {
invalid!(vals[index].span().ok(), "should be a record");
// Reconstruct
vals[index] = Value::record_from_hashmap(&config.color_config, *span);
vals[index] = Value::record_from_hashmap(&config.color_config, span);
}
}
"use_grid_icons" => {
@ -1068,7 +1074,7 @@ impl Value {
},
};
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = Value::String {
val: match config.footer_mode {
@ -1077,7 +1083,7 @@ impl Value {
FooterMode::Always => "always".into(),
FooterMode::RowCount(number) => number.to_string(),
},
span: *span,
span,
};
}
}
@ -1091,9 +1097,9 @@ impl Value {
if let Ok(v) = value.as_string() {
config.edit_mode = v.to_lowercase();
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
// Reconstruct
vals[index] = Value::string(config.edit_mode.clone(), *span);
vals[index] = Value::string(config.edit_mode.clone(), span);
}
}
"shell_integration" => {
@ -1103,7 +1109,7 @@ impl Value {
if let Ok(v) = value.as_string() {
config.buffer_editor = v.to_lowercase();
} else {
invalid!(Some(*span), "should be a string");
invalid!(Some(span), "should be a string");
}
}
"show_banner" => {
@ -1119,7 +1125,7 @@ impl Value {
"menus" => match create_menus(value) {
Ok(map) => config.menus = map,
Err(e) => {
invalid!(Some(*span), "should be a valid list of menus");
invalid!(Some(span), "should be a valid list of menus");
errors.push(e);
// Reconstruct
vals[index] = Value::List {
@ -1152,12 +1158,12 @@ impl Value {
menu_type.clone(),
source.clone(),
],
span: *span,
span,
}
},
)
.collect(),
span: *span,
span,
}
}
},
@ -1165,7 +1171,7 @@ impl Value {
"keybindings" => match create_keybindings(value) {
Ok(keybindings) => config.keybindings = keybindings,
Err(e) => {
invalid!(Some(*span), "should be a valid keybindings list");
invalid!(Some(span), "should be a valid keybindings list");
errors.push(e);
// Reconstruct
vals[index] = Value::List {
@ -1192,12 +1198,12 @@ impl Value {
mode.clone(),
event.clone(),
],
span: *span,
span,
}
},
)
.collect(),
span: *span,
span,
}
}
},
@ -1205,7 +1211,7 @@ impl Value {
"hooks" => match create_hooks(value) {
Ok(hooks) => config.hooks = hooks,
Err(e) => {
invalid!(Some(*span), "should be a valid hooks list");
invalid!(Some(span), "should be a valid hooks list");
errors.push(e);
// Reconstruct
let mut hook_cols = vec![];
@ -1229,7 +1235,7 @@ impl Value {
vals.push(Value::Record {
cols: hook_cols,
vals: hook_vals,
span: *span,
span,
});
}
},
@ -1270,10 +1276,10 @@ impl Value {
vals[index] = Value::record(
vec!["metric".into(), "format".into()],
vec![
Value::bool(config.filesize_metric, *span),
Value::string(config.filesize_format.clone(), *span),
Value::bool(config.filesize_metric, span),
Value::string(config.filesize_format.clone(), span),
],
*span,
span,
);
}
}
@ -1458,6 +1464,7 @@ fn create_hooks(value: &Value) -> Result<Hooks, ShellError> {
fn create_keybindings(value: &Value) -> Result<Vec<ParsedKeybinding>, ShellError> {
match value {
Value::Record { cols, vals, span } => {
let span = *span;
// Finding the modifier value in the record
let modifier = extract_value("modifier", cols, vals, span)?.clone();
let keycode = extract_value("keycode", cols, vals, span)?.clone();
@ -1495,6 +1502,7 @@ fn create_keybindings(value: &Value) -> Result<Vec<ParsedKeybinding>, ShellError
pub fn create_menus(value: &Value) -> Result<Vec<ParsedMenu>, ShellError> {
match value {
Value::Record { cols, vals, span } => {
let span = *span;
// Finding the modifier value in the record
let name = extract_value("name", cols, vals, span)?.clone();
let marker = extract_value("marker", cols, vals, span)?.clone();
@ -1506,7 +1514,7 @@ pub fn create_menus(value: &Value) -> Result<Vec<ParsedMenu>, ShellError> {
// Source is an optional value
let source = match extract_value("source", cols, vals, span) {
Ok(source) => source.clone(),
Err(_) => Value::Nothing { span: *span },
Err(_) => Value::Nothing { span },
};
let menu = ParsedMenu {
@ -1538,10 +1546,10 @@ pub fn extract_value<'record>(
name: &str,
cols: &'record [String],
vals: &'record [Value],
span: &Span,
span: Span,
) -> Result<&'record Value, ShellError> {
cols.iter()
.position(|col| col.as_str() == name)
.and_then(|index| vals.get(index))
.ok_or_else(|| ShellError::MissingConfigValue(name.to_string(), *span))
.ok_or_else(|| ShellError::MissingConfigValue(name.to_string(), span))
}

View File

@ -724,7 +724,7 @@ impl EngineState {
output
}
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
pub fn get_span_contents(&self, span: Span) -> &[u8] {
for (contents, start, finish) in &self.file_contents {
if span.start >= *start && span.end <= *finish {
return &contents[(span.start - start)..(span.end - start)];
@ -902,7 +902,7 @@ impl EngineState {
pub fn build_usage(&self, spans: &[Span]) -> (String, String) {
let comment_lines: Vec<&[u8]> = spans
.iter()
.map(|span| self.get_span_contents(span))
.map(|span| self.get_span_contents(*span))
.collect();
build_usage(&comment_lines)
}
@ -1370,7 +1370,7 @@ impl<'a> StateWorkingSet<'a> {
}
}
} else {
return self.permanent_state.get_span_contents(&span);
return self.permanent_state.get_span_contents(span);
}
panic!("internal error: missing span contents in file cache")

View File

@ -480,25 +480,25 @@ impl Value {
/// Get the span for the current value
pub fn span(&self) -> Result<Span, ShellError> {
match self {
Value::Bool { span, .. }
| Value::Int { span, .. }
| Value::Float { span, .. }
| Value::Filesize { span, .. }
| Value::Duration { span, .. }
| Value::Date { span, .. }
| Value::Range { span, .. }
| Value::String { span, .. }
| Value::Record { span, .. }
| Value::List { span, .. }
| Value::Block { span, .. }
| Value::Closure { span, .. }
| Value::Nothing { span, .. }
| Value::Binary { span, .. }
| Value::CellPath { span, .. }
| Value::CustomValue { span, .. }
| Value::LazyRecord { span, .. }
| Value::MatchPattern { span, .. } => Ok(*span),
Value::Error { error } => Err(*error.clone()),
Value::Bool { span, .. } => Ok(*span),
Value::Int { span, .. } => Ok(*span),
Value::Float { span, .. } => Ok(*span),
Value::Filesize { span, .. } => Ok(*span),
Value::Duration { span, .. } => Ok(*span),
Value::Date { span, .. } => Ok(*span),
Value::Range { span, .. } => Ok(*span),
Value::String { span, .. } => Ok(*span),
Value::Record { span, .. } => Ok(*span),
Value::List { span, .. } => Ok(*span),
Value::Block { span, .. } => Ok(*span),
Value::Closure { span, .. } => Ok(*span),
Value::Nothing { span, .. } => Ok(*span),
Value::Binary { span, .. } => Ok(*span),
Value::CellPath { span, .. } => Ok(*span),
Value::CustomValue { span, .. } => Ok(*span),
Value::LazyRecord { span, .. } => Ok(*span),
Value::MatchPattern { span, .. } => Ok(*span),
}
}
@ -512,25 +512,25 @@ impl Value {
/// Update the value with a new span
pub fn with_span(mut self, new_span: Span) -> Value {
match &mut self {
Value::Bool { span, .. } => *span = new_span,
Value::Int { span, .. } => *span = new_span,
Value::Float { span, .. } => *span = new_span,
Value::Filesize { span, .. } => *span = new_span,
Value::Duration { span, .. } => *span = new_span,
Value::Date { span, .. } => *span = new_span,
Value::Range { span, .. } => *span = new_span,
Value::String { span, .. } => *span = new_span,
Value::Record { span, .. } => *span = new_span,
Value::LazyRecord { span, .. } => *span = new_span,
Value::List { span, .. } => *span = new_span,
Value::Closure { span, .. } => *span = new_span,
Value::Block { span, .. } => *span = new_span,
Value::Nothing { span, .. } => *span = new_span,
Value::Error { .. } => {}
Value::Binary { span, .. } => *span = new_span,
Value::CellPath { span, .. } => *span = new_span,
Value::CustomValue { span, .. } => *span = new_span,
Value::MatchPattern { span, .. } => *span = new_span,
Value::Bool { span, .. }
| Value::Int { span, .. }
| Value::Float { span, .. }
| Value::Filesize { span, .. }
| Value::Duration { span, .. }
| Value::Date { span, .. }
| Value::Range { span, .. }
| Value::String { span, .. }
| Value::Record { span, .. }
| Value::LazyRecord { span, .. }
| Value::List { span, .. }
| Value::Closure { span, .. }
| Value::Block { span, .. }
| Value::Nothing { span, .. }
| Value::Binary { span, .. }
| Value::CellPath { span, .. }
| Value::CustomValue { span, .. }
| Value::MatchPattern { span, .. } => *span = new_span,
Value::Error { .. } => (),
}
self