Migrate type vars to snake case (Redo)

This commit is contained in:
Anthony Bullard 2025-01-03 19:23:19 -06:00
parent 1b15896212
commit 89b7900d7a
No known key found for this signature in database
6 changed files with 484 additions and 81 deletions

View file

@ -7,6 +7,7 @@
use crate::test_helpers::{Input, InputKind};
use bumpalo::Bump;
use roc_fmt::MigrationFlags;
use roc_parse::{ast::Malformed, normalize::Normalize};
#[derive(Copy, Clone, Debug)]
@ -122,7 +123,12 @@ fn round_trip_once(input: Input<'_>, options: Options) -> Option<String> {
}
}
let output = actual.format();
let flags = MigrationFlags {
snakify: false,
parens_and_commas: false,
};
let output = actual.format(flags);
let reparsed_ast = match output.as_ref().parse_in(&arena) {
Ok(r) => r,
@ -142,7 +148,7 @@ fn round_trip_once(input: Input<'_>, options: Options) -> Option<String> {
return Some("Different ast".to_string());
}
let reformatted = reparsed_ast.format();
let reformatted = reparsed_ast.format(flags);
if output != reformatted {
return Some("Formatting not stable".to_string());

View file

@ -108,12 +108,8 @@ pub enum Output<'a> {
}
impl<'a> Output<'a> {
pub fn format(&self) -> InputOwned {
pub fn format(&self, flags: MigrationFlags) -> InputOwned {
let arena = Bump::new();
let flags = MigrationFlags {
snakify: false,
parens_and_commas: false,
};
let mut buf = Buf::new_in(&arena, flags);
match self {
Output::Header(header) => {
@ -334,16 +330,33 @@ impl<'a> Input<'a> {
}
}
/// Parse and re-format the given input, and pass the output to `check_formatting`
/// for verification. The expectation is that `check_formatting` assert the result matches
/// expectations (or, overwrite the expectation based on a command-line flag)
/// Optionally, based on the value of `check_idempotency`, also verify that the formatting
/// is idempotent - that if we reformat the output, we get the same result.
pub fn check_invariants(
&self,
handle_formatted_output: impl Fn(Input),
check_idempotency: bool,
canonicalize_mode: Option<bool>,
) {
self.check_invariants_with_flags(
handle_formatted_output,
check_idempotency,
canonicalize_mode,
MigrationFlags {
snakify: false,
parens_and_commas: false,
},
);
}
/// Parse and re-format the given input, and pass the output to `check_formatting`
/// for verification. The expectation is that `check_formatting` assert the result matches
/// expectations (or, overwrite the expectation based on a command-line flag)
/// Optionally, based on the value of `check_idempotency`, also verify that the formatting
/// is idempotent - that if we reformat the output, we get the same result.
pub fn check_invariants_with_flags(
&self,
handle_formatted_output: impl Fn(Input),
check_idempotency: bool,
canonicalize_mode: Option<bool>,
flags: MigrationFlags,
) {
let arena = Bump::new();
@ -351,7 +364,7 @@ impl<'a> Input<'a> {
panic!("Unexpected parse failure when parsing this for formatting:\n\n{}\n\nParse error was:\n\n{:#?}\n\n", self.as_str(), err);
});
let output = actual.format();
let output = actual.format(flags);
handle_formatted_output(output.as_ref());
@ -369,31 +382,33 @@ impl<'a> Input<'a> {
);
});
let ast_normalized = actual.normalize(&arena);
let reparsed_ast_normalized = reparsed_ast.normalize(&arena);
if !flags.at_least_one_active() {
let ast_normalized = actual.normalize(&arena);
let reparsed_ast_normalized = reparsed_ast.normalize(&arena);
// HACK!
// We compare the debug format strings of the ASTs, because I'm finding in practice that _somewhere_ deep inside the ast,
// the PartialEq implementation is returning `false` even when the Debug-formatted impl is exactly the same.
// I don't have the patience to debug this right now, so let's leave it for another day...
// TODO: fix PartialEq impl on ast types
if format!("{ast_normalized:?}") != format!("{reparsed_ast_normalized:?}") {
panic!(
"Formatting bug; formatting didn't reparse to the same AST (after removing spaces)\n\n\
* * * Source code before formatting:\n{}\n\n\
* * * Source code after formatting:\n{}\n\n\
* * * AST before formatting:\n{:#?}\n\n\
* * * AST after formatting:\n{:#?}\n\n",
self.as_str(),
output.as_ref().as_str(),
actual,
reparsed_ast
);
// HACK!
// We compare the debug format strings of the ASTs, because I'm finding in practice that _somewhere_ deep inside the ast,
// the PartialEq implementation is returning `false` even when the Debug-formatted impl is exactly the same.
// I don't have the patience to debug this right now, so let's leave it for another day...
// TODO: fix PartialEq impl on ast types
if format!("{ast_normalized:?}") != format!("{reparsed_ast_normalized:?}") {
panic!(
"Formatting bug; formatting didn't reparse to the same AST (after removing spaces)\n\n\
* * * Source code before formatting:\n{}\n\n\
* * * Source code after formatting:\n{}\n\n\
* * * AST before formatting:\n{:#?}\n\n\
* * * AST after formatting:\n{:#?}\n\n",
self.as_str(),
output.as_ref().as_str(),
actual,
reparsed_ast
);
}
}
// Now verify that the resultant formatting is _idempotent_ - i.e. that it doesn't change again if re-formatted
if check_idempotency {
let reformatted = reparsed_ast.format();
let reformatted = reparsed_ast.format(flags);
if output != reformatted {
eprintln!("Formatting bug; formatting is not stable.\nOriginal code:\n{}\n\nFormatted code:\n{}\n\nAST:\n{:#?}\n\nReparsed AST:\n{:#?}\n\n",