From 1286bbecb0d01f88e366bc563665a9e19423a08d Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 17 Jul 2024 15:16:53 -0500 Subject: [PATCH] Fix occurs check --- compiler/noirc_frontend/src/hir_def/types.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index 677915776e9..37cda2bd04d 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -519,7 +519,9 @@ impl TypeVariable { /// variable is already bound to a different type. This generally /// a logic error to use outside of monomorphization. pub fn force_bind(&self, typ: Type) { - *self.1.borrow_mut() = TypeBinding::Bound(typ); + if !typ.occurs(self.id()) { + *self.1.borrow_mut() = TypeBinding::Bound(typ); + } } } @@ -1924,9 +1926,11 @@ impl Type { generic_args.iter().any(|arg| arg.occurs(target_id)) } Type::Tuple(fields) => fields.iter().any(|field| field.occurs(target_id)), - Type::NamedGeneric(binding, _, _) | Type::TypeVariable(binding, _) => { - match &*binding.borrow() { - TypeBinding::Bound(binding) => binding.occurs(target_id), + Type::NamedGeneric(type_var, _, _) | Type::TypeVariable(type_var, _) => { + match &*type_var.borrow() { + TypeBinding::Bound(binding) => { + type_var.id() == target_id || binding.occurs(target_id) + } TypeBinding::Unbound(id) => *id == target_id, } }