Skip to content

Commit

Permalink
Fix crash with doubly-nested parens in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawarner32 committed Dec 24, 2024
1 parent 941c6c4 commit 8f0566a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/compiler/fmt/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,10 @@ impl<'a, T: Copy> ExtractSpaces<'a> for NodeSpaces<'a, T> {
after: self.after,
}
}

fn without_spaces(&self) -> T {
self.item
}
}

impl<'a, V: Formattable> Formattable for NodeSpaces<'a, V> {
Expand Down
40 changes: 40 additions & 0 deletions crates/compiler/parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl<'a, T: Copy> ExtractSpaces<'a> for Spaces<'a, T> {
fn extract_spaces(&self) -> Spaces<'a, T> {
*self
}

fn without_spaces(&self) -> T {
self.item
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -111,13 +115,18 @@ impl<'a, T: Debug> Debug for Spaced<'a, T> {
pub trait ExtractSpaces<'a>: Sized + Copy {
type Item;
fn extract_spaces(&self) -> Spaces<'a, Self::Item>;
fn without_spaces(&self) -> Self::Item;
}

impl<'a, T: ExtractSpaces<'a>> ExtractSpaces<'a> for &'a T {
type Item = T::Item;
fn extract_spaces(&self) -> Spaces<'a, Self::Item> {
(*self).extract_spaces()
}

fn without_spaces(&self) -> Self::Item {
(*self).without_spaces()
}
}

impl<'a, T: ExtractSpaces<'a>> ExtractSpaces<'a> for Loc<T> {
Expand All @@ -130,6 +139,10 @@ impl<'a, T: ExtractSpaces<'a>> ExtractSpaces<'a> for Loc<T> {
after: spaces.after,
}
}

fn without_spaces(&self) -> Self::Item {
self.value.without_spaces()
}
}

impl<'a> Header<'a> {
Expand Down Expand Up @@ -2350,6 +2363,17 @@ macro_rules! impl_extract_spaces {
}
}
}
fn without_spaces(&self) -> Self::Item {
match self {
$t::SpaceBefore(item, _) => {
item.without_spaces()
},
$t::SpaceAfter(item, _) => {
item.without_spaces()
},
_ => *self,
}
}
}
};
}
Expand Down Expand Up @@ -2412,6 +2436,14 @@ impl<'a, T: Copy> ExtractSpaces<'a> for Spaced<'a, T> {
},
}
}

fn without_spaces(&self) -> T {
match self {
Spaced::SpaceBefore(item, _) => item.without_spaces(),
Spaced::SpaceAfter(item, _) => item.without_spaces(),
Spaced::Item(item) => *item,
}
}
}

impl<'a> ExtractSpaces<'a> for AbilityImpls<'a> {
Expand Down Expand Up @@ -2454,6 +2486,14 @@ impl<'a> ExtractSpaces<'a> for AbilityImpls<'a> {
},
}
}

fn without_spaces(&self) -> Self::Item {
match self {
AbilityImpls::AbilityImpls(inner) => *inner,
AbilityImpls::SpaceBefore(item, _) => item.without_spaces(),
AbilityImpls::SpaceAfter(item, _) => item.without_spaces(),
}
}
}

pub trait Malformed {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/parse/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn loc_tag_pattern_arg<'a>(

if stop_on_has_kw
&& matches!(
value.extract_spaces().item,
value.without_spaces(),
Pattern::Identifier {
ident: crate::keyword::IMPLEMENTS,
..
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\L z -> 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@0-13 SpaceAfter(
Closure(
[
@1-5 Apply(
@1-2 Tag(
"L",
),
[
@4-5 SpaceAfter(
SpaceAfter(
Identifier {
ident: "z",
},
[
Newline,
],
),
[
Newline,
],
),
],
),
],
@11-13 Num(
"42",
),
),
[
Newline,
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\L((z
)
)->42
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 @@ -353,6 +353,7 @@ mod test_snapshots {
pass/closure_in_apply_in_binop.expr,
pass/closure_in_binop_with_spaces.expr,
pass/closure_newline_empty_record_newline.expr,
pass/closure_parens_double_newlines.expr,
pass/closure_pat_reccord_comment.expr,
pass/closure_with_underscores.expr,
pass/comma_prefixed_indented_record.expr,
Expand Down

0 comments on commit 8f0566a

Please sign in to comment.