From 735685dd86b83402e710676fc5fe3170bf6ec999 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Wed, 16 Nov 2022 10:02:05 -0600 Subject: [PATCH] Include error vars in is_recursion_var checks --- crates/compiler/types/src/subs.rs | 12 ++++++++++++ crates/compiler/unify/src/unify.rs | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/compiler/types/src/subs.rs b/crates/compiler/types/src/subs.rs index 4b32ff9ba4..922b9cbba3 100644 --- a/crates/compiler/types/src/subs.rs +++ b/crates/compiler/types/src/subs.rs @@ -2150,6 +2150,18 @@ impl Subs { pub fn dbg(&self, var: Variable) -> impl std::fmt::Debug + '_ { SubsFmtContent(self.get_content_without_compacting(var), self) } + + /// Is this variable involved in an error? + pub fn is_error_var(&self, var: Variable) -> bool { + match self.get_content_without_compacting(var) { + Content::Error => true, + Content::FlexVar(Some(index)) => { + // Generated names for errors start with `#` + self[*index].as_str().starts_with('#') + } + _ => false, + } + } } #[inline(always)] diff --git a/crates/compiler/unify/src/unify.rs b/crates/compiler/unify/src/unify.rs index f0284fade6..77c7f69d20 100644 --- a/crates/compiler/unify/src/unify.rs +++ b/crates/compiler/unify/src/unify.rs @@ -2807,7 +2807,7 @@ fn unify_shared_tags_merge_new( let flat_type = match recursion_var { Rec::None => FlatType::TagUnion(new_tags, new_ext_var), Rec::Left(rec) | Rec::Right(rec) | Rec::Both(rec, _) => { - debug_assert!(is_recursion_var(env.subs, rec)); + debug_assert!(is_recursion_var(env.subs, rec), "{:?}", env.subs.dbg(rec)); FlatType::RecursiveTagUnion(rec, new_tags, new_ext_var) } }; @@ -3494,7 +3494,9 @@ fn is_recursion_var(subs: &Subs, var: Variable) -> bool { matches!( subs.get_content_without_compacting(var), Content::RecursionVar { .. } - ) + ) || + // Error-like vars should always unify, so pretend they are recursion vars too. + subs.is_error_var(var) } #[allow(clippy::too_many_arguments)]