Skip to content

Commit

Permalink
Restrict closure expression to be something like {|| ...} (#8290)
Browse files Browse the repository at this point in the history
# Description

As title, closes: #7921 closes: #8273

# User-Facing Changes

when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe

  × Missing || inside closure
   ╭─[entry #2:1:1]
 1 │ let x = {ss ss}
   ·         ───┬───
   ·            ╰── Parsing as a closure, but || is missing
   ╰────
  help: Try add || to the beginning of closure
```

`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe

  × Missing || inside closure
   ╭─[entry #4:1:1]
 1 │ {major:2, minor:1, patch:4} | values | each { into string }
   ·                                             ───────┬───────
   ·                                                    ╰── Parsing as a closure, but || is missing
   ╰────
  help: Try add || to the beginning of closure
```

`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```

Just realize that It's a big breaking change, we need to update config
and scripts...

# 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# 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.
  • Loading branch information
WindSoilder committed Mar 17, 2023
1 parent 400a9d3 commit a8eef9a
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 71 deletions.
6 changes: 5 additions & 1 deletion crates/nu-cmd-lang/src/core_commands/do_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ impl Command for Do {

fn signature(&self) -> Signature {
Signature::build("do")
.required("closure", SyntaxShape::Any, "the closure to run")
.required(
"closure",
SyntaxShape::OneOf(vec![SyntaxShape::Closure(None), SyntaxShape::Any]),
"the closure to run",
)
.input_output_types(vec![(Type::Any, Type::Any)])
.switch(
"ignore-errors",
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-cmd-lang/src/core_commands/try_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ impl Command for Try {
"catch_block",
SyntaxShape::Keyword(
b"catch".to_vec(),
Box::new(SyntaxShape::Closure(Some(vec![SyntaxShape::Any]))),
Box::new(SyntaxShape::OneOf(vec![
SyntaxShape::Closure(None),
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
])),
),
"block to run if try block fails",
)
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/debug/timeit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn test_time_closure() {
use nu_test_support::{nu, nu_repl_code, playground::Playground};
Playground::setup("test_time_closure", |dirs, _| {
let inp = [
r#"[2 3 4] | timeit { to nuon | save foo.txt }"#,
r#"[2 3 4] | timeit {|_| to nuon | save foo.txt }"#,
"open foo.txt",
];
let actual_repl = nu!(cwd: dirs.test(), nu_repl_code(&inp));
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/filters/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Command for All {
},
Example {
description: "Check that each item is a string",
example: "[foo bar 2 baz] | all { ($in | describe) == 'string' }",
example: "[foo bar 2 baz] | all {|| ($in | describe) == 'string' }",
result: Some(Value::test_bool(false)),
},
Example {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/filters/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Command for Any {
},
Example {
description: "Check that any item is a string",
example: "[1 2 3 4] | any { ($in | describe) == 'string' }",
example: "[1 2 3 4] | any {|| ($in | describe) == 'string' }",
result: Some(Value::test_bool(false)),
},
Example {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/filters/each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ with 'transpose' first."#
}),
},
Example {
example: "{major:2, minor:1, patch:4} | values | each { into string }",
example: "{major:2, minor:1, patch:4} | values | each {|| into string }",
description: "Produce a list of values in the record, converted to string",
result: Some(Value::List {
vals: vec![
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/tests/commands/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn works_with_1_param_blocks() {
fn works_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | all { print $in | true }"#
r#"[1 2 3] | all {|| print $in | true }"#
));

assert_eq!(actual.out, "123true");
Expand All @@ -105,7 +105,7 @@ fn early_exits_with_1_param_blocks() {
fn early_exits_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | all { print $in | false }"#
r#"[1 2 3] | all {|| print $in | false }"#
));

assert_eq!(actual.out, "1false");
Expand All @@ -125,7 +125,7 @@ fn all_uses_enumerate_index() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | all { print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
"[1 2] | all {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
);

assert_eq!(actual.out, "truetruetrue");
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/tests/commands/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn works_with_1_param_blocks() {
fn works_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | any { print $in | false }"#
r#"[1 2 3] | any {|| print $in | false }"#
));

assert_eq!(actual.out, "123false");
Expand All @@ -81,7 +81,7 @@ fn early_exits_with_1_param_blocks() {
fn early_exits_with_0_param_blocks() {
let actual = nu!(
cwd: ".", pipeline(
r#"[1 2 3] | any { print $in | true }"#
r#"[1 2 3] | any {|| print $in | true }"#
));

assert_eq!(actual.out, "1true");
Expand All @@ -101,7 +101,7 @@ fn any_uses_enumerate_index() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | any { print ($env.PWD | str ends-with 'formats') | cd '/' | false } | to nuon"
"[1 2] | any {|| print ($env.PWD | str ends-with 'formats') | cd '/' | false } | to nuon"
);

assert_eq!(actual.out, "truetruefalse");
Expand Down
14 changes: 7 additions & 7 deletions crates/nu-command/tests/commands/bytes/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn long_stream_binary_overflow() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater (0x[01]) 32768 | bytes starts-with (0..32768 | each { 0x[01] } | bytes collect)
nu --testbin repeater (0x[01]) 32768 | bytes starts-with (0..32768 | each {|| 0x[01] } | bytes collect)
"#
);

Expand All @@ -92,7 +92,7 @@ fn long_stream_binary_exact() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater (0x[01020304]) 8192 | bytes starts-with (0..<8192 | each { 0x[01020304] } | bytes collect)
nu --testbin repeater (0x[01020304]) 8192 | bytes starts-with (0..<8192 | each {|| 0x[01020304] } | bytes collect)
"#
);

Expand All @@ -105,7 +105,7 @@ fn long_stream_string_exact() {
let actual = nu!(
cwd: ".",
r#"
nu --testbin repeater hell 8192 | bytes starts-with (0..<8192 | each { "hell" | into binary } | bytes collect)
nu --testbin repeater hell 8192 | bytes starts-with (0..<8192 | each {|| "hell" | into binary } | bytes collect)
"#
);

Expand All @@ -118,8 +118,8 @@ fn long_stream_mixed_exact() {
let actual = nu!(
cwd: ".",
r#"
let binseg = (0..<2048 | each { 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each { "hell" | into binary } | bytes collect)
let binseg = (0..<2048 | each {|| 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each {|| "hell" | into binary } | bytes collect)
nu --testbin repeat_bytes 003d9fbf 2048 68656c6c 2048 | bytes starts-with (bytes build $binseg $strseg)
"#
Expand All @@ -138,8 +138,8 @@ fn long_stream_mixed_overflow() {
let actual = nu!(
cwd: ".",
r#"
let binseg = (0..<2048 | each { 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each { "hell" | into binary } | bytes collect)
let binseg = (0..<2048 | each {|| 0x[003d9fbf] } | bytes collect)
let strseg = (0..<2048 | each {|| "hell" | into binary } | bytes collect)
nu --testbin repeat_bytes 003d9fbf 2048 68656c6c 2048 | bytes starts-with (bytes build $binseg $strseg 0x[01])
"#
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-command/tests/commands/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn reports_emptiness() {
cwd: ".", pipeline(
r#"
[[] '' {} null]
| all {
| all {||
is-empty
}
"#
Expand All @@ -21,7 +21,7 @@ fn reports_nonemptiness() {
cwd: ".", pipeline(
r#"
[[1] ' ' {a:1} 0]
| any {
| any {||
is-empty
}
"#
Expand All @@ -36,7 +36,7 @@ fn reports_emptiness_by_columns() {
cwd: ".", pipeline(
r#"
[{a:1 b:null c:null} {a:2 b:null c:null}]
| any {
| any {||
is-empty b c
}
"#
Expand All @@ -51,7 +51,7 @@ fn reports_nonemptiness_by_columns() {
cwd: ".", pipeline(
r#"
[{a:1 b:null c:3} {a:null b:5 c:2}]
| any {
| any {||
is-empty a b
}
"#
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn errors_if_given_unknown_column_name() {
cwd: dirs.test(), pipeline(
r#"
open los_tres_caballeros.json
| group-by { get nu.releases.version }
| group-by {|| get nu.releases.version }
"#
));

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ fn list_ignores_ansi() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls | find .txt | each { ls $in.name }
ls | find .txt | each {|| ls $in.name }
"#
));

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn sets_the_column_from_a_block_full_stream_output() {
cwd: "tests/fixtures/formats", pipeline(
r#"
{content: null}
| update content { open --raw cargo_sample.toml | lines | first 5 }
| update content {|| open --raw cargo_sample.toml | lines | first 5 }
| get content.1
| str contains "nu"
"#
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn sets_the_column_from_a_block_full_stream_output() {
cwd: "tests/fixtures/formats", pipeline(
r#"
{content: null}
| upsert content { open --raw cargo_sample.toml | lines | first 5 }
| upsert content {|| open --raw cargo_sample.toml | lines | first 5 }
| get content.1
| str contains "nu"
"#
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/tests/commands/where_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn where_inside_block_works() {
fn filters_with_0_arity_block() {
let actual = nu!(
cwd: ".",
"[1 2 3 4] | where { $in < 3 } | to nuon"
"[1 2 3 4] | where {|| $in < 3 } | to nuon"
);

assert_eq!(actual.out, "[1, 2]");
Expand All @@ -55,7 +55,7 @@ fn filters_with_1_arity_block() {
fn unique_env_each_iteration() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"[1 2] | where { print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
"[1 2] | where {|| print ($env.PWD | str ends-with 'formats') | cd '/' | true } | to nuon"
);

assert_eq!(actual.out, "truetrue[1, 2]");
Expand Down
8 changes: 8 additions & 0 deletions crates/nu-parser/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ pub enum ParseError {
#[diagnostic(code(nu::parser::parse_mismatch))]
Expected(String, #[label("expected {0}")] Span),

#[error("Missing || inside closure")]
#[diagnostic(
code(nu::parser::closure_missing_pipe),
help("Try add || to the beginning of closure")
)]
ClosureMissingPipe(#[label("Parsing as a closure, but || is missing")] Span),

#[error("Type mismatch during operation.")]
#[diagnostic(code(nu::parser::type_mismatch))]
Mismatch(String, String, #[label("expected {0}, found {1}")] Span), // expected, found, span
Expand Down Expand Up @@ -487,6 +494,7 @@ impl ParseError {
ParseError::UnknownOperator(_, _, s) => *s,
ParseError::InvalidLiteral(_, _, s) => *s,
ParseError::NotAConstant(s) => *s,
ParseError::ClosureMissingPipe(s) => *s,
}
}
}
25 changes: 19 additions & 6 deletions crates/nu-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,8 +1779,10 @@ pub fn parse_brace_expr(

if matches!(second_token, None) {
// If we're empty, that means an empty record or closure
if matches!(shape, SyntaxShape::Closure(_)) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist)
if matches!(shape, SyntaxShape::Closure(None)) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, false)
} else if matches!(shape, SyntaxShape::Closure(Some(_))) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true)
} else if matches!(shape, SyntaxShape::Block) {
parse_block_expression(working_set, span, expand_aliases_denylist)
} else {
Expand All @@ -1789,11 +1791,13 @@ pub fn parse_brace_expr(
} else if matches!(second_token_contents, Some(TokenContents::Pipe))
|| matches!(second_token_contents, Some(TokenContents::PipePipe))
{
parse_closure_expression(working_set, shape, span, expand_aliases_denylist)
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true)
} else if matches!(third_token, Some(b":")) {
parse_full_cell_path(working_set, None, span, expand_aliases_denylist)
} else if matches!(shape, SyntaxShape::Closure(_)) || matches!(shape, SyntaxShape::Any) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist)
} else if matches!(shape, SyntaxShape::Closure(None)) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, false)
} else if matches!(shape, SyntaxShape::Closure(Some(_))) || matches!(shape, SyntaxShape::Any) {
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true)
} else if matches!(shape, SyntaxShape::Block) {
parse_block_expression(working_set, span, expand_aliases_denylist)
} else {
Expand Down Expand Up @@ -4414,6 +4418,7 @@ pub fn parse_closure_expression(
shape: &SyntaxShape,
span: Span,
expand_aliases_denylist: &[usize],
require_pipe: bool,
) -> (Expression, Option<ParseError>) {
trace!("parsing: closure expression");

Expand Down Expand Up @@ -4490,7 +4495,15 @@ pub fn parse_closure_expression(
Some((Box::new(Signature::new("closure".to_string())), *span)),
1,
),
_ => (None, 0),
_ => {
if require_pipe {
error = error.or(Some(ParseError::ClosureMissingPipe(span)));
working_set.exit_scope();
return (garbage(span), error);
} else {
(None, 0)
}
}
};

// TODO: Finish this
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-parser/tests/test_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,13 @@ mod input_types {
)
.optional(
"else_expression",
SyntaxShape::Keyword(b"else".to_vec(), Box::new(SyntaxShape::Expression)),
SyntaxShape::Keyword(
b"else".to_vec(),
Box::new(SyntaxShape::OneOf(vec![
SyntaxShape::Block,
SyntaxShape::Expression,
])),
),
"expression or block to run if check fails",
)
.category(Category::Core)
Expand Down
Loading

0 comments on commit a8eef9a

Please sign in to comment.