use super::collections::VecU32MapWithDefault;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
static LOGGED: AtomicBool = AtomicBool::new(false);
const MAX_COUNT: u16 = 1_000;
const FLIPPING_COUNT: u16 = 10;
pub struct InfiniteReevaluationProtector {
reevaluation_count: VecU32MapWithDefault<u16>,
flipping_condition_names: Vec<&'static str>,
}
impl InfiniteReevaluationProtector {
pub fn with_capacity(capacity: u32) -> Self {
Self {
reevaluation_count: VecU32MapWithDefault::with_capacity(capacity),
flipping_condition_names: Vec::new(),
}
}
pub fn should_reevaluate(&mut self, reevaluation_id: u32, condition_name: &'static str, current_value: Option<bool>, last_value: bool) -> bool {
let current_value = current_value.unwrap_or(false);
if current_value == last_value {
true
} else {
let next_count = self.reevaluation_count.get(reevaluation_id).unwrap() + 1;
if next_count == MAX_COUNT + 1 {
return false;
}
self.reevaluation_count.set(reevaluation_id, next_count);
if next_count == FLIPPING_COUNT && !self.flipping_condition_names.contains(&condition_name) {
self.flipping_condition_names.push(condition_name);
}
if next_count == MAX_COUNT {
if !LOGGED.swap(true, Ordering::SeqCst) {
#[allow(clippy::print_stderr)]
{
eprintln!("{}", self.exceeded_message(condition_name));
}
}
false
} else {
true
}
}
}
fn exceeded_message(&self, _stopped_condition_name: &'static str) -> String {
#[allow(unused_mut)]
let mut message =
String::from("[dprint-core] A file exceeded the re-evaluation count and formatting stabilized at a random condition value. Please report this as a bug.");
#[cfg(debug_assertions)]
{
message.push_str(&format!(
"\n Stopped condition: {}\n Conditions that kept flipping: {}",
_stopped_condition_name,
self.flipping_condition_names.join(", ")
));
}
message
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn should_keep_track_flipping_reevaluation() {
let mut protector = InfiniteReevaluationProtector::with_capacity(1);
for _ in 0..999 {
assert!(protector.should_reevaluate(0, "a", Some(true), false));
}
assert!(!protector.should_reevaluate(0, "a", Some(true), false));
assert!(!protector.should_reevaluate(0, "a", Some(true), false));
assert!(protector.should_reevaluate(0, "a", Some(true), true));
}
#[test]
fn should_count_flips_cumulatively_across_non_flips() {
let mut protector = InfiniteReevaluationProtector::with_capacity(1);
for _ in 0..999 {
assert!(protector.should_reevaluate(0, "a", Some(true), false));
assert!(protector.should_reevaluate(0, "a", Some(true), true));
}
assert!(!protector.should_reevaluate(0, "a", Some(true), false));
assert!(!protector.should_reevaluate(0, "a", Some(true), false));
}
#[test]
fn should_name_the_flipping_conditions() {
let mut protector = InfiniteReevaluationProtector::with_capacity(3);
for _ in 0..FLIPPING_COUNT - 1 {
protector.should_reevaluate(2, "stabilizes", Some(true), false);
}
for _ in 0..MAX_COUNT - 1 {
protector.should_reevaluate(0, "first", Some(true), false);
protector.should_reevaluate(1, "second", Some(true), false);
}
let message = protector.exceeded_message("first");
if cfg!(debug_assertions) {
assert!(
message.ends_with("\n Stopped condition: first\n Conditions that kept flipping: first, second"),
"{}",
message
);
} else {
assert!(!message.contains("first"), "{}", message);
}
}
}