Fix crash with doubly-nested parens in patterns

This commit is contained in:
Joshua Warner 2024-12-24 14:08:52 -05:00
parent 941c6c4fe3
commit 8f0566a55f
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
7 changed files with 82 additions and 1 deletions

View file

@ -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)]
@ -111,6 +115,7 @@ 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 {
@ -118,6 +123,10 @@ impl<'a, T: ExtractSpaces<'a>> ExtractSpaces<'a> for &'a T {
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> {
@ -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> {
@ -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,
}
}
}
};
}
@ -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> {
@ -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 {

View file

@ -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,
..