Skip to content

Commit

Permalink
fix: collection bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Oct 3, 2024
1 parent 80eae76 commit f862a3f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
6 changes: 5 additions & 1 deletion crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4191,7 +4191,11 @@ impl Context {
.unwrap_or(&Type::Obj);
if self.related(base_def_t, assert_def_t) {
// FIXME: Vec(_), List(Int, 2) -> Vec(2)
let casted = poly(base.qual_name(), guard.to.typarams());
let casted = if base.is_polymorphic() {
poly(base.qual_name(), guard.to.typarams())
} else {
*guard.to.clone()
};
Ok(casted)
} else {
Err(TyCheckErrors::from(TyCheckError::invalid_type_cast_error(
Expand Down
12 changes: 11 additions & 1 deletion crates/erg_compiler/context/instantiate_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2914,7 +2914,17 @@ impl Context {
let err = TyCheckError::new(err.into(), self.cfg.input.clone(), self.caused_by());
(Type::Failure, TyCheckErrors::from(err))
})?;
self.instantiate_typespec(&t_spec)
match self.instantiate_typespec(&t_spec)? {
// FIXME:
Type::Mono(name) => match &name[..] {
"GenericList" => Ok(unknown_len_list_t(Obj)),
"GenericTuple" => Ok(homo_tuple_t(Obj)),
"GenericDict" => Ok(dict! { Obj => Obj }.into()),
"GenericSet" => Ok(unknown_len_set_t(Obj)),
_ => Ok(mono(name)),
},
other => Ok(other),
}
}

pub(crate) fn expr_to_value(&self, expr: ast::Expr) -> Failable<ValueObj> {
Expand Down
7 changes: 6 additions & 1 deletion crates/erg_compiler/context/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,12 @@ impl<'c, 'l, 'u, L: Locational> Unifier<'c, 'l, 'u, L> {
return Ok(());
}
let sub = mem::take(&mut sub);
let new_sup = self.ctx.intersection(&sup, maybe_sup);
// min(L, R) != L and R
let new_sup = if let Some(new_sup) = self.ctx.min(&sup, maybe_sup).either() {
new_sup.clone()
} else {
self.ctx.intersection(&sup, maybe_sup)
};
self.sub_unify(&sub, &new_sup)?;
// ?T(:> Int, <: Int) ==> ?T == Int
// ?T(:> List(Int, 3), <: List(?T, ?N)) ==> ?T == List(Int, 3)
Expand Down
8 changes: 8 additions & 0 deletions crates/erg_compiler/ty/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ pub fn tuple_t(args: Vec<Type>) -> Type {
)
}

pub fn homo_tuple_t(t: Type) -> Type {
poly("HomogenousTuple", vec![TyParam::t(t)])
}

pub fn set_t(elem_t: Type, len: TyParam) -> Type {
poly("Set", vec![TyParam::t(elem_t), len])
}

pub fn unknown_len_set_t(elem_t: Type) -> Type {
set_t(elem_t, TyParam::erased(Type::Nat))
}

pub fn set_mut(elem_t: Type, len: TyParam) -> Type {
poly("Set!", vec![TyParam::t(elem_t), len])
}
Expand Down
4 changes: 4 additions & 0 deletions tests/should_ok/dict.er
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ assert dic.diff({ "a": 1 }) == { "b": 2 }
assert dic.get("a"+"b", 3) == 3
rec = dic.as_record()
assert rec.a == 1 and rec.b == 2

gdic as {Obj: Obj} = dic
assert "a" in gdic
assert not 1 in gdic
4 changes: 4 additions & 0 deletions tests/should_ok/list.er
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ l3 = [1, 2, 3, 4]
assert l3.from(2) == [3, 4]
assert l3.from(2).from(1) == [4]
assert l3.from(5) == []

l4 as List Obj = l3
assert 1 in l4
assert not "a" in l4

0 comments on commit f862a3f

Please sign in to comment.