From 9c340302e2b83246ad3857337b7f5d22fcd58a7f Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Mon, 13 Jan 2025 21:34:06 -0800 Subject: [PATCH] Relax argument indentation requirements in closures --- crates/compiler/load/tests/test_reporting.rs | 15 ++++--- crates/compiler/parse/src/expr.rs | 6 +-- ..._closure_pattern_in_parens.expr.result-ast | 2 +- ..._apply_closure_comments.expr.formatted.roc | 4 ++ ...ate_apply_closure_comments.expr.result-ast | 41 +++++++++++++++++++ ...ord_update_apply_closure_comments.expr.roc | 3 ++ .../test_syntax/tests/test_snapshots.rs | 1 + 7 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.formatted.roc create mode 100644 crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.result-ast create mode 100644 crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.roc diff --git a/crates/compiler/load/tests/test_reporting.rs b/crates/compiler/load/tests/test_reporting.rs index 84ba1a06468..081e8c964c2 100644 --- a/crates/compiler/load/tests/test_reporting.rs +++ b/crates/compiler/load/tests/test_reporting.rs @@ -6692,17 +6692,20 @@ All branches in an `if` must have the same type! ) " ), - @r" - ── UNFINISHED FUNCTION in tmp/unfinished_closure_pattern_in_parens/Test.roc ──── + @r###" + ── MISSING ARROW in tmp/unfinished_closure_pattern_in_parens/Test.roc ────────── - I was partway through parsing a function, but I got stuck here: + I am partway through parsing a function argument list, but I got stuck + here: 4│ x = \( a 5│ ) - ^ + 6│ + 7│ + ^ - I just saw a pattern, so I was expecting to see a -> next. - " + I was expecting a -> next. + "### ); test_report!( diff --git a/crates/compiler/parse/src/expr.rs b/crates/compiler/parse/src/expr.rs index 898b6f17b4f..3520e8d440a 100644 --- a/crates/compiler/parse/src/expr.rs +++ b/crates/compiler/parse/src/expr.rs @@ -2330,12 +2330,12 @@ fn closure_help<'a>(check_for_arrow: CheckForArrow) -> impl Parser<'a, Expr<'a>, // closure_help_help(options) map_with_arena( // After the first token, all other tokens must be indented past the start of the line - indented_seq_skip_first( + skip_first( // All closures start with a '\' - e.g. (\x -> x + 1) byte_indent(b'\\', EClosure::Start), // Once we see the '\', we're committed to parsing this as a closure. // It may turn out to be malformed, but it is definitely a closure. - and( + reset_min_indent(and( // Parse the params // Params are comma-separated sep_by1_e( @@ -2353,7 +2353,7 @@ fn closure_help<'a>(check_for_arrow: CheckForArrow) -> impl Parser<'a, Expr<'a>, // Parse the body block(check_for_arrow, true, EClosure::IndentBody, EClosure::Body), ), - ), + )), ), |arena: &'a Bump, (params, body)| { let params: Vec<'a, Loc>> = params; diff --git a/crates/compiler/test_syntax/tests/snapshots/fail/unfinished_closure_pattern_in_parens.expr.result-ast b/crates/compiler/test_syntax/tests/snapshots/fail/unfinished_closure_pattern_in_parens.expr.result-ast index 37c92c7aa61..de6d3cd7eb1 100644 --- a/crates/compiler/test_syntax/tests/snapshots/fail/unfinished_closure_pattern_in_parens.expr.result-ast +++ b/crates/compiler/test_syntax/tests/snapshots/fail/unfinished_closure_pattern_in_parens.expr.result-ast @@ -1 +1 @@ -Expr(Closure(IndentArrow(@10), @4), @0) \ No newline at end of file +Expr(Closure(Arrow(@11), @4), @0) \ No newline at end of file diff --git a/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.formatted.roc b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.formatted.roc new file mode 100644 index 00000000000..9a520040244 --- /dev/null +++ b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.formatted.roc @@ -0,0 +1,4 @@ +{ # + h & } \ # + i + -> 0 \ No newline at end of file diff --git a/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.result-ast b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.result-ast new file mode 100644 index 00000000000..ad8a9ea734b --- /dev/null +++ b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.result-ast @@ -0,0 +1,41 @@ +@0-14 SpaceAfter( + Apply( + @0-6 RecordUpdate { + update: @3-4 SpaceBefore( + Var { + module_name: "", + ident: "h", + }, + [ + LineComment( + "", + ), + ], + ), + fields: [], + }, + [ + @6-14 Closure( + [ + @10-11 SpaceBefore( + Identifier { + ident: "i", + }, + [ + LineComment( + "", + ), + ], + ), + ], + @13-14 Num( + "0", + ), + ), + ], + Space, + ), + [ + Newline, + ], +) diff --git a/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.roc b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.roc new file mode 100644 index 00000000000..9fe156971da --- /dev/null +++ b/crates/compiler/test_syntax/tests/snapshots/pass/record_update_apply_closure_comments.expr.roc @@ -0,0 +1,3 @@ +{# +h&}\# + i->0 diff --git a/crates/compiler/test_syntax/tests/test_snapshots.rs b/crates/compiler/test_syntax/tests/test_snapshots.rs index ea6f3abdc8c..63b23617997 100644 --- a/crates/compiler/test_syntax/tests/test_snapshots.rs +++ b/crates/compiler/test_syntax/tests/test_snapshots.rs @@ -670,6 +670,7 @@ mod test_snapshots { pass/record_literal_field_bang.expr, pass/record_type_with_function.expr, pass/record_update.expr, + pass/record_update_apply_closure_comments.expr, pass/record_update_comment_before_ampersand.expr, pass/record_updater_closure_weirdness.expr, pass/record_updater_literal_apply.expr,