Merge pull request #4758 from joshuawarner32/fuzzing-take-2

Add fuzzing for the formatter and fix bugs
This commit is contained in:
Ayaz 2022-12-22 06:35:34 -07:00 committed by GitHub
commit dbdf4acdd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 1286 additions and 471 deletions

View file

@ -12,9 +12,9 @@ use roc_parse::{
ModuleName, PackageEntry, PackageHeader, PackageName, PlatformHeader, PlatformRequires,
ProvidesTo, To, TypedIdent,
},
ident::UppercaseIdent,
ident::{BadIdent, UppercaseIdent},
};
use roc_region::all::{Loc, Region};
use roc_region::all::{Loc, Position, Region};
use crate::{Ast, Buf};
@ -32,6 +32,17 @@ pub fn fmt_default_spaces<'a, 'buf>(
fmt_spaces(buf, spaces.iter(), indent);
}
}
pub fn fmt_default_newline<'a, 'buf>(
buf: &mut Buf<'buf>,
spaces: &[CommentOrNewline<'a>],
indent: u16,
) {
if spaces.is_empty() {
buf.newline();
} else {
fmt_spaces(buf, spaces.iter(), indent);
}
}
/// Like fmt_spaces, but disallows two consecutive newlines.
pub fn fmt_spaces_no_blank_lines<'a, 'buf, I>(buf: &mut Buf<'buf>, spaces: I, indent: u16)
@ -714,7 +725,7 @@ impl<'a> RemoveSpaces<'a> for Expr<'a> {
// The formatter can remove redundant parentheses, so also remove these when normalizing for comparison.
a.remove_spaces(arena)
}
Expr::MalformedIdent(a, b) => Expr::MalformedIdent(a, b),
Expr::MalformedIdent(a, b) => Expr::MalformedIdent(a, remove_spaces_bad_ident(b)),
Expr::MalformedClosure => Expr::MalformedClosure,
Expr::PrecedenceConflict(a) => Expr::PrecedenceConflict(a),
Expr::SpaceBefore(a, _) => a.remove_spaces(arena),
@ -724,6 +735,20 @@ impl<'a> RemoveSpaces<'a> for Expr<'a> {
}
}
fn remove_spaces_bad_ident(ident: BadIdent) -> BadIdent {
match ident {
BadIdent::Start(_) => BadIdent::Start(Position::zero()),
BadIdent::Space(e, _) => BadIdent::Space(e, Position::zero()),
BadIdent::Underscore(_) => BadIdent::Underscore(Position::zero()),
BadIdent::QualifiedTag(_) => BadIdent::QualifiedTag(Position::zero()),
BadIdent::WeirdAccessor(_) => BadIdent::WeirdAccessor(Position::zero()),
BadIdent::WeirdDotAccess(_) => BadIdent::WeirdDotAccess(Position::zero()),
BadIdent::WeirdDotQualified(_) => BadIdent::WeirdDotQualified(Position::zero()),
BadIdent::StrayDot(_) => BadIdent::StrayDot(Position::zero()),
BadIdent::BadOpaqueRef(_) => BadIdent::BadOpaqueRef(Position::zero()),
}
}
impl<'a> RemoveSpaces<'a> for Pattern<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self {
match *self {
@ -755,7 +780,7 @@ impl<'a> RemoveSpaces<'a> for Pattern<'a> {
Pattern::StrLiteral(a) => Pattern::StrLiteral(a),
Pattern::Underscore(a) => Pattern::Underscore(a),
Pattern::Malformed(a) => Pattern::Malformed(a),
Pattern::MalformedIdent(a, b) => Pattern::MalformedIdent(a, b),
Pattern::MalformedIdent(a, b) => Pattern::MalformedIdent(a, remove_spaces_bad_ident(b)),
Pattern::QualifiedIdentifier { module_name, ident } => {
Pattern::QualifiedIdentifier { module_name, ident }
}