Skip to content

Commit

Permalink
fix: polymorphic type spec instantiation bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 13, 2024
1 parent e386749 commit 74e89f6
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 180 deletions.
25 changes: 25 additions & 0 deletions crates/erg_common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ where
Ok(v)
}

pub fn failable_map_mut<T, U, E, F, I>(i: I, mut f: F) -> Result<Vec<U>, (Vec<U>, Vec<E>)>
where
F: FnMut(T) -> Result<U, (U, E)>,
I: Iterator<Item = T>,
{
let mut v = vec![];
let mut errs = vec![];
for x in i {
match f(x) {
Ok(y) => {
v.push(y);
}
Err((y, e)) => {
v.push(y);
errs.push(e);
}
}
}
if errs.is_empty() {
Ok(v)
} else {
Err((v, errs))
}
}

pub fn unique_in_place<T: Eq + std::hash::Hash + Clone>(v: &mut Vec<T>) {
let mut uniques = Set::new();
v.retain(|e| uniques.insert(e.clone()));
Expand Down
1 change: 1 addition & 0 deletions crates/erg_common/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl<T: Hash + Eq> Set<T> {
self.elems.contains(value)
}

/// newly inserted: true, already present: false
#[inline]
pub fn insert(&mut self, value: T) -> bool {
self.elems.insert(value)
Expand Down
23 changes: 19 additions & 4 deletions crates/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,19 @@ impl Context {

fn eval_const_def(&mut self, def: &Def) -> EvalResult<ValueObj> {
if def.is_const() {
let mut errs = EvalErrors::empty();
let __name__ = def.sig.ident().unwrap().inspect();
let vis = self.instantiate_vis_modifier(def.sig.vis())?;
let tv_cache = match &def.sig {
Signature::Subr(subr) => {
let ty_cache =
self.instantiate_ty_bounds(&subr.bounds, RegistrationMode::Normal)?;
match self.instantiate_ty_bounds(&subr.bounds, RegistrationMode::Normal) {
Ok(ty_cache) => ty_cache,
Err((ty_cache, es)) => {
errs.extend(es);
ty_cache
}
};
Some(ty_cache)
}
Signature::Var(_) => None,
Expand All @@ -740,7 +747,8 @@ impl Context {
} else {
None
};
let (_ctx, errs) = self.check_decls_and_pop();
let (_ctx, es) = self.check_decls_and_pop();
errs.extend(es);
self.register_gen_const(
def.sig.ident().unwrap(),
obj,
Expand Down Expand Up @@ -917,10 +925,16 @@ impl Context {

/// FIXME: grow
fn eval_const_lambda(&self, lambda: &Lambda) -> EvalResult<ValueObj> {
let mut errs = EvalErrors::empty();
let mut tmp_tv_cache =
self.instantiate_ty_bounds(&lambda.sig.bounds, RegistrationMode::Normal)?;
match self.instantiate_ty_bounds(&lambda.sig.bounds, RegistrationMode::Normal) {
Ok(ty_cache) => ty_cache,
Err((ty_cache, es)) => {
errs.extend(es);
ty_cache
}
};
let mut non_default_params = Vec::with_capacity(lambda.sig.params.non_defaults.len());
let mut errs = EvalErrors::empty();
for sig in lambda.sig.params.non_defaults.iter() {
match self.instantiate_param_ty(
sig,
Expand Down Expand Up @@ -2090,6 +2104,7 @@ impl Context {
TyParam::ProjCall { obj, attr, args } => Ok(proj_call(*obj, attr, args)),
// TyParam::Erased(_t) => Ok(Type::Obj),
TyParam::Value(v) => self.convert_value_into_type(v).map_err(TyParam::Value),
TyParam::Erased(t) if t.is_type() => Ok(Type::Obj),
// TODO: Dict, Set
other => Err(other),
}
Expand Down
Loading

0 comments on commit 74e89f6

Please sign in to comment.