Skip to content

Commit 2f427f7

Browse files
committed
fix union_tpl_pattern_match
1 parent 40fcd72 commit 2f427f7

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,4 +1380,42 @@ mod test {
13801380
"#
13811381
));
13821382
}
1383+
1384+
#[test]
1385+
fn test_pairs_1() {
1386+
let mut ws = VirtualWorkspace::new();
1387+
ws.def(
1388+
r#"
1389+
---@param value string
1390+
function aaaa(value)
1391+
end
1392+
1393+
---@generic K, V
1394+
---@param t {[K]: V} | V[]
1395+
---@return fun(tbl: any):K, V
1396+
function pairs(t) end
1397+
"#,
1398+
);
1399+
assert!(!ws.check_code_for(
1400+
DiagnosticCode::ParamTypeMismatch,
1401+
r#"
1402+
---@type {[string]: number}
1403+
local matchers = {}
1404+
for _, matcher in pairs(matchers) do
1405+
aaaa(matcher)
1406+
end
1407+
"#
1408+
));
1409+
assert!(!ws.check_code_for(
1410+
DiagnosticCode::ParamTypeMismatch,
1411+
r#"
1412+
---@alias MatchersObject {[string]: number}
1413+
---@type MatchersObject
1414+
local matchers = {}
1415+
for _, matcher in pairs(matchers) do
1416+
aaaa(matcher)
1417+
end
1418+
"#
1419+
));
1420+
}
13831421
}

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type/instantiate_func_generic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ fn infer_generic_types_from_call(
160160
}
161161

162162
let arg_type = infer_expr(db, context.cache, call_arg_expr.clone())?;
163-
164163
match (func_param_type, &arg_type) {
165164
(LuaType::Variadic(variadic), _) => {
166165
let mut arg_types = vec![];

crates/emmylua_code_analysis/src/semantic/generic/tpl_pattern/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,24 @@ fn union_tpl_pattern_match(
509509
union: &LuaUnionType,
510510
target: &LuaType,
511511
) -> TplPatternMatchResult {
512+
let mut error_count = 0;
513+
let mut last_error = InferFailReason::None;
512514
for u in union.into_vec() {
513-
tpl_pattern_match(context, &u, target)?;
515+
match tpl_pattern_match(context, &u, target) {
516+
// 返回 ok 时并不一定匹配成功, 仅表示没有发生错误
517+
Ok(_) => {}
518+
Err(e) => {
519+
error_count += 1;
520+
last_error = e;
521+
}
522+
}
514523
}
515524

516-
Ok(())
525+
if error_count == union.into_vec().len() {
526+
Err(last_error)
527+
} else {
528+
Ok(())
529+
}
517530
}
518531

519532
fn func_tpl_pattern_match(

0 commit comments

Comments
 (0)