Show unified variables as a tree

This commit is contained in:
Ayaz Hafiz 2022-04-20 14:18:12 -04:00
parent 2cc8e95198
commit 21661275d8
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58

View file

@ -275,11 +275,24 @@ pub fn unify_pool(
}
#[cfg(debug_assertions)]
fn debug_print_unified_types(subs: &mut Subs, ctx: &Context, before_unified: bool) {
fn debug_print_unified_types(subs: &mut Subs, ctx: &Context, opt_outcome: Option<&Outcome>) {
static mut UNIFICATION_DEPTH: usize = 0;
if std::env::var("ROC_PRINT_UNIFICATIONS").is_ok() {
let time = if before_unified { "START" } else { "END" };
// if true, print the types that are unified.
//
let prefix = match opt_outcome {
None => "",
Some(outcome) if outcome.mismatches.is_empty() => "",
Some(_) => "",
};
let depth = unsafe { UNIFICATION_DEPTH };
let indent = 2;
let (use_depth, new_depth) = if opt_outcome.is_none() {
(depth, depth + indent)
} else {
(depth - indent, depth - indent)
};
// NOTE: names are generated here (when creating an error type) and that modifies names
// generated by pretty_print.rs. So many test will fail with changes in variable names when
// this block runs.
@ -294,8 +307,9 @@ fn debug_print_unified_types(subs: &mut Subs, ctx: &Context, before_unified: boo
let content_2 = subs.get(ctx.second).content;
let mode = if ctx.mode.is_eq() { "~" } else { "+=" };
eprintln!(
"{}({:?}-{:?}): {:?} {:?} {} {:?} {:?}",
time,
"{}{}({:?}-{:?}): {:?} {:?} {} {:?} {:?}",
" ".repeat(use_depth),
prefix,
ctx.first,
ctx.second,
ctx.first,
@ -304,12 +318,14 @@ fn debug_print_unified_types(subs: &mut Subs, ctx: &Context, before_unified: boo
ctx.second,
roc_types::subs::SubsFmtContent(&content_2, subs),
);
unsafe { UNIFICATION_DEPTH = new_depth };
}
}
fn unify_context(subs: &mut Subs, pool: &mut Pool, ctx: Context) -> Outcome {
#[cfg(debug_assertions)]
debug_print_unified_types(subs, &ctx, true);
debug_print_unified_types(subs, &ctx, None);
let result = match &ctx.first_desc.content {
FlexVar(opt_name) => unify_flex(subs, &ctx, opt_name, None, &ctx.second_desc.content),
@ -349,7 +365,7 @@ fn unify_context(subs: &mut Subs, pool: &mut Pool, ctx: Context) -> Outcome {
};
#[cfg(debug_assertions)]
debug_print_unified_types(subs, &ctx, false);
debug_print_unified_types(subs, &ctx, Some(&result));
result
}