fix do closure with both required, options, and rest args (#13002)

# Description
Fixes: #12985

`val_iter` has already handle required positional and optional
positional arguments, it not skip them again while handling rest
arguments.

# User-Facing Changes
Makes `do {|a, ...b| echo $a ...$b} 1 2 3 4` output the following again:
```nushell
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
│ 3 │ 4 │
╰───┴───╯
```

# Tests + Formatting
Added some test cases
This commit is contained in:
Wind 2024-05-30 21:29:46 +08:00 committed by GitHub
parent 0e1553026e
commit 40772fea15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -298,9 +298,7 @@ fn bind_args_to(
if let Some(rest_positional) = &signature.rest_positional {
let mut rest_items = vec![];
for result in
val_iter.skip(signature.required_positional.len() + signature.optional_positional.len())
{
for result in val_iter {
rest_items.push(result);
}

View File

@ -539,6 +539,15 @@ fn dynamic_closure_optional_arg() {
fn dynamic_closure_rest_args() {
let actual = nu!(r#"let closure = {|...args| $args | str join ""}; do $closure 1 2 3"#);
assert_eq!(actual.out, "123");
let actual = nu!(
r#"let closure = {|required, ...args| $"($required), ($args | str join "")"}; do $closure 1 2 3"#
);
assert_eq!(actual.out, "1, 23");
let actual = nu!(
r#"let closure = {|required, optional?, ...args| $"($required), ($optional), ($args | str join "")"}; do $closure 1 2 3"#
);
assert_eq!(actual.out, "1, 2, 3");
}
#[cfg(feature = "which-support")]