Merge pull request #7409 from joshuawarner32/fuzzing-bugs-5

Fix another batch of parsing/formatting bugs found in fuzzing
This commit is contained in:
Luke Boswell 2024-12-28 17:36:03 +11:00 committed by GitHub
commit f7dbf850b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 750 additions and 74 deletions

View file

@ -1329,6 +1329,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
item,
after,
},
true,
)
} else {
Spaces {
@ -1369,7 +1370,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
after: new_res.after,
};
if parens == Parens::InCollection || parens == Parens::InApply {
parens_around_node(arena, inner)
parens_around_node(arena, inner, true)
} else {
inner
}
@ -1382,7 +1383,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
after: lifted.after,
};
if parens == Parens::InApply || parens == Parens::InAsPattern {
parens_around_node(arena, item)
parens_around_node(arena, item, true)
} else {
item
}
@ -1402,12 +1403,24 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
fn parens_around_node<'a, 'b: 'a>(
arena: &'a Bump,
item: Spaces<'b, Node<'b>>,
allow_spaces_before: bool,
) -> Spaces<'a, Node<'a>> {
Spaces {
before: &[],
before: if allow_spaces_before {
item.before
} else {
&[]
},
item: Node::DelimitedSequence(
Braces::Round,
arena.alloc_slice_copy(&[(item.before.into(), item.item)]),
arena.alloc_slice_copy(&[(
if allow_spaces_before {
Sp::empty()
} else {
item.before.into()
},
item.item,
)]),
Sp::empty(),
),
// We move the comments/newlines to the outer scope, since they tend to migrate there when re-parsed
@ -1432,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> {

View file

@ -291,7 +291,7 @@ fn format_expr_only(
let inner_parens = if needs_parens {
Parens::NotNeeded
} else {
Parens::InApply
Parens::InApplyLastArg
};
if !before_all_newlines {
@ -1314,13 +1314,21 @@ pub fn expr_lift_spaces<'a, 'b: 'a>(
after: right_lifted.after,
}
}
Expr::UnaryOp(expr, op) => {
let expr_lifted = expr_lift_spaces_after(Parens::InOperator, arena, &expr.value);
Expr::UnaryOp(inner, op) => {
if parens == Parens::InApply && matches!(inner.without_spaces(), Expr::Closure(..)) {
return Spaces {
before: &[],
item: Expr::ParensAround(arena.alloc(*expr)),
after: &[],
};
}
let inner_lifted = expr_lift_spaces_after(Parens::InOperator, arena, &inner.value);
Spaces {
before: &[],
item: Expr::UnaryOp(arena.alloc(Loc::at(expr.region, expr_lifted.item)), *op),
after: expr_lifted.after,
item: Expr::UnaryOp(arena.alloc(Loc::at(inner.region, inner_lifted.item)), *op),
after: inner_lifted.after,
}
}