Skip to content

Commit 1351438

Browse files
committed
fix #573
1 parent f55faa2 commit 1351438

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,25 @@ mod test {
11671167
"#
11681168
));
11691169
}
1170+
1171+
#[test]
1172+
fn test_issue_573() {
1173+
let mut ws = VirtualWorkspace::new();
1174+
ws.def(
1175+
r#"
1176+
--- @class A
1177+
--- @field [integer] string
1178+
--- @field data any
1179+
1180+
--- @param a string[]
1181+
function takesArray(a) end
1182+
"#,
1183+
);
1184+
assert!(ws.check_code_for(
1185+
DiagnosticCode::ParamTypeNotMatch,
1186+
r#"
1187+
takesArray({} --[[@as A]])
1188+
"#
1189+
));
1190+
}
11701191
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
find_index_operations,
23
semantic::type_check::{check_general_type_compact, type_check_guard::TypeCheckGuard},
34
DbIndex, LuaMemberKey, LuaMemberOwner, LuaType, TypeCheckFailReason, TypeCheckResult, TypeOps,
45
};
@@ -63,12 +64,47 @@ pub fn check_array_type_compact(
6364
}
6465
}
6566
LuaType::Any => return Ok(()),
67+
LuaType::Ref(_) | LuaType::Def(_) => {
68+
return check_array_type_compact_ref_def(
69+
db,
70+
&source_base,
71+
compact_type,
72+
check_guard.next_level()?,
73+
);
74+
}
6675
_ => {}
6776
}
6877

6978
Err(TypeCheckFailReason::DonotCheck)
7079
}
7180

81+
fn check_array_type_compact_ref_def(
82+
db: &DbIndex,
83+
source_base: &LuaType,
84+
compact_type: &LuaType,
85+
check_guard: TypeCheckGuard,
86+
) -> TypeCheckResult {
87+
let Some(members) = find_index_operations(db, compact_type) else {
88+
return Err(TypeCheckFailReason::TypeNotMatch);
89+
};
90+
91+
for member in &members {
92+
match &member.key {
93+
LuaMemberKey::ExprType(key_type) => {
94+
if key_type.is_integer() {
95+
match check_general_type_compact(db, source_base, &member.typ, check_guard) {
96+
Ok(()) => return Ok(()),
97+
_ => {}
98+
}
99+
}
100+
}
101+
_ => {}
102+
}
103+
}
104+
105+
Err(TypeCheckFailReason::TypeNotMatch)
106+
}
107+
72108
fn check_array_type_compact_table(
73109
db: &DbIndex,
74110
source_base: &LuaType,

0 commit comments

Comments
 (0)