Skip to content

Commit 6151d2e

Browse files
instantiate generics using the passed functions
This patch adds simple type level matching for the doc functions and signatures allowing to instantiate generics based on the passed functions. The following code demonstrates a generic type instantiated by this feature. ```lua ---@param a number ---@return string local function tostr(a) return tostring(a) end ---@Generic T, U ---@param x T ---@param f fun(x: T): U ---@return U local function apply(x, f) return f(x) end --"a" is inferred as a string. a = apply(3, tostr) ```
1 parent a94b0bb commit 6151d2e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
`CHG` refactor `semantic token`
1010

11+
`NEW` support simple generic type instantiation based on the passed functions
12+
1113
# 0.3.2
1214

1315
`FIX` Fixed some multiple return value inference errors

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,32 @@ fn func_tpl_pattern_match(
178178
) -> Option<()> {
179179
match target {
180180
LuaType::DocFunction(target_doc_func) => {
181-
// todo
181+
for i in 0..doc_func.get_params().len() {
182+
let param_type = &doc_func.get_params()[i].clone().1?;
183+
let target_param_type = &target_doc_func.get_params().get(i)?.clone().1?;
184+
tpl_pattern_match(db, config, root, param_type, target_param_type, result);
185+
}
186+
187+
for i in 0..doc_func.get_ret().len() {
188+
let ret_type = &doc_func.get_ret()[i];
189+
let target_ret_type = &target_doc_func.get_ret().get(i)?;
190+
tpl_pattern_match(db, config, root, ret_type, target_ret_type, result);
191+
}
182192
}
183193
LuaType::Signature(signature_id) => {
184-
// todo
194+
let signature = db.get_signature_index().get(&signature_id)?;
195+
196+
for i in 0..doc_func.get_params().len() {
197+
let param_type = &doc_func.get_params()[i].clone().1?;
198+
let target_param_type = &signature.param_docs.get(&i)?.type_ref;
199+
tpl_pattern_match(db, config, root, param_type, target_param_type, result);
200+
}
201+
202+
for i in 0..doc_func.get_ret().len() {
203+
let ret_type = &doc_func.get_ret()[i];
204+
let target_ret_type = &signature.return_docs.get(i)?.type_ref;
205+
tpl_pattern_match(db, config, root, ret_type, target_ret_type, result);
206+
}
185207
}
186208
_ => {}
187209
}

0 commit comments

Comments
 (0)