Skip to content

Commit b032896

Browse files
committed
fix #896
1 parent a368099 commit b032896

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

crates/emmylua_code_analysis/src/diagnostic/test/param_type_check_test.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,4 +1478,40 @@ mod test {
14781478
"#,
14791479
));
14801480
}
1481+
1482+
#[test]
1483+
fn test_issue_896() {
1484+
let mut ws = VirtualWorkspace::new();
1485+
let mut emmyrc = ws.get_emmyrc();
1486+
emmyrc.strict.array_index = false;
1487+
ws.update_emmyrc(emmyrc);
1488+
1489+
assert!(ws.check_code_for(
1490+
DiagnosticCode::ParamTypeMismatch,
1491+
r#"
1492+
---@alias MyFnA fun(): number
1493+
---@alias MyFnB<T> fun(): T
1494+
---@alias MyFnC<T> fun(): number
1495+
1496+
local aaa ---@type MyFnA[]
1497+
---@param aaa MyFnA[]
1498+
local function AAA(aaa) return aaa end
1499+
local _ = AAA(aaa)
1500+
1501+
local bbb1 ---@type (MyFnB<number>)[]
1502+
local bbb2 = { function() return 1 end } ---@type (MyFnB<number>)[]
1503+
---@param bbb (MyFnB<number>)[]
1504+
local function BBB(bbb) return bbb end
1505+
local _ = BBB(bbb1)
1506+
local _ = BBB(bbb2)
1507+
1508+
local ccc1 ---@type (MyFnC<number>)[]
1509+
local ccc2 = { function() return 1 end } ---@type (MyFnC<number>)[]
1510+
---@param ccc (MyFnC<number>)[]
1511+
local function CCC(ccc) return ccc end
1512+
local _ = CCC(ccc1)
1513+
local _ = CCC(ccc2)
1514+
"#,
1515+
));
1516+
}
14811517
}

crates/emmylua_code_analysis/src/semantic/type_check/func_type.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
TypeSubstitutor,
23
db_index::{LuaFunctionType, LuaOperatorMetaMethod, LuaSignatureId, LuaType, LuaTypeDeclId},
34
semantic::type_check::type_check_context::TypeCheckContext,
45
};
@@ -14,6 +15,27 @@ pub fn check_doc_func_type_compact(
1415
compact_type: &LuaType,
1516
check_guard: TypeCheckGuard,
1617
) -> TypeCheckResult {
18+
// TODO: 缓存以提高性能
19+
// 如果是泛型+不包含模板参数+alias, 那么尝试实例化再检查
20+
if let LuaType::Generic(generic) = compact_type {
21+
if !generic.contain_tpl() {
22+
let base_id = generic.get_base_type_id();
23+
if let Some(decl) = context.db.get_type_index().get_type_decl(&base_id)
24+
&& decl.is_alias()
25+
{
26+
let substitutor =
27+
TypeSubstitutor::from_alias(generic.get_params().clone(), base_id.clone());
28+
if let Some(alias_origin) = decl.get_alias_origin(context.db, Some(&substitutor)) {
29+
return check_general_type_compact(
30+
context,
31+
&LuaType::DocFunction(source_func.clone().into()),
32+
&alias_origin,
33+
check_guard.next_level()?,
34+
);
35+
}
36+
}
37+
}
38+
}
1739
match compact_type {
1840
LuaType::DocFunction(compact_func) => {
1941
check_doc_func_type_compact_for_params(context, source_func, compact_func, check_guard)

0 commit comments

Comments
 (0)