Skip to content

Commit

Permalink
fix: don't pass Ref as arg to recursive calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Oct 4, 2024
1 parent 841826d commit 3066781
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
26 changes: 14 additions & 12 deletions crates/erg_compiler/context/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ impl Context {
}
}
if lfv.is_linked() {
self.supertype_of(&lfv.crack(), rhs)
self.supertype_of(&lfv.unwrap_linked(), rhs)
} else if rfv.is_linked() {
self.supertype_of(lhs, &rfv.crack())
self.supertype_of(lhs, &rfv.unwrap_linked())
} else {
false
}
Expand Down Expand Up @@ -912,7 +912,7 @@ impl Context {

pub fn fields(&self, t: &Type) -> Dict<Field, Type> {
match t {
Type::FreeVar(fv) if fv.is_linked() => self.fields(&fv.crack()),
Type::FreeVar(fv) if fv.is_linked() => self.fields(&fv.unwrap_linked()),
Type::Record(fields) => fields.clone(),
Type::NamedTuple(fields) => fields.iter().cloned().collect(),
Type::Refinement(refine) => self.fields(&refine.t),
Expand Down Expand Up @@ -1000,10 +1000,10 @@ impl Context {
}
match (sup_p, sub_p) {
(TyParam::FreeVar(fv), _) if fv.is_linked() => {
self.supertype_of_tp(&fv.crack(), sub_p, variance)
self.supertype_of_tp(&fv.unwrap_linked(), sub_p, variance)
}
(_, TyParam::FreeVar(fv)) if fv.is_linked() => {
self.supertype_of_tp(sup_p, &fv.crack(), variance)
self.supertype_of_tp(sup_p, &fv.unwrap_linked(), variance)
}
(TyParam::Erased(t), _) => match variance {
Variance::Contravariant => {
Expand Down Expand Up @@ -1328,10 +1328,10 @@ impl Context {
} else { Some(Any) }
},
(TyParam::FreeVar(fv), p) if fv.is_linked() => {
self.try_cmp(&fv.crack(), p)
self.try_cmp(&fv.unwrap_linked(), p)
}
(p, TyParam::FreeVar(fv)) if fv.is_linked() => {
self.try_cmp(p, &fv.crack())
self.try_cmp(p, &fv.unwrap_linked())
}
(
l @ (TyParam::FreeVar(_) | TyParam::Erased(_)),
Expand Down Expand Up @@ -1470,7 +1470,7 @@ impl Context {
}
match (lhs, rhs) {
(FreeVar(fv), other) | (other, FreeVar(fv)) if fv.is_linked() => {
self.union(&fv.crack(), other)
self.union(&fv.unwrap_linked(), other)
}
(Refinement(l), Refinement(r)) => Type::Refinement(self.union_refinement(l, r)),
(Refinement(refine), other) | (other, Refinement(refine))
Expand Down Expand Up @@ -1785,7 +1785,7 @@ impl Context {
}
match (lhs, rhs) {
(FreeVar(fv), other) | (other, FreeVar(fv)) if fv.is_linked() => {
self.intersection(&fv.crack(), other)
self.intersection(&fv.unwrap_linked(), other)
}
(Refinement(l), Refinement(r)) => Type::Refinement(self.intersection_refinement(l, r)),
(Structural(l), Structural(r)) => self.intersection(l, r).structuralize(),
Expand Down Expand Up @@ -1816,7 +1816,9 @@ impl Context {
// {i = Int; j = Int} and not {i = Int} == {j = Int}
// not {i = Int} and {i = Int; j = Int} == {j = Int}
(other @ Record(rec), Not(t)) | (Not(t), other @ Record(rec)) => match t.as_ref() {
Type::FreeVar(fv) => self.intersection(&fv.crack(), other),
Type::FreeVar(fv) if fv.is_linked() => {
self.intersection(&fv.unwrap_linked(), other)
}
Type::Record(rec2) => Type::Record(rec.clone().diff(rec2)),
_ => Type::Never,
},
Expand Down Expand Up @@ -2182,7 +2184,7 @@ impl Context {
#[allow(clippy::only_used_in_recursion)]
pub(crate) fn complement(&self, ty: &Type) -> Type {
match ty {
FreeVar(fv) if fv.is_linked() => self.complement(&fv.crack()),
FreeVar(fv) if fv.is_linked() => self.complement(&fv.unwrap_linked()),
Not(t) => *t.clone(),
Refinement(r) => Type::Refinement(r.clone().invert()),
Guard(guard) => Type::Guard(GuardType::new(
Expand Down Expand Up @@ -2211,7 +2213,7 @@ impl Context {
_ => {}
}
match lhs {
Type::FreeVar(fv) if fv.is_linked() => self.diff(&fv.crack(), rhs),
Type::FreeVar(fv) if fv.is_linked() => self.diff(&fv.unwrap_linked(), rhs),
// Type::And(l, r) => self.intersection(&self.diff(l, rhs), &self.diff(r, rhs)),
Type::Or(tys) => {
let mut new_tys = vec![];
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3827,7 +3827,7 @@ impl Context {
match p {
TyParam::Value(v) => Ok(v_enum(set![v])),
TyParam::Erased(t) => Ok((*t).clone()),
TyParam::FreeVar(fv) if fv.is_linked() => self.get_tp_t(&fv.crack()),
TyParam::FreeVar(fv) if fv.is_linked() => self.get_tp_t(&fv.unwrap_linked()),
TyParam::FreeVar(fv) => {
if let Some(t) = fv.get_type() {
Ok(t)
Expand Down
16 changes: 9 additions & 7 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ impl Context {
// (obj: Failure).foo: Failure
Type::Failure => Triple::Ok(VarInfo::ILLEGAL),
Type::FreeVar(fv) if fv.is_linked() => {
self.get_attr_info_from_attributive(&fv.crack(), ident, namespace)
self.get_attr_info_from_attributive(&fv.unwrap_linked(), ident, namespace)
}
Type::FreeVar(fv) if fv.get_super().is_some() => {
let sup = fv.get_super().unwrap();
Expand Down Expand Up @@ -1890,7 +1890,7 @@ impl Context {
Type::FreeVar(fv) if fv.is_linked() => self.substitute_call(
obj,
attr_name,
&fv.crack(),
&fv.unwrap_linked(),
pos_args,
kw_args,
(var_args, kw_var_args),
Expand Down Expand Up @@ -3063,7 +3063,9 @@ impl Context {
/// ```
pub fn get_nominal_super_type_ctxs<'a>(&'a self, t: &Type) -> Option<Vec<&'a TypeContext>> {
match t {
Type::FreeVar(fv) if fv.is_linked() => self.get_nominal_super_type_ctxs(&fv.crack()),
Type::FreeVar(fv) if fv.is_linked() => {
self.get_nominal_super_type_ctxs(&fv.unwrap_linked())
}
Type::FreeVar(fv) => {
if let Some(sup) = fv.get_super() {
self.get_nominal_super_type_ctxs(&sup)
Expand Down Expand Up @@ -3173,7 +3175,7 @@ impl Context {
pub(crate) fn get_nominal_type_ctx<'a>(&'a self, typ: &Type) -> Option<&'a TypeContext> {
match typ {
Type::FreeVar(fv) if fv.is_linked() => {
if let Some(res) = self.get_nominal_type_ctx(&fv.crack()) {
if let Some(res) = self.get_nominal_type_ctx(&fv.unwrap_linked()) {
return Some(res);
}
}
Expand Down Expand Up @@ -3351,7 +3353,7 @@ impl Context {
) -> Option<&'a mut TypeContext> {
match typ {
Type::FreeVar(fv) if fv.is_linked() => {
if let Some(res) = self.get_mut_nominal_type_ctx(&fv.crack()) {
if let Some(res) = self.get_mut_nominal_type_ctx(&fv.unwrap_linked()) {
return Some(res);
}
}
Expand Down Expand Up @@ -4022,7 +4024,7 @@ impl Context {
match typ {
Type::And(_, _) => false,
Type::Never => true,
Type::FreeVar(fv) if fv.is_linked() => self.is_class(&fv.crack()),
Type::FreeVar(fv) if fv.is_linked() => self.is_class(&fv.unwrap_linked()),
Type::FreeVar(_) => false,
Type::Or(tys) => tys.iter().all(|t| self.is_class(t)),
Type::Proj { lhs, rhs } => self
Expand All @@ -4045,7 +4047,7 @@ impl Context {
pub fn is_trait(&self, typ: &Type) -> bool {
match typ {
Type::Never => false,
Type::FreeVar(fv) if fv.is_linked() => self.is_trait(&fv.crack()),
Type::FreeVar(fv) if fv.is_linked() => self.is_trait(&fv.unwrap_linked()),
Type::FreeVar(_) => false,
Type::And(tys, _) => tys.iter().any(|t| self.is_trait(t)),
Type::Or(tys) => tys.iter().all(|t| self.is_trait(t)),
Expand Down
12 changes: 6 additions & 6 deletions crates/erg_compiler/context/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl<'c, 'l, 'u, L: Locational> Unifier<'c, 'l, 'u, L> {
}
}
match (maybe_sub, maybe_sup) {
(FreeVar(fv), _) if fv.is_linked() => self.occur(&fv.crack(), maybe_sup),
(_, FreeVar(fv)) if fv.is_linked() => self.occur(maybe_sub, &fv.crack()),
(FreeVar(fv), _) if fv.is_linked() => self.occur(&fv.unwrap_linked(), maybe_sup),
(_, FreeVar(fv)) if fv.is_linked() => self.occur(maybe_sub, &fv.unwrap_linked()),
(Subr(subr), FreeVar(fv)) if fv.is_unbound() => {
for default_t in subr.default_params.iter().map(|pt| pt.typ()) {
self.occur_inner(default_t, maybe_sup)?;
Expand Down Expand Up @@ -229,8 +229,8 @@ impl<'c, 'l, 'u, L: Locational> Unifier<'c, 'l, 'u, L> {

fn occur_inner(&self, maybe_sub: &Type, maybe_sup: &Type) -> TyCheckResult<()> {
match (maybe_sub, maybe_sup) {
(FreeVar(fv), _) if fv.is_linked() => self.occur_inner(&fv.crack(), maybe_sup),
(_, FreeVar(fv)) if fv.is_linked() => self.occur_inner(maybe_sub, &fv.crack()),
(FreeVar(fv), _) if fv.is_linked() => self.occur_inner(&fv.unwrap_linked(), maybe_sup),
(_, FreeVar(fv)) if fv.is_linked() => self.occur_inner(maybe_sub, &fv.unwrap_linked()),
(FreeVar(sub), FreeVar(sup)) => {
if sub.addr_eq(sup) {
Err(TyCheckErrors::from(TyCheckError::subtyping_error(
Expand Down Expand Up @@ -2054,8 +2054,8 @@ impl<'c, 'l, 'u, L: Locational> Unifier<'c, 'l, 'u, L> {
return None;
}
}
(FreeVar(fv), _) if fv.is_linked() => return self.unify(&fv.crack(), rhs),
(_, FreeVar(fv)) if fv.is_linked() => return self.unify(lhs, &fv.crack()),
(FreeVar(fv), _) if fv.is_linked() => return self.unify(&fv.unwrap_linked(), rhs),
(_, FreeVar(fv)) if fv.is_linked() => return self.unify(lhs, &fv.unwrap_linked()),
// TODO: unify(?T, ?U) ?
(FreeVar(_), FreeVar(_)) => {}
(FreeVar(fv), _) if fv.constraint_is_sandwiched() => {
Expand Down

0 comments on commit 3066781

Please sign in to comment.