Skip to content

Commit

Permalink
Relax argument indentation requirements in closures
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawarner32 committed Jan 14, 2025
1 parent dcb0819 commit 9c34030
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
15 changes: 9 additions & 6 deletions crates/compiler/load/tests/test_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions crates/compiler/parse/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Pattern<'a>>> = params;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Expr(Closure(IndentArrow(@10), @4), @0)
Expr(Closure(Arrow(@11), @4), @0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ #
h & } \ #
i
-> 0
Original file line number Diff line number Diff line change
@@ -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,
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#
h&}\#
i->0
1 change: 1 addition & 0 deletions crates/compiler/test_syntax/tests/test_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9c34030

Please sign in to comment.