Skip to content

Commit d90f71f

Browse files
committed
diagnostic: fix call param is function
1 parent 3408f39 commit d90f71f

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

crates/emmylua_code_analysis/src/diagnostic/checker/check_param_count.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ fn check_closure_expr(
3838
semantic_model: &SemanticModel,
3939
closure_expr: &LuaClosureExpr,
4040
) -> Option<()> {
41-
let source_typ =
42-
semantic_model.infer_left_value_type_from_right_value(closure_expr.clone().into())?;
43-
let right_value = context
41+
let current_signature = context
4442
.db
4543
.get_signature_index()
4644
.get(&LuaSignatureId::from_closure(
4745
semantic_model.get_file_id(),
4846
&closure_expr,
4947
))?;
48+
49+
let source_typ = semantic_model.infer_bind_value_type(closure_expr.clone().into())?;
50+
5051
let source_params_len = match &source_typ {
5152
LuaType::DocFunction(func_type) => {
5253
let params = func_type.get_params();
@@ -61,7 +62,7 @@ fn check_closure_expr(
6162
}?;
6263

6364
// 只检查右值参数多于左值参数的情况, 右值参数少于左值参数的情况是能够接受的
64-
if source_params_len > right_value.params.len() {
65+
if source_params_len > current_signature.params.len() {
6566
return Some(());
6667
}
6768
let params = closure_expr
@@ -76,7 +77,7 @@ fn check_closure_expr(
7677
t!(
7778
"expected %{num} parameters but found %{found_num}",
7879
num = source_params_len,
79-
found_num = right_value.params.len(),
80+
found_num = current_signature.params.len(),
8081
)
8182
.to_string(),
8283
None,

crates/emmylua_code_analysis/src/diagnostic/checker/check_return_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn get_function_return_info(
3232
closure_expr: &LuaClosureExpr,
3333
) -> Option<(bool, LuaType)> {
3434
let typ = semantic_model
35-
.infer_left_value_type_from_right_value(closure_expr.clone().into())
35+
.infer_bind_value_type(closure_expr.clone().into())
3636
.unwrap_or(LuaType::Unknown);
3737

3838
match typ {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,24 @@ mod test {
105105
"#
106106
));
107107
}
108+
109+
#[test]
110+
fn test_function_param() {
111+
let mut ws = VirtualWorkspace::new();
112+
assert!(!ws.check_code_for(
113+
DiagnosticCode::RedundantParameter,
114+
r#"
115+
---@class D30
116+
local M = {}
117+
118+
---@param callback fun()
119+
local function with_local(callback)
120+
end
121+
122+
function M:add_local_event()
123+
with_local(function(local_player) end)
124+
end
125+
"#
126+
));
127+
}
108128
}

crates/emmylua_code_analysis/src/semantic/infer/mod.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ mod test;
1010
use std::ops::Deref;
1111

1212
use emmylua_parser::{
13-
LuaAst, LuaAstNode, LuaClosureExpr, LuaExpr, LuaLiteralExpr, LuaLiteralToken, LuaTableExpr,
14-
LuaVarExpr,
13+
LuaAst, LuaAstNode, LuaCallExpr, LuaClosureExpr, LuaExpr, LuaLiteralExpr, LuaLiteralToken,
14+
LuaTableExpr, LuaVarExpr,
1515
};
1616
use infer_binary::infer_binary_expr;
1717
use infer_call::infer_call_expr;
@@ -213,15 +213,14 @@ pub fn infer_multi_value_adjusted_expression_types(
213213
value_types
214214
}
215215

216-
/// 从右值推断左值已绑定的类型
217-
pub fn infer_left_value_type_from_right_value(
216+
/// 推断值已经绑定的类型(不是推断值的类型). 例如从右值推断左值类型, 从调用参数推断函数参数类型参数类型
217+
pub fn infer_bind_value_type(
218218
db: &DbIndex,
219219
cache: &mut LuaInferCache,
220220
expr: LuaExpr,
221221
) -> Option<LuaType> {
222-
let ast = expr.syntax().parent().map(LuaAst::cast).flatten()?;
223-
224-
let typ = match ast {
222+
let parent_node = expr.syntax().parent().map(LuaAst::cast).flatten()?;
223+
let typ = match parent_node {
225224
LuaAst::LuaAssignStat(assign) => {
226225
let (vars, exprs) = assign.get_var_and_expr_list();
227226
let mut typ = None;
@@ -264,6 +263,42 @@ pub fn infer_left_value_type_from_right_value(
264263
Err(_) => Some(LuaType::Unknown),
265264
}
266265
}
266+
LuaAst::LuaCallArgList(call_arg_list) => {
267+
let call_expr = call_arg_list.get_parent::<LuaCallExpr>()?;
268+
// 获取调用位置
269+
let mut param_pos = 0;
270+
for (idx, arg) in call_arg_list.get_args().enumerate() {
271+
if arg == expr {
272+
param_pos = idx;
273+
break;
274+
}
275+
}
276+
let is_colon_call = call_expr.is_colon_call();
277+
278+
let expr_type = infer_expr(db, cache, call_expr.get_prefix_expr()?).ok()?;
279+
match expr_type {
280+
LuaType::Signature(signature_id) => {
281+
let signature = db.get_signature_index().get(&signature_id)?;
282+
match (signature.is_colon_define, is_colon_call) {
283+
(true, false) => {
284+
param_pos += 1;
285+
}
286+
(false, true) => {
287+
if param_pos == 0 {
288+
return None;
289+
}
290+
param_pos -= 1;
291+
}
292+
_ => {}
293+
}
294+
let param_info = signature.get_param_info_by_id(param_pos)?;
295+
return Some(param_info.type_ref.clone());
296+
}
297+
_ => {}
298+
}
299+
300+
return None;
301+
}
267302
_ => None,
268303
};
269304

crates/emmylua_code_analysis/src/semantic/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use emmylua_parser::{
1717
LuaCallExpr, LuaChunk, LuaExpr, LuaIndexKey, LuaParseError, LuaSyntaxNode, LuaSyntaxToken,
1818
LuaTableExpr,
1919
};
20-
use infer::{infer_left_value_type_from_right_value, infer_multi_value_adjusted_expression_types};
20+
use infer::{infer_bind_value_type, infer_multi_value_adjusted_expression_types};
2121
pub use infer::{infer_table_field_value_should_be, infer_table_should_be};
2222
use lsp_types::Uri;
2323
pub use member::get_member_map;
@@ -163,9 +163,9 @@ impl<'a> SemanticModel<'a> {
163163
)
164164
}
165165

166-
/// 从右值推断左值已绑定的类型
167-
pub fn infer_left_value_type_from_right_value(&self, expr: LuaExpr) -> Option<LuaType> {
168-
infer_left_value_type_from_right_value(self.db, &mut self.infer_cache.borrow_mut(), expr)
166+
/// 推断值已经绑定的类型(不是推断值的类型). 例如从右值推断左值类型, 从调用参数推断函数参数类型
167+
pub fn infer_bind_value_type(&self, expr: LuaExpr) -> Option<LuaType> {
168+
infer_bind_value_type(self.db, &mut self.infer_cache.borrow_mut(), expr)
169169
}
170170

171171
pub fn get_semantic_info(

0 commit comments

Comments
 (0)