Lines
100 %
Functions
40 %
Branches
//! Settles the homes of a unit's stored globals, struct fields and assigned
//! binders (plan 0014 stages GJ and F2a): a home's type is the join of its init
//! and every store into it, and a store's type exists only where it compiles.
//! The unit compiles again, from the table as it was, until the homes it
//! allocates are the join of its stores.
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
/// Runs `attempt` until no home it allocated is narrower than the join of the
/// stores it compiled. A unit whose homes are right the first time compiles
/// once; a unit that never settles is an `Error::Resource`, so the loop is
/// bounded by the homes the unit can widen (three steps each, plus the first
/// attempt). A unit that does not compile ran nothing, so it leaves the table
/// as it was.
pub(super) fn settle_homes<T>(
symbols: &mut SymbolTable,
mut attempt: impl FnMut(&mut SymbolTable) -> Result<T>,
) -> Result<T> {
let pristine = symbols.clone();
symbols.begin_home_attempt(Default::default());
let mut attempts = 1;
loop {
let result = attempt(symbols)
.map_err(|err| symbols.explain_expired(err))
.map_err(|err| symbols.name_structs(err));
let Some(hints) = symbols.widened_homes() else {
symbols.end_home_attempt();
if result.is_err() {
*symbols = pristine;
}
return result;
};
if attempts > 3 * (symbols.unit_home_count() + symbols.site_home_count()) {
let unsettled = symbols
.unsettled_home()
.map_or_else(|| "a global home".to_string(), |key| key.to_string());
let err = symbols.name_structs(Error::Resource(format!(
"the type of {unsettled} did not settle"
)));
return Err(err);
let closures = symbols.closure_shapes();
let forced = symbols.forced_cells();
let refused = symbols.refused_seeds();
*symbols = pristine.clone();
symbols.begin_home_attempt(hints);
symbols.hint_closures(closures);
symbols.force_cells(forced);
symbols.unseed_cells(refused);
attempts += 1;
#[cfg(test)]
#[path = "settle_tests.rs"]
mod tests;