mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Improve normalization to handle cases found in fuzzing
This commit is contained in:
parent
912db1b76b
commit
61d885039d
5 changed files with 93 additions and 27 deletions
|
@ -396,10 +396,22 @@ impl<'a> Normalize<'a> for ValueDef<'a> {
|
|||
|
||||
match *self {
|
||||
Annotation(a, b) => Annotation(a.normalize(arena), b.normalize(arena)),
|
||||
Body(a, b) => Body(
|
||||
arena.alloc(a.normalize(arena)),
|
||||
arena.alloc(b.normalize(arena)),
|
||||
),
|
||||
Body(a, b) => {
|
||||
let a = a.normalize(arena);
|
||||
let b = b.normalize(arena);
|
||||
|
||||
let is_unit_assignment = if let Pattern::RecordDestructure(collection) = a.value {
|
||||
collection.is_empty()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if is_unit_assignment {
|
||||
Stmt(arena.alloc(b))
|
||||
} else {
|
||||
Body(arena.alloc(a), arena.alloc(b))
|
||||
}
|
||||
}
|
||||
AnnotatedBody {
|
||||
ann_pattern,
|
||||
ann_type,
|
||||
|
@ -560,26 +572,6 @@ impl<'a> Normalize<'a> for StrLiteral<'a> {
|
|||
match *self {
|
||||
StrLiteral::PlainLine(t) => StrLiteral::PlainLine(t),
|
||||
StrLiteral::Line(t) => {
|
||||
let mut needs_merge = false;
|
||||
let mut last_was_mergable = false;
|
||||
for segment in t.iter() {
|
||||
let mergable = matches!(
|
||||
segment,
|
||||
StrSegment::Plaintext(_)
|
||||
| StrSegment::Unicode(_)
|
||||
| StrSegment::EscapedChar(_)
|
||||
);
|
||||
if mergable && last_was_mergable {
|
||||
needs_merge = true;
|
||||
break;
|
||||
}
|
||||
last_was_mergable = mergable;
|
||||
}
|
||||
|
||||
if !needs_merge {
|
||||
return StrLiteral::Line(t.normalize(arena));
|
||||
}
|
||||
|
||||
let mut new_segments = Vec::new_in(arena);
|
||||
let mut last_text = String::new_in(arena);
|
||||
|
||||
|
@ -744,7 +736,22 @@ impl<'a> Normalize<'a> for Expr<'a> {
|
|||
}
|
||||
Expr::BinOps(a, b) => Expr::BinOps(a.normalize(arena), arena.alloc(b.normalize(arena))),
|
||||
Expr::UnaryOp(a, b) => {
|
||||
Expr::UnaryOp(arena.alloc(a.normalize(arena)), b.normalize(arena))
|
||||
let a = a.normalize(arena);
|
||||
match (a.value, b.value) {
|
||||
(Expr::Num(text), UnaryOp::Negate) if !text.starts_with('-') => {
|
||||
let mut res = String::new_in(arena);
|
||||
res.push('-');
|
||||
res.push_str(text);
|
||||
Expr::Num(res.into_bump_str())
|
||||
}
|
||||
(Expr::Float(text), UnaryOp::Negate) if !text.starts_with('-') => {
|
||||
let mut res = String::new_in(arena);
|
||||
res.push('-');
|
||||
res.push_str(text);
|
||||
Expr::Float(res.into_bump_str())
|
||||
}
|
||||
_ => Expr::UnaryOp(arena.alloc(a), b.normalize(arena)),
|
||||
}
|
||||
}
|
||||
Expr::If {
|
||||
if_thens,
|
||||
|
@ -765,7 +772,7 @@ impl<'a> Normalize<'a> for Expr<'a> {
|
|||
Expr::PrecedenceConflict(a) => Expr::PrecedenceConflict(a),
|
||||
Expr::SpaceBefore(a, _) => a.normalize(arena),
|
||||
Expr::SpaceAfter(a, _) => a.normalize(arena),
|
||||
Expr::SingleQuote(a) => Expr::Num(a),
|
||||
Expr::SingleQuote(a) => Expr::SingleQuote(a),
|
||||
Expr::EmptyRecordBuilder(a) => {
|
||||
Expr::EmptyRecordBuilder(arena.alloc(a.normalize(arena)))
|
||||
}
|
||||
|
@ -891,7 +898,7 @@ impl<'a> Normalize<'a> for Pattern<'a> {
|
|||
is_negative,
|
||||
},
|
||||
Pattern::FloatLiteral(a) => Pattern::FloatLiteral(a),
|
||||
Pattern::StrLiteral(a) => Pattern::StrLiteral(a),
|
||||
Pattern::StrLiteral(a) => Pattern::StrLiteral(a.normalize(arena)),
|
||||
Pattern::Underscore(a) => Pattern::Underscore(a),
|
||||
Pattern::Malformed(a) => Pattern::Malformed(a),
|
||||
Pattern::MalformedIdent(a, b) => Pattern::MalformedIdent(a, remove_spaces_bad_ident(b)),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue