Skip to content

Commit 9cf96cc

Browse files
committed
fix #791
1 parent 3ed9d94 commit 9cf96cc

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/unresolve/resolve_closure.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,11 @@ fn resolve_doc_function(
486486
Ok(())
487487
}
488488

489-
fn filter_signature_type(typ: &LuaType) -> Option<Vec<Arc<LuaFunctionType>>> {
489+
fn filter_signature_type(db: &DbIndex, typ: &LuaType) -> Option<Vec<Arc<LuaFunctionType>>> {
490490
let mut result: Vec<Arc<LuaFunctionType>> = Vec::new();
491491
let mut stack = Vec::new();
492492
stack.push(typ.clone());
493+
let guard = InferGuard::new();
493494
while let Some(typ) = stack.pop() {
494495
match typ {
495496
LuaType::DocFunction(func) => {
@@ -501,6 +502,24 @@ fn filter_signature_type(typ: &LuaType) -> Option<Vec<Arc<LuaFunctionType>>> {
501502
stack.push(typ);
502503
}
503504
}
505+
LuaType::Ref(type_ref_id) => {
506+
guard.check(&type_ref_id).ok()?;
507+
let type_decl = db.get_type_index().get_type_decl(&type_ref_id)?;
508+
if let Some(func) = type_decl.get_alias_origin(db, None) {
509+
match func {
510+
LuaType::DocFunction(f) => {
511+
result.push(f.clone());
512+
}
513+
LuaType::Union(union) => {
514+
let types = union.into_vec();
515+
for typ in types.into_iter().rev() {
516+
stack.push(typ);
517+
}
518+
}
519+
_ => {}
520+
}
521+
}
522+
}
504523
_ => {}
505524
}
506525
}
@@ -522,7 +541,7 @@ fn find_best_function_type(
522541
if let Ok(result) = find_decl_function_type(db, cache, prefix_type, index_member_expr) {
523542
if result.is_current_owner {
524543
// 对应当前类型下的声明, 我们需要过滤掉所有`signature`类型
525-
if let Some(filtered_types) = filter_signature_type(&result.typ) {
544+
if let Some(filtered_types) = filter_signature_type(db, &result.typ) {
526545
match filtered_types.len() {
527546
0 => {}
528547
1 => return Some(LuaType::DocFunction(filtered_types[0].clone())),

crates/emmylua_code_analysis/src/compilation/test/closure_param_infer_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,31 @@ mod test {
487487
let expected = ws.ty("LocalTimer");
488488
assert_eq!(ws.humanize_type(ty), ws.humanize_type(expected));
489489
}
490+
491+
#[test]
492+
fn test_issue_791() {
493+
let mut ws = VirtualWorkspace::new();
494+
ws.def(
495+
r#"
496+
---@alias HookAlias fun(a:integer)
497+
498+
---@class TypeA
499+
---@field hook HookAlias
500+
501+
---@class TypeB
502+
---@field hook fun(a:integer)
503+
504+
---@param d TypeA
505+
function fnA(d) end
506+
507+
---@param d TypeB
508+
function fnB(d) end
509+
510+
fnA({ hook = function(obj) a = obj end }) -- obj is any, not integer
511+
"#,
512+
);
513+
let ty = ws.expr_ty("a");
514+
let expected = ws.ty("integer");
515+
assert_eq!(ty, expected);
516+
}
490517
}

0 commit comments

Comments
 (0)