mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
fix: traits can have impls
This commit is contained in:
parent
cdabb2e546
commit
ebd7707f77
4 changed files with 42 additions and 1 deletions
|
@ -344,6 +344,7 @@ impl<K: Hash + Eq, V> Dict<K, V> {
|
|||
self.dict.extend(iter);
|
||||
}
|
||||
|
||||
/// If the key already exists, the value will not be updated.
|
||||
#[inline]
|
||||
pub fn guaranteed_extend<I: IntoIterator<Item = (K, V)>>(&mut self, other: I) {
|
||||
for (k, v) in other {
|
||||
|
|
|
@ -1381,6 +1381,14 @@ impl Context {
|
|||
.structuralize();
|
||||
bin_op(S, R.clone(), Bool).quantify()
|
||||
};
|
||||
let op_t2 = {
|
||||
let S = Type::from(
|
||||
dict! { Field::public(FUNDAMENTAL_ITER.into()) => fn0_met(Never, poly(ITERATOR, vec![ty_tp(R.clone())])) },
|
||||
)
|
||||
.structuralize();
|
||||
bin_op(S, R.clone(), Bool).quantify()
|
||||
};
|
||||
let op_t = (op_t & op_t2).with_default_intersec_index(0);
|
||||
self.register_builtin_erg_impl(
|
||||
FUNDAMENTAL_CONTAINS,
|
||||
op_t.clone(),
|
||||
|
|
|
@ -385,8 +385,14 @@ impl Context {
|
|||
let mut iterator = Self::builtin_poly_trait(ITERATOR, vec![PS::t_nd(TY_T)], 2);
|
||||
iterator.register_superclass(poly(ITERABLE, vec![ty_tp(T.clone())]), &iterable);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(ITERATOR, vec![ty_tp(T.clone())])));
|
||||
let t = fn0_met(Slf, or(T.clone(), NoneType)).quantify();
|
||||
let t = fn0_met(Slf.clone(), or(T.clone(), NoneType)).quantify();
|
||||
iterator.register_builtin_erg_decl(FUNDAMENTAL_NEXT, t, Visibility::BUILTIN_PUBLIC);
|
||||
iterator.register_builtin_erg_impl(
|
||||
FUNDAMENTAL_ITER,
|
||||
fn0_met(Slf.clone(), Slf.clone()).quantify(),
|
||||
Const,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
);
|
||||
/* Container */
|
||||
let mut container = Self::builtin_poly_trait(CONTAINER, vec![PS::t_nd(TY_T)], 2);
|
||||
let op_t = fn1_met(mono(CONTAINER), T.clone(), Bool).quantify();
|
||||
|
|
|
@ -1435,6 +1435,32 @@ impl Context {
|
|||
attrs.guaranteed_extend(sup_ctx.type_dir(namespace));
|
||||
}
|
||||
}
|
||||
for sup in self.super_traits.iter() {
|
||||
if let Some(sup_ctx) = namespace.get_nominal_type_ctx(sup) {
|
||||
if sup_ctx.name == self.name {
|
||||
continue;
|
||||
}
|
||||
attrs.guaranteed_extend(sup_ctx.type_impl_dir(namespace));
|
||||
}
|
||||
}
|
||||
attrs
|
||||
}
|
||||
|
||||
fn type_impl_dir<'t>(&'t self, namespace: &'t Context) -> Dict<&VarName, &VarInfo> {
|
||||
let mut attrs = self.locals.iter().collect::<Dict<_, _>>();
|
||||
attrs.guaranteed_extend(
|
||||
self.methods_list
|
||||
.iter()
|
||||
.flat_map(|ctx| ctx.type_impl_dir(namespace)),
|
||||
);
|
||||
for sup in self.super_classes.iter().chain(self.super_traits.iter()) {
|
||||
if let Some(sup_ctx) = namespace.get_nominal_type_ctx(sup) {
|
||||
if sup_ctx.name == self.name {
|
||||
continue;
|
||||
}
|
||||
attrs.guaranteed_extend(sup_ctx.type_impl_dir(namespace));
|
||||
}
|
||||
}
|
||||
attrs
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue