Skip to content

Commit 40fcd72

Browse files
committed
fix #844
1 parent 40da8e3 commit 40fcd72

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,4 +1355,29 @@ mod test {
13551355
"#,
13561356
));
13571357
}
1358+
1359+
#[test]
1360+
fn test_fix_issue_844() {
1361+
let mut ws = VirtualWorkspace::new();
1362+
ws.def(
1363+
r#"
1364+
---@alias Tester fun(customTesters: Tester[]): boolean?
1365+
1366+
---@generic V
1367+
---@param t V[]
1368+
---@return fun(tbl: any):int, V
1369+
function ipairs(t) end
1370+
"#,
1371+
);
1372+
assert!(ws.check_code_for(
1373+
DiagnosticCode::ParamTypeMismatch,
1374+
r#"
1375+
---@param newTesters Tester[]
1376+
local function addMatchers(newTesters)
1377+
for _, tester in ipairs(newTesters) do
1378+
end
1379+
end
1380+
"#
1381+
));
1382+
}
13581383
}

crates/emmylua_code_analysis/src/semantic/type_check/complex_type/array_type_check.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ pub fn check_array_type_compact(
1313
compact_type: &LuaType,
1414
check_guard: TypeCheckGuard,
1515
) -> TypeCheckResult {
16-
let source_base = TypeOps::Union.apply(context.db, source_base, &LuaType::Nil);
16+
let source_base = if context.db.get_emmyrc().strict.array_index {
17+
TypeOps::Union.apply(context.db, source_base, &LuaType::Nil)
18+
} else {
19+
source_base.clone()
20+
};
1721

1822
match compact_type {
1923
LuaType::Array(compact_array_type) => {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ mod type_check_context;
99
mod type_check_fail_reason;
1010
mod type_check_guard;
1111

12+
use std::ops::Deref;
13+
1214
use complex_type::check_complex_type_compact;
1315
use func_type::{check_doc_func_type_compact, check_sig_type_compact};
1416
use generic_type::check_generic_type_compact;
@@ -18,6 +20,7 @@ pub use type_check_fail_reason::TypeCheckFailReason;
1820
use type_check_guard::TypeCheckGuard;
1921

2022
use crate::{
23+
LuaUnionType,
2124
db_index::{DbIndex, LuaType},
2225
semantic::type_check::type_check_context::TypeCheckContext,
2326
};
@@ -198,6 +201,12 @@ fn fast_eq_check(a: &LuaType, b: &LuaType) -> bool {
198201
| (LuaType::Unknown, LuaType::Unknown)
199202
| (LuaType::Any, LuaType::Any) => true,
200203
(LuaType::Ref(type_id_left), LuaType::Ref(type_id_right)) => type_id_left == type_id_right,
204+
(LuaType::Union(u), LuaType::Ref(type_id_right)) => {
205+
if let LuaUnionType::Nullable(LuaType::Ref(type_id_left)) = u.deref() {
206+
return type_id_left == type_id_right;
207+
}
208+
false
209+
}
201210
_ => false,
202211
}
203212
}

0 commit comments

Comments
 (0)