Skip to content

Commit df64825

Browse files
committed
Fixes #393
1 parent 211fc24 commit df64825

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

crates/emmylua_code_analysis/src/db_index/type/type_ops/remove_type.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,21 @@ pub fn remove_type(db: &DbIndex, source: LuaType, removed_type: LuaType) -> Opti
5959
LuaType::TableConst(_)
6060
| LuaType::Table
6161
| LuaType::Userdata
62-
| LuaType::Ref(_)
63-
| LuaType::Def(_)
6462
| LuaType::Global
6563
| LuaType::Array(_)
6664
| LuaType::Tuple(_)
6765
| LuaType::Generic(_)
6866
| LuaType::Object(_)
6967
| LuaType::TableGeneric(_) => return None,
68+
LuaType::Ref(type_decl_id) | LuaType::Def(type_decl_id) => {
69+
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
70+
if type_decl.is_alias() {
71+
if let Some(alias_ref) = get_real_type(db, &source) {
72+
return remove_type(db, alias_ref.clone(), removed_type);
73+
}
74+
}
75+
return None;
76+
}
7077
_ => {}
7178
},
7279
LuaType::DocStringConst(s) => match &source {
@@ -152,3 +159,16 @@ pub fn remove_type(db: &DbIndex, source: LuaType, removed_type: LuaType) -> Opti
152159

153160
Some(source)
154161
}
162+
163+
fn get_real_type<'a>(db: &'a DbIndex, compact_type: &'a LuaType) -> Option<&'a LuaType> {
164+
match compact_type {
165+
LuaType::Ref(type_decl_id) => {
166+
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
167+
if type_decl.is_alias() {
168+
return get_real_type(db, type_decl.get_alias_ref()?);
169+
}
170+
Some(compact_type)
171+
}
172+
_ => Some(compact_type),
173+
}
174+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,4 +822,23 @@ return t
822822
"#
823823
));
824824
}
825+
826+
#[test]
827+
fn test_issue_393() {
828+
let mut ws = VirtualWorkspace::new();
829+
assert!(ws.check_code_for(
830+
DiagnosticCode::AssignTypeMismatch,
831+
r#"
832+
---@alias SortByScoreCallback fun(o: any): integer
833+
834+
---@param tbl any[]
835+
---@param callbacks SortByScoreCallback | SortByScoreCallback[]
836+
function sortByScore(tbl, callbacks)
837+
if type(callbacks) ~= 'table' then
838+
callbacks = { callbacks }
839+
end
840+
end
841+
"#
842+
));
843+
}
825844
}

0 commit comments

Comments
 (0)