fix: method subtyping

This commit is contained in:
Shunsuke Shibayama 2024-08-21 11:28:17 +09:00
parent af360a52cc
commit bc50ceeb6a
2 changed files with 32 additions and 1 deletions

View file

@ -386,6 +386,7 @@ impl Context {
// (Int, n := Int, m := Int) -> Int <: (Int, Int) -> Int
// (Int, n := Int) -> Int <!: (Int, Int, Int) -> Int
// (*Int) -> Int <: (Int, Int) -> Int
// (self: Self, T) -> U <: T -> U
let len_judge = ls.non_default_params.len()
<= rs.non_default_params.len() + rs.default_params.len()
|| rs.var_params.is_some();
@ -401,9 +402,21 @@ impl Context {
.zip(repeat(r_var))
.all(|(l, r)| self.subtype_of(l.typ(), r.typ()))
} else {
let rs_params = if !ls.is_method() && rs.is_method() {
rs.non_default_params
.iter()
.skip(1)
.chain(&rs.default_params)
} else {
#[allow(clippy::iter_skip_zero)]
rs.non_default_params
.iter()
.skip(0)
.chain(&rs.default_params)
};
ls.non_default_params
.iter()
.zip(rs.non_default_params.iter().chain(rs.default_params.iter()))
.zip(rs_params)
.all(|(l, r)| self.subtype_of(l.typ(), r.typ()))
};
let var_params_judge = ls

View file

@ -1273,6 +1273,8 @@ impl Context {
) -> SingleTyCheckResult<VarInfo> {
// search_method_info(?T, aaa, pos_args: [1, 2]) == None
// => ?T(<: Structural({ .aaa = (self: ?T, ?U, ?V) -> ?W }))
// search_method_info(?T, aaa, kw_args: [b:=1, c:=2]) == None
// => ?T(<: Structural({ .aaa = (self: ?T, b: ?U, c: ?V) -> ?W }))
if PYTHON_MODE
&& obj
.var_info()
@ -1554,6 +1556,22 @@ impl Context {
}
_ => {}
}
if PYTHON_MODE {
if let Some(subr_t) = self.get_union_attr_type_by_name(attr_name) {
let muty = Mutability::from(&attr_name.inspect()[..]);
let vi = VarInfo::new(
subr_t,
muty,
Visibility::DUMMY_PUBLIC,
VarKind::Builtin,
None,
ContextKind::Dummy,
None,
AbsLocation::unknown(),
);
return Ok(vi);
}
}
for patch in self.find_patches_of(obj.ref_t()) {
if let Some(vi) = patch.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;