GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Recommend the spread operator when a list is passed to a rest parameter by r00tkrnl · Pull Request #19679 · nushell/nushell Conversationpassing a list to now it adds the parse time path covers variables with a known type. the runtime path covers untyped params like tests in tests/commands/rm.rs cover both closes #13755
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsA list given to `rm` failed with a bare type mismatch or a bare conversion error. Both now say how to spread the list, naming the argument. Refs nushell#13755 🤖 Generated with Codebuff Co-Authored-By: Codebuff <[email protected]>
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
fdncred
reviewed
|
| if let ShellError::CantConvert { help, .. } = &mut err | ||
| && help.is_none() | ||
| { | ||
| let arg = String::from_utf8_lossy(engine_state.get_span_contents(value.span())); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
$env lists
On 9c4e120 the runtime hint takes its text from value.span(). That span points at wherever the list was made, not at the argument the user typed. When the list comes from a subexpression or the environment, the suggestion isn't something you can type:
rm (ls | get name) # help: to pass each item of the list as a separate argument, spread it like so: ...get rm (open list.json) # help: ... spread it like so: ...open rm (glob *.txt | each {|f| $f | path basename }) # help: ... spread it like so: ...each rm $env.PATH # help: ... spread it like so: ..."/usr/bin:/bin" rm $nu.user-autoload-dirs # help: ... spread it like so: ...$list
Main shows no help on any of these. The $env.PATH one copies the variable's value out of the host environment, so with a real PATH the help runs to a dozen lines. $list names a variable that doesn't exist.
The call already has the right span. Argument::Positional { span, .. } is documented as "the span encompassing the argument's usage within the call, distinct from the span of the actual value". I tried building the hint from it, walking self.arguments(stack) in rest instead of rest_iter_flattened, and got ...(ls | get name), ...(open list.json), ...$env.PATH and ...$nu.user-autoload-dirs. Both new tests still pass with that change.
The tests only use variables, where the value span and the argument span happen to match. A case like rm (ls | get name) would have caught this.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| /// Lists are not spread automatically, so a list given to a rest parameter fails to type | ||
| /// check. Build a hint that names the argument to spread. | ||
| fn list_needs_spread(working_set: &StateWorkingSet, arg: &Expression) -> Option<String> { | ||
| if !matches!(&arg.ty, Type::List(_)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The hint fires for any list, even when spreading it would fail too:
let l = [{a: 1}]; rm $l # help: to pass each item of the list as a separate argument, spread it like so: ...$l let l = [{a: 1}]; rm ...$l # Error: nu::shell::cant_convert # can't convert record<a: int> to string
The runtime path does the same. def f [x] { rm $x }; f (ls) suggests ...$x, and rm ...$x then fails on the first record.
list_needs_spread only checks for Type::List(_), and explain_list_argument only checks for Value::List. In the parser, pass expected in and return None unless type_compatible(&expected, item) holds for the list's item type. At runtime the check only has to run on the error path: add the help only when the list's items convert with T::from_value. The issue asked for the hint "when a list containing the correct type is given".
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| stack: &mut Stack, | ||
| starting_pos: usize, | ||
| ) -> Result<Vec<T>, ShellError> { | ||
| self.rest_iter_flattened(stack, starting_pos)? |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
rest runs the hint on every value from rest_iter_flattened, including the items of a list that was already spread:
let x = [[a.txt] [b.txt]]; rm ...$x # help: to pass each item of the list as a separate argument, spread it like so: ...[a.txt]
The user already wrote .... Another spread doesn't fix a nested list, flatten does. Only Argument::Positional values should get the hint. With rest walking self.arguments(stack) (the change from the span thread), skipping Argument::Spread items is one match arm, and I saw no help on this case with it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .into_iter() | ||
| .map(T::from_value) | ||
| .map(|value| { | ||
| T::from_value(value.clone()) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
rest_iter_flattened already clones each value once, and T::from_value(value.clone()) now makes a second copy of every rest value of every built-in, only so the error path can look at it afterwards. For strings and binaries that is a full extra copy on every call, including rm ...$paths, run-external, exec and bytes build.
The argument walk from the span thread fixes this too: with rest reading self.arguments(stack), each value gets one val.clone() into T::from_value, and the hint uses the borrowed argument.
From reading the code; I didn't measure it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| let text = String::from_utf8_lossy(working_set.get_span_contents(arg.span)); | ||
| let text = text.trim(); | ||
| let text = if text.is_empty() { "$list" } else { text }; | ||
|
|
||
| Some(format!( |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
list_needs_spread here and explain_list_argument in crates/nu-engine/src/call_ext.rs repeat the same message, the trim and the $list fallback, so the two can drift apart. AGENTS.md: "keep code duplication low without adding a pile of new helpers: reuse the helpers that already exist." One function in nu-protocol, next to ShellError::RECORD_SPREAD_DUPLICATE_HELP, that takes the argument text and returns the help would cover both call sites.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Change error message to recommend using the spread operator when passing a list to rm
There was an error while loading. Please reload this page.