Skip to content

Commit 219d810

Browse files
committed
special support for table< any, *>
1 parent 13aef33 commit 219d810

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,24 @@ mod test {
8989
"#
9090
));
9191
}
92+
93+
#[test]
94+
fn test_issue_83() {
95+
let mut ws = VirtualWorkspace::new();
96+
97+
assert!(ws.check_code_for(
98+
DiagnosticCode::ParamTypeNotMatch,
99+
r#"
100+
---@param _t table<any, string>
101+
local function foo(_t) end
102+
103+
foo({})
104+
foo({'a'})
105+
foo({'a', 'b'})
106+
107+
local a ---@type string[]
108+
foo(a)
109+
"#
110+
));
111+
}
92112
}

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
};
77

88
use super::{
9-
check_general_type_compact, type_check_fail_reason::TypeCheckFailReason,
9+
check_general_type_compact, check_type_compact, type_check_fail_reason::TypeCheckFailReason,
1010
type_check_guard::TypeCheckGuard, TypeCheckResult,
1111
};
1212

@@ -203,6 +203,40 @@ pub fn check_complex_type_compact(
203203
check_guard.next_level()?,
204204
);
205205
}
206+
LuaType::Array(base) => {
207+
if source_generic_param.len() == 2 {
208+
let key = &source_generic_param[0];
209+
let value = &source_generic_param[1];
210+
if key.is_any() && check_type_compact(db, value, base).is_ok() {
211+
return Ok(());
212+
}
213+
}
214+
}
215+
LuaType::Tuple(tuple) => {
216+
if source_generic_param.len() == 2 {
217+
let key = &source_generic_param[0];
218+
let value = &source_generic_param[1];
219+
if key.is_any() {
220+
for tuple_type in tuple.get_types() {
221+
if check_general_type_compact(
222+
db,
223+
value,
224+
tuple_type,
225+
check_guard.next_level()?,
226+
)
227+
.is_err()
228+
{
229+
return Err(TypeCheckFailReason::TypeNotMatch);
230+
}
231+
}
232+
233+
return Ok(());
234+
}
235+
236+
return Ok(());
237+
}
238+
}
239+
// maybe support object
206240
// need check later
207241
LuaType::Ref(_) | LuaType::Def(_) | LuaType::Userdata => return Ok(()),
208242
_ => {}
@@ -242,7 +276,8 @@ pub fn check_complex_type_compact(
242276
// Do I need to check union types?
243277
if let LuaType::Union(union) = compact_type {
244278
for sub_compact in union.get_types() {
245-
if check_complex_type_compact(db, source, sub_compact, check_guard.next_level()?).is_err()
279+
if check_complex_type_compact(db, source, sub_compact, check_guard.next_level()?)
280+
.is_err()
246281
{
247282
return Err(TypeCheckFailReason::TypeNotMatch);
248283
}

0 commit comments

Comments
 (0)