Skip to content

Commit ed8125a

Browse files
committed
fix: parameter type inferring bug
fix: mtshiba/pylyzer#47
1 parent 60f82ba commit ed8125a

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

crates/erg_compiler/context/inquire.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ impl Context {
552552
) -> Triple<VarInfo, TyCheckError> {
553553
// get_attr_info(?T, aaa) == None
554554
// => ?T(<: Structural({ .aaa = ?U }))
555-
if self.in_subr() && PYTHON_MODE {
555+
if PYTHON_MODE && obj.var_info().is_some_and(|vi| vi.is_untyped_parameter()) {
556556
let t = free_var(self.level, Constraint::new_type_of(Type));
557557
if let Some(fv) = obj.ref_t().as_free() {
558558
if fv.get_sub().is_some() {
@@ -894,7 +894,7 @@ impl Context {
894894
) -> SingleTyCheckResult<VarInfo> {
895895
// search_method_info(?T, aaa, pos_args: [1, 2]) == None
896896
// => ?T(<: Structural({ .aaa = (self: ?T, ?U, ?V) -> ?W }))
897-
if PYTHON_MODE && self.in_subr() {
897+
if PYTHON_MODE && obj.var_info().is_some_and(|vi| vi.is_untyped_parameter()) {
898898
let nd_params = pos_args
899899
.iter()
900900
.map(|_| ParamTy::Pos(free_var(self.level, Constraint::new_type_of(Type))))
@@ -2559,10 +2559,6 @@ impl Context {
25592559
}
25602560
}
25612561

2562-
pub(crate) fn in_subr(&self) -> bool {
2563-
self.kind.is_subr() || self.get_outer().map_or(false, |ctx| ctx.in_subr())
2564-
}
2565-
25662562
pub(crate) fn gen_type(&self, ident: &ast::Identifier) -> Type {
25672563
let vis = ident.vis.display_as_accessor();
25682564
mono(format!("{}{vis}{}", self.name, ident.inspect()))

crates/erg_compiler/hir.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,14 @@ impl Expr {
25292529
}
25302530
}
25312531

2532+
pub fn var_info(&self) -> Option<&VarInfo> {
2533+
match self {
2534+
Expr::Accessor(acc) => Some(acc.var_info()),
2535+
Expr::TypeAsc(t_asc) => t_asc.expr.var_info(),
2536+
_ => None,
2537+
}
2538+
}
2539+
25322540
/// 参照するオブジェクト自体が持っている名前(e.g. Int.qual_name == Some("int"), Socket!.qual_name == Some("io.Socket!"))
25332541
pub fn qual_name(&self) -> Option<Str> {
25342542
match self {

crates/erg_compiler/varinfo.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,8 @@ impl VarInfo {
329329
AbsLocation::unknown(),
330330
)
331331
}
332+
333+
pub fn is_untyped_parameter(&self) -> bool {
334+
self.kind.is_parameter() && self.t.is_unbound_var()
335+
}
332336
}

0 commit comments

Comments
 (0)