Skip to content

Commit ea9d237

Browse files
committed
fix #565
1 parent e671005 commit ea9d237

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

crates/emmylua_code_analysis/src/diagnostic/checker/cast_type_mismatch.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ fn expand_type_recursive(
242242
LuaType::Union(union_type) => {
243243
// 递归展开 union 中的每个类型
244244
let mut expanded_types = Vec::new();
245+
let mut has_nil = false;
245246
for inner_type in union_type.get_types() {
247+
if inner_type.is_nil() {
248+
has_nil = true;
249+
continue;
250+
}
246251
if let Some(expanded) = expand_type_recursive(db, inner_type, visited) {
247252
match expanded {
248253
LuaType::Union(inner_union) => {
@@ -260,7 +265,13 @@ fn expand_type_recursive(
260265

261266
let expanded_types = expanded_types.into_iter().unique().collect::<Vec<_>>();
262267
return match expanded_types.len() {
263-
0 => Some(LuaType::Unknown),
268+
0 => {
269+
if has_nil {
270+
Some(LuaType::Nil)
271+
} else {
272+
Some(LuaType::Unknown)
273+
}
274+
}
264275
1 => Some(expanded_types[0].clone().into()),
265276
_ => Some(LuaType::Union(LuaUnionType::new(expanded_types).into())),
266277
};

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,20 @@ mod tests {
233233
"#
234234
));
235235
}
236+
237+
#[test]
238+
fn test_issue_565() {
239+
let mut ws = VirtualWorkspace::new();
240+
ws.def(
241+
r#"
242+
"#,
243+
);
244+
assert!(ws.check_code_for(
245+
DiagnosticCode::CastTypeMismatch,
246+
r#"
247+
local a --- @type table?
248+
--- @cast a [integer,integer]?
249+
"#
250+
));
251+
}
236252
}

0 commit comments

Comments
 (0)