From 21661275d8818e7f58afdfdf885a0a2034f09659 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Wed, 20 Apr 2022 14:18:12 -0400 Subject: [PATCH] Show unified variables as a tree --- compiler/unify/src/unify.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/compiler/unify/src/unify.rs b/compiler/unify/src/unify.rs index cebebcca4f..a8ceea4cd6 100644 --- a/compiler/unify/src/unify.rs +++ b/compiler/unify/src/unify.rs @@ -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 }