mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-03 05:54:33 +00:00
Refactor
This commit is contained in:
parent
e382aca7fc
commit
31045a338f
5 changed files with 43 additions and 45 deletions
|
@ -1006,7 +1006,7 @@ impl Context {
|
||||||
if lhs != coerced {
|
if lhs != coerced {
|
||||||
let proj = proj(coerced, rhs);
|
let proj = proj(coerced, rhs);
|
||||||
self.eval_t_params(proj, level, t_loc).map(|t| {
|
self.eval_t_params(proj, level, t_loc).map(|t| {
|
||||||
self.coerce(&lhs);
|
lhs.coerce();
|
||||||
t
|
t
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -1119,9 +1119,9 @@ impl Context {
|
||||||
let mut tv_cache = TyVarCache::new(self.level, self);
|
let mut tv_cache = TyVarCache::new(self.level, self);
|
||||||
let t = self.detach(t, &mut tv_cache);
|
let t = self.detach(t, &mut tv_cache);
|
||||||
// Int -> T, 2 -> M, 4 -> N
|
// Int -> T, 2 -> M, 4 -> N
|
||||||
self.undo_substitute_typarams(quant_sub);
|
Self::undo_substitute_typarams(quant_sub);
|
||||||
if let Some(quant_sup) = methods.impl_of() {
|
if let Some(quant_sup) = methods.impl_of() {
|
||||||
self.undo_substitute_typarams(&quant_sup);
|
Self::undo_substitute_typarams(&quant_sup);
|
||||||
}
|
}
|
||||||
return Some(t);
|
return Some(t);
|
||||||
}
|
}
|
||||||
|
@ -1179,7 +1179,6 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::only_used_in_recursion)]
|
|
||||||
/// e.g. qt: Array(T, N), st: Array(Int, 3)
|
/// e.g. qt: Array(T, N), st: Array(Int, 3)
|
||||||
pub(crate) fn substitute_typarams(&self, qt: &Type, st: &Type) {
|
pub(crate) fn substitute_typarams(&self, qt: &Type, st: &Type) {
|
||||||
let qtps = qt.typarams();
|
let qtps = qt.typarams();
|
||||||
|
@ -1220,8 +1219,7 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::only_used_in_recursion)]
|
pub(crate) fn undo_substitute_typarams(substituted: &Type) {
|
||||||
pub(crate) fn undo_substitute_typarams(&self, substituted: &Type) {
|
|
||||||
for tp in substituted.typarams().into_iter() {
|
for tp in substituted.typarams().into_iter() {
|
||||||
match tp {
|
match tp {
|
||||||
TyParam::FreeVar(fv) if fv.is_undoable_linked() => fv.undo(),
|
TyParam::FreeVar(fv) if fv.is_undoable_linked() => fv.undo(),
|
||||||
|
@ -1232,7 +1230,7 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TyParam::Type(t) => {
|
TyParam::Type(t) => {
|
||||||
self.undo_substitute_typarams(&t);
|
Self::undo_substitute_typarams(&t);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -1333,7 +1331,7 @@ impl Context {
|
||||||
if lhs != coerced {
|
if lhs != coerced {
|
||||||
let proj = proj_call(coerced, attr_name, args);
|
let proj = proj_call(coerced, attr_name, args);
|
||||||
self.eval_t_params(proj, level, t_loc).map(|t| {
|
self.eval_t_params(proj, level, t_loc).map(|t| {
|
||||||
self.coerce_tp(&lhs);
|
lhs.coerce();
|
||||||
t
|
t
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -548,7 +548,7 @@ impl Context {
|
||||||
})? {
|
})? {
|
||||||
match ctx.rec_get_var_info(ident, AccessKind::Attr, input, namespace) {
|
match ctx.rec_get_var_info(ident, AccessKind::Attr, input, namespace) {
|
||||||
Ok(t) => {
|
Ok(t) => {
|
||||||
self.coerce(obj.ref_t());
|
obj.ref_t().coerce();
|
||||||
return Ok(t);
|
return Ok(t);
|
||||||
}
|
}
|
||||||
Err(e) if e.core.kind == ErrorKind::NameError => {}
|
Err(e) if e.core.kind == ErrorKind::NameError => {}
|
||||||
|
|
|
@ -665,40 +665,6 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::only_used_in_recursion)]
|
|
||||||
/// Fix type variables at their lower bound
|
|
||||||
/// ```erg
|
|
||||||
/// i: ?T(:> Int)
|
|
||||||
/// assert i.Real == 1
|
|
||||||
/// i: (Int)
|
|
||||||
/// ```
|
|
||||||
pub(crate) fn coerce(&self, t: &Type) {
|
|
||||||
match t {
|
|
||||||
Type::FreeVar(fv) if fv.is_linked() => {
|
|
||||||
self.coerce(&fv.crack());
|
|
||||||
}
|
|
||||||
Type::FreeVar(fv) if fv.is_unbound() => {
|
|
||||||
let (sub, _sup) = fv.get_subsup().unwrap();
|
|
||||||
fv.link(&sub);
|
|
||||||
}
|
|
||||||
Type::And(l, r) | Type::Or(l, r) | Type::Not(l, r) => {
|
|
||||||
self.coerce(l);
|
|
||||||
self.coerce(r);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn coerce_tp(&self, tp: &TyParam) {
|
|
||||||
match tp {
|
|
||||||
TyParam::FreeVar(fv) if fv.is_linked() => {
|
|
||||||
self.coerce_tp(&fv.crack());
|
|
||||||
}
|
|
||||||
TyParam::Type(t) => self.coerce(t),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if all types are resolvable (if traits, check if an implementation exists)
|
/// Check if all types are resolvable (if traits, check if an implementation exists)
|
||||||
/// And replace them if resolvable
|
/// And replace them if resolvable
|
||||||
pub(crate) fn resolve(
|
pub(crate) fn resolve(
|
||||||
|
@ -1667,6 +1633,7 @@ impl Context {
|
||||||
) => {
|
) => {
|
||||||
// e.g. Set(?T) <: Eq(Set(?T))
|
// e.g. Set(?T) <: Eq(Set(?T))
|
||||||
// Array(Str) <: Iterable(Str)
|
// Array(Str) <: Iterable(Str)
|
||||||
|
// Zip(T, U) <: Iterable(Tuple([T, U]))
|
||||||
if ln != rn {
|
if ln != rn {
|
||||||
if let Some((sub_def_t, sub_ctx)) = self.get_nominal_type_ctx(maybe_sub) {
|
if let Some((sub_def_t, sub_ctx)) = self.get_nominal_type_ctx(maybe_sub) {
|
||||||
self.substitute_typarams(sub_def_t, maybe_sub);
|
self.substitute_typarams(sub_def_t, maybe_sub);
|
||||||
|
@ -1677,11 +1644,11 @@ impl Context {
|
||||||
{
|
{
|
||||||
self.sub_unify_tp(l_maybe_sub, r_maybe_sup, None, loc, false)
|
self.sub_unify_tp(l_maybe_sub, r_maybe_sup, None, loc, false)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
self.undo_substitute_typarams(sub_def_t);
|
Self::undo_substitute_typarams(sub_def_t);
|
||||||
e
|
e
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
self.undo_substitute_typarams(sub_def_t);
|
Self::undo_substitute_typarams(sub_def_t);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1908,6 +1908,29 @@ impl Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fix type variables at their lower bound
|
||||||
|
/// ```erg
|
||||||
|
/// i: ?T(:> Int)
|
||||||
|
/// assert i.Real == 1
|
||||||
|
/// i: (Int)
|
||||||
|
/// ```
|
||||||
|
pub fn coerce(&self) {
|
||||||
|
match self {
|
||||||
|
Type::FreeVar(fv) if fv.is_linked() => {
|
||||||
|
Self::coerce(&fv.crack());
|
||||||
|
}
|
||||||
|
Type::FreeVar(fv) if fv.is_unbound() => {
|
||||||
|
let (sub, _sup) = fv.get_subsup().unwrap();
|
||||||
|
fv.link(&sub);
|
||||||
|
}
|
||||||
|
Type::And(l, r) | Type::Or(l, r) | Type::Not(l, r) => {
|
||||||
|
Self::coerce(l);
|
||||||
|
Self::coerce(r);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn qvars(&self) -> Set<(Str, Constraint)> {
|
pub fn qvars(&self) -> Set<(Str, Constraint)> {
|
||||||
match self {
|
match self {
|
||||||
Self::FreeVar(fv) if fv.is_linked() => fv.forced_as_ref().linked().unwrap().qvars(),
|
Self::FreeVar(fv) if fv.is_linked() => fv.forced_as_ref().linked().unwrap().qvars(),
|
||||||
|
|
|
@ -705,6 +705,16 @@ impl TyParam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn coerce(&self) {
|
||||||
|
match self {
|
||||||
|
TyParam::FreeVar(fv) if fv.is_linked() => {
|
||||||
|
fv.crack().coerce();
|
||||||
|
}
|
||||||
|
TyParam::Type(t) => t.coerce(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn qvars(&self) -> Set<(Str, Constraint)> {
|
pub fn qvars(&self) -> Set<(Str, Constraint)> {
|
||||||
match self {
|
match self {
|
||||||
Self::FreeVar(fv) if !fv.constraint_is_uninited() => {
|
Self::FreeVar(fv) if !fv.constraint_is_uninited() => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue