Skip to content

refactor generic infer #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use emmylua_parser::{LuaAstNode, LuaAstToken, LuaExpr, LuaForRangeStat};
use crate::{
compilation::analyzer::unresolve::UnResolveIterVar, infer_expr, instantiate_doc_function,
tpl_pattern_match_args, DbIndex, InferFailReason, LuaDeclId, LuaInferCache,
LuaOperatorMetaMethod, LuaType, LuaTypeCache, TypeOps, TypeSubstitutor, VariadicType,
LuaOperatorMetaMethod, LuaType, LuaTypeCache, TplContext, TypeOps, TypeSubstitutor,
VariadicType,
};

use super::LuaAnalyzer;
Expand Down Expand Up @@ -149,19 +150,19 @@ pub fn infer_for_range_iter_expr_func(
return Ok(doc_function.get_variadic_ret());
}
let mut substitutor = TypeSubstitutor::new();
let mut context = TplContext {
db,
cache,
substitutor: &mut substitutor,
root: root,
};
let params = doc_function
.get_params()
.iter()
.map(|(_, opt_ty)| opt_ty.clone().unwrap_or(LuaType::Any))
.collect::<Vec<_>>();
tpl_pattern_match_args(
db,
cache,
&params,
&vec![status_param.clone().unwrap()],
&root,
&mut substitutor,
)?;

tpl_pattern_match_args(&mut context, &params, &vec![status_param.clone().unwrap()])?;

let instantiate_func = if let LuaType::DocFunction(f) =
instantiate_doc_function(db, &doc_function, &substitutor)
Expand Down
70 changes: 70 additions & 0 deletions crates/emmylua_code_analysis/src/compilation/test/generic_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#[cfg(test)]
mod test {
use crate::VirtualWorkspace;

#[test]
fn test_issue_586() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
ws.def(
r#"
--- @generic T
--- @param cb fun(...: T...)
--- @param ... T...
function invoke1(cb, ...)
cb(...)
end

invoke1(
function(a, b, c)
_a = a
_b = b
_c = c
end,
1, "2", "3"
)
"#,
);

let a_ty = ws.expr_ty("_a");
let b_ty = ws.expr_ty("_b");
let c_ty = ws.expr_ty("_c");

assert_eq!(a_ty, ws.ty("integer"));
assert_eq!(b_ty, ws.ty("string"));
assert_eq!(c_ty, ws.ty("string"));
}

#[test]
fn test_issue_658() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
ws.def(
r#"
--- @generic T1, T2, R
--- @param fn fun(_:T1..., _:T2...): R...
--- @param ... T1...
--- @return fun(_:T2...): R...
local function curry(fn, ...)
local nargs, args = select('#', ...), { ... }
return function(...)
local nargs2 = select('#', ...)
for i = 1, nargs2 do
args[nargs + i] = select(i, ...)
end
return fn(unpack(args, 1, nargs + nargs2))
end
end

--- @param a string
--- @param b string
--- @param c table
local function foo(a, b, c) end

bar = curry(foo, 'a')
"#,
);

let bar_ty = ws.expr_ty("bar");
let expected = ws.ty("fun(b:string, c:table)");
assert_eq!(bar_ty, expected);
}
}
1 change: 1 addition & 0 deletions crates/emmylua_code_analysis/src/compilation/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod diagnostic_disable_test;
mod export_test;
mod flow;
mod for_range_var_infer_test;
mod generic_test;
mod infer_str_tpl_test;
mod inherit_type;
mod mathlib_test;
Expand Down
2 changes: 2 additions & 0 deletions crates/emmylua_code_analysis/src/db_index/type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod test;
mod type_decl;
mod type_ops;
mod type_owner;
mod type_visit_trait;
mod types;

use super::traits::LuaIndex;
Expand All @@ -14,6 +15,7 @@ pub use type_decl::{
};
pub use type_ops::TypeOps;
pub use type_owner::{LuaTypeCache, LuaTypeOwner};
pub use type_visit_trait::TypeVisitTrait;
pub use types::*;

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::LuaType;

pub trait TypeVisitTrait {
fn visit_type<F>(&self, f: &mut F)
where
F: FnMut(&LuaType);
}
Loading
Loading