mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Print recursion vars correctly
This commit is contained in:
parent
7306e131b9
commit
3497237c99
8 changed files with 79 additions and 57 deletions
|
@ -233,7 +233,11 @@ fn find_names_needed(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn name_all_type_vars(variable: Variable, subs: &mut Subs) {
|
||||
pub struct NamedResult {
|
||||
recursion_structs_to_expand: Vec<Variable>,
|
||||
}
|
||||
|
||||
pub fn name_all_type_vars(variable: Variable, subs: &mut Subs) -> NamedResult {
|
||||
let mut roots = Vec::new();
|
||||
let mut letters_used = 0;
|
||||
let mut appearances = MutMap::default();
|
||||
|
@ -242,13 +246,30 @@ pub fn name_all_type_vars(variable: Variable, subs: &mut Subs) {
|
|||
// Populate names_needed
|
||||
find_names_needed(variable, subs, &mut roots, &mut appearances, &mut taken);
|
||||
|
||||
let mut recursion_structs_to_expand = vec![];
|
||||
|
||||
for root in roots {
|
||||
// show the type variable number instead of `*`. useful for debugging
|
||||
// set_root_name(root, (format!("<{:?}>", root).into()), subs);
|
||||
if let Some(Appearances::Multiple) = appearances.get(&root) {
|
||||
letters_used = name_root(letters_used, root, subs, &mut taken);
|
||||
match appearances.get(&root) {
|
||||
Some(Appearances::Multiple) => {
|
||||
letters_used = name_root(letters_used, root, subs, &mut taken);
|
||||
}
|
||||
Some(Appearances::Single) => {
|
||||
if let Content::RecursionVar { structure, .. } =
|
||||
subs.get_content_without_compacting(root)
|
||||
{
|
||||
recursion_structs_to_expand.push(*structure);
|
||||
letters_used = name_root(letters_used, root, subs, &mut taken);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
NamedResult {
|
||||
recursion_structs_to_expand,
|
||||
}
|
||||
}
|
||||
|
||||
fn name_root(
|
||||
|
@ -312,6 +333,7 @@ fn set_root_name(root: Variable, name: Lowercase, subs: &mut Subs) {
|
|||
#[derive(Default)]
|
||||
struct Context<'a> {
|
||||
able_variables: Vec<(&'a str, Symbol)>,
|
||||
recursion_structs_to_expand: Vec<Variable>,
|
||||
}
|
||||
|
||||
pub fn content_to_string(
|
||||
|
@ -319,10 +341,14 @@ pub fn content_to_string(
|
|||
subs: &Subs,
|
||||
home: ModuleId,
|
||||
interns: &Interns,
|
||||
named_result: NamedResult,
|
||||
) -> String {
|
||||
let mut buf = String::new();
|
||||
let env = Env { home, interns };
|
||||
let mut ctx = Context::default();
|
||||
let mut ctx = Context {
|
||||
able_variables: vec![],
|
||||
recursion_structs_to_expand: named_result.recursion_structs_to_expand,
|
||||
};
|
||||
|
||||
write_content(&env, &mut ctx, content, subs, &mut buf, Parens::Unnecessary);
|
||||
|
||||
|
@ -381,12 +407,34 @@ fn write_content<'a>(
|
|||
ctx.able_variables.push((name, *ability));
|
||||
buf.push_str(name);
|
||||
}
|
||||
RecursionVar { opt_name, .. } => match opt_name {
|
||||
RecursionVar {
|
||||
opt_name,
|
||||
structure,
|
||||
} => match opt_name {
|
||||
Some(name_index) => {
|
||||
let name = &subs.field_names[name_index.index as usize];
|
||||
buf.push_str(name.as_str())
|
||||
if let Some(idx) = ctx
|
||||
.recursion_structs_to_expand
|
||||
.iter()
|
||||
.position(|v| v == structure)
|
||||
{
|
||||
ctx.recursion_structs_to_expand.swap_remove(idx);
|
||||
|
||||
write_content(
|
||||
env,
|
||||
ctx,
|
||||
subs.get_content_without_compacting(*structure),
|
||||
subs,
|
||||
buf,
|
||||
parens,
|
||||
);
|
||||
} else {
|
||||
let name = &subs.field_names[name_index.index as usize];
|
||||
buf.push_str(name.as_str())
|
||||
}
|
||||
}
|
||||
None => {
|
||||
unreachable!("This should always be filled in!")
|
||||
}
|
||||
None => buf.push_str(WILDCARD),
|
||||
},
|
||||
Structure(flat_type) => write_flat_type(env, ctx, flat_type, subs, buf, parens),
|
||||
Alias(symbol, args, _actual, _kind) => {
|
||||
|
|
|
@ -2172,23 +2172,6 @@ impl Content {
|
|||
Content::Structure(FlatType::Apply(Symbol::NUM_NUM, _))
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
#[allow(dead_code)]
|
||||
pub fn dbg(self, subs: &Subs) -> Self {
|
||||
let home = roc_module::symbol::ModuleIds::default().get_or_insert(&"#Dbg".into());
|
||||
let interns = roc_module::symbol::Interns {
|
||||
all_ident_ids: roc_module::symbol::IdentIds::exposed_builtins(0),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
eprintln!(
|
||||
"{}",
|
||||
crate::pretty_print::content_to_string(&self, subs, home, &interns)
|
||||
);
|
||||
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue