fix: infinite recursion bug

This commit is contained in:
Shunsuke Shibayama 2024-12-07 21:40:58 +09:00
parent 12ff4168c3
commit 43fbb7dbfd
2 changed files with 57 additions and 33 deletions

View file

@ -1640,6 +1640,8 @@ impl<L: Locational> Unifier<'_, '_, '_, L> {
{
// contravariant
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else if let Some(sub_pt) = sub_subr.kw_var_params.as_ref() {
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else {
let param_name = super_pt.name().map_or("_", |s| &s[..]);
let similar_param = erg_common::levenshtein::get_similar_name(
@ -1692,6 +1694,8 @@ impl<L: Locational> Unifier<'_, '_, '_, L> {
}
// contravariant
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else if let Some(sub_pt) = sub_subr.kw_var_params.as_ref() {
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else {
todo!("{maybe_sub} <: {maybe_super}")
}
@ -1728,6 +1732,8 @@ impl<L: Locational> Unifier<'_, '_, '_, L> {
continue;
}
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else if let Some(sub_pt) = sub_subr.kw_var_params.as_ref() {
self.sub_unify(super_pt.typ(), sub_pt.typ())?;
} else {
todo!("{maybe_sub} <: {maybe_super}")
}
@ -1943,6 +1949,13 @@ impl<L: Locational> Unifier<'_, '_, '_, L> {
/// TODO: Current implementation is inefficient because coercion is performed twice with `subtype_of` in `sub_unify`
fn nominal_sub_unify(&self, maybe_sub: &Type, maybe_super: &Type) -> TyCheckResult<()> {
debug_assert_ne!(maybe_sub.qual_name(), maybe_super.qual_name());
if (maybe_sub.is_dict() || maybe_sub.is_dict_mut())
&& (maybe_super.is_dict() || maybe_super.is_dict_mut())
{
let sub_dict = maybe_sub.typarams().into_iter().next().unwrap();
let super_dict = maybe_super.typarams().into_iter().next().unwrap();
return self.sub_unify_tp(&sub_dict, &super_dict, None, false);
}
if let Some(sub_ctx) = self.ctx.get_nominal_type_ctx(maybe_sub) {
let sub_def_t = &sub_ctx.typ;
// e.g.