fix: trait impl check for declared classes

This commit is contained in:
Shunsuke Shibayama 2024-04-26 01:18:25 +09:00
parent 22c2016eae
commit 44b4b8d82a
4 changed files with 24 additions and 6 deletions

View file

@ -1129,15 +1129,23 @@ impl Context {
} }
pub(crate) fn register_methods(&mut self, t: &Type, ctx: &Self) { pub(crate) fn register_methods(&mut self, t: &Type, ctx: &Self) {
let declared_in = NormalizedPathBuf::from(self.module_path());
let declared_in = declared_in.exists().then_some(declared_in);
for impl_trait in ctx.super_traits.iter() { for impl_trait in ctx.super_traits.iter() {
let declared_in = NormalizedPathBuf::from(self.module_path());
let declared_in = declared_in.exists().then_some(declared_in);
if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) { if let Some(mut impls) = self.trait_impls().get_mut(&impl_trait.qual_name()) {
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone(), declared_in)); impls.insert(TraitImpl::new(
t.clone(),
impl_trait.clone(),
declared_in.clone(),
));
} else { } else {
self.trait_impls().register( self.trait_impls().register(
impl_trait.qual_name(), impl_trait.qual_name(),
set![TraitImpl::new(t.clone(), impl_trait.clone(), declared_in)], set![TraitImpl::new(
t.clone(),
impl_trait.clone(),
declared_in.clone()
)],
); );
} }
} }

View file

@ -1031,7 +1031,7 @@ impl Context {
} }
} }
fn register_trait_impl( pub(crate) fn register_trait_impl(
&mut self, &mut self,
class: &Type, class: &Type,
trait_: &Type, trait_: &Type,
@ -1525,6 +1525,7 @@ impl Context {
} }
/// Register that a class implements a trait and its super-traits. /// Register that a class implements a trait and its super-traits.
/// This does not register the class-trait relationship to `shared.trait_impls` (use `register_trait_impl`).
pub(crate) fn register_trait(&mut self, ctx: &Self, trait_: Type) -> CompileResult<()> { pub(crate) fn register_trait(&mut self, ctx: &Self, trait_: Type) -> CompileResult<()> {
let trait_ctx = ctx.get_nominal_type_ctx(&trait_).ok_or_else(|| { let trait_ctx = ctx.get_nominal_type_ctx(&trait_).ok_or_else(|| {
CompileError::type_not_found( CompileError::type_not_found(

View file

@ -1037,7 +1037,9 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
let res = if self.module.context.is_class(sup) { let res = if self.module.context.is_class(sup) {
tmp.register_base_class(&self.module.context, sup.clone()) tmp.register_base_class(&self.module.context, sup.clone())
} else { } else {
tmp.register_trait(&self.module.context, sup.clone()) let class = tmp.typ.clone();
let imp = self.module.context.register_trait_impl(&class, sup, ident);
imp.and(tmp.register_trait(&self.module.context, sup.clone()))
}; };
res.map_err(|err| { res.map_err(|err| {
let ctx = self.module.context.rec_get_mut_type(&name).unwrap(); let ctx = self.module.context.rec_get_mut_type(&name).unwrap();

View file

@ -27,3 +27,10 @@ _ = real_getter(1) # OK
ab_getter = op.itemgetter("a", "b") ab_getter = op.itemgetter("a", "b")
assert ab_getter({"a": 1, "b": 2}) == [1, 2] assert ab_getter({"a": 1, "b": 2}) == [1, 2]
ip = pyimport "ipaddress"
a = ip.IPv4Address "192.168.0.0"
_ = a == a
_ = a <= a
_ = a + 1 + 1