Skip to content

Commit 8c5624a

Browse files
committed
optimize for range for tuple
1 parent e954734 commit 8c5624a

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn union_type(source: LuaType, target: LuaType) -> LuaType {
4747
.iter()
4848
.map(|it| it.clone())
4949
.collect::<Vec<_>>();
50+
types.dedup();
5051
types.push(right.clone());
5152

5253
LuaType::Union(LuaUnionType::new(types).into())

crates/emmylua_code_analysis/src/db_index/type/types.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,19 @@ impl LuaTupleType {
410410
pub fn cast_down_array_base(&self) -> LuaType {
411411
let mut ty = LuaType::Unknown;
412412
for t in &self.types {
413-
if t.is_integer() {
414-
ty = TypeOps::Union.apply(&ty, &LuaType::Integer);
415-
} else if t.is_number() {
416-
ty = TypeOps::Union.apply(&ty, &LuaType::Number);
417-
} else if t.is_string() {
418-
ty = TypeOps::Union.apply(&ty, &LuaType::String);
419-
} else if t.is_boolean() {
420-
ty = TypeOps::Union.apply(&ty, &LuaType::Boolean);
421-
} else {
422-
ty = TypeOps::Union.apply(&ty, &t);
413+
match t {
414+
LuaType::IntegerConst(i) => {
415+
ty = TypeOps::Union.apply(&ty, &LuaType::DocIntegerConst(*i));
416+
}
417+
LuaType::FloatConst(_) => {
418+
ty = TypeOps::Union.apply(&ty, &LuaType::Number);
419+
}
420+
LuaType::StringConst(s) => {
421+
ty = TypeOps::Union.apply(&ty, &LuaType::DocStringConst(s.clone()));
422+
}
423+
_ => {
424+
ty = TypeOps::Union.apply(&ty, t);
425+
}
423426
}
424427
}
425428

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,21 @@ mod test {
189189
"#
190190
));
191191
}
192+
193+
#[test]
194+
fn test_issue_102() {
195+
let mut ws = VirtualWorkspace::new_with_init_std_lib();
196+
197+
assert!(ws.check_code_for(
198+
DiagnosticCode::ParamTypeNotMatch,
199+
r#"
200+
---@param _kind '' | 'Nr' | 'Ln' | 'Cul'
201+
function foo(_kind) end
202+
203+
for _, kind in ipairs({ '', 'Nr', 'Ln', 'Cul' }) do
204+
foo(kind)
205+
end
206+
"#
207+
));
208+
}
192209
}

0 commit comments

Comments
 (0)