Skip to content

Commit 2a8b8ee

Browse files
committed
fix object type
1 parent 76835d1 commit 2a8b8ee

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

crates/emmylua_code_analysis/resources/std/table.lua

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ function table.insert(list, pos, value) end
5858
---@return table
5959
function table.move(a1, f, e, t, a2) end
6060

61-
---
62-
--- Returns a new table with all arguments stored into keys 1, 2, etc. and
63-
--- with a field "`n`" with the total number of arguments. Note that the
64-
--- resulting table may not be a sequence, if some arguments are **nil**.
65-
---@generic T...
66-
---@param ... T...
67-
---@return [T...]
68-
function table.pack(...) end
69-
7061
---@version 5.1
7162
---
7263
---Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices.
@@ -108,8 +99,8 @@ function table.remove(list, pos) end
10899
--- order may have their relative positions changed by the sort.
109100
---@overload fun(list:table):number
110101
---@generic V
111-
---@param list table<number, V> | V[]
112-
---@param comp? fun(a:V, b:V):boolean
102+
---@param list V[]
103+
---@param comp fun(a:V, b:V):boolean
113104
---@return number
114105
function table.sort(list, comp) end
115106

@@ -130,7 +121,7 @@ function table.unpack(list, i, j) end
130121
---
131122
---@generic T
132123
---@param ... T...
133-
---@return [T...] | { n: number }
124+
---@return [T...] | { n: integer }
134125
---@nodiscard
135126
function table.pack(...) end
136127

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,40 @@ impl LuaObjectType {
555555
.iter()
556556
.any(|(k, v)| k.contain_tpl() || v.contain_tpl())
557557
}
558+
559+
pub fn cast_down_array_base(&self) -> Option<LuaType> {
560+
if self.index_access.len() != 0 {
561+
return None;
562+
}
563+
564+
let mut ty = LuaType::Unknown;
565+
let mut count = 1;
566+
let mut fields = self
567+
.fields
568+
.iter()
569+
.collect::<Vec<_>>();
570+
571+
fields.sort_by(|(a, _), (b, _)| a.cmp(b));
572+
573+
for (key, value_type) in fields {
574+
let idx = match key {
575+
LuaMemberKey::Integer(i) => i,
576+
_ => {
577+
return None;
578+
}
579+
};
580+
581+
if *idx != count {
582+
return None;
583+
}
584+
585+
count += 1;
586+
587+
ty = TypeOps::Union.apply(&ty, value_type);
588+
}
589+
590+
Some(ty)
591+
}
558592
}
559593

560594
impl From<LuaObjectType> for LuaType {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,18 @@ mod test {
206206
"#
207207
));
208208
}
209+
210+
#[test]
211+
fn test_issue_95() {
212+
let mut ws = VirtualWorkspace::new_with_init_std_lib();
213+
214+
assert!(ws.check_code_for(
215+
DiagnosticCode::ParamTypeNotMatch,
216+
r#"
217+
local range ---@type { [1]: integer, [2]: integer }
218+
219+
table.sort(range)
220+
"#
221+
));
222+
}
209223
}

crates/emmylua_code_analysis/src/semantic/instantiate/tpl_pattern.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ fn array_tpl_pattern_match(
7878
let target_base = target_tuple.cast_down_array_base();
7979
tpl_pattern_match(db, config, root, base, &target_base, substitutor);
8080
}
81+
LuaType::Object(target_object) => {
82+
let target_base = target_object.cast_down_array_base()?;
83+
tpl_pattern_match(db, config, root, base, &target_base, substitutor);
84+
}
8185
_ => {}
8286
}
8387

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ pub fn check_complex_type_compact(
5252
check_guard.next_level()?,
5353
);
5454
}
55+
LuaType::Object(compact_object) => {
56+
let compact_base = compact_object
57+
.cast_down_array_base()
58+
.ok_or(TypeCheckFailReason::TypeNotMatch)?;
59+
return check_general_type_compact(
60+
db,
61+
source_base,
62+
&compact_base,
63+
check_guard.next_level()?,
64+
);
65+
}
5566
LuaType::Table => return Ok(()),
5667
_ => {}
5768
},

0 commit comments

Comments
 (0)