Skip to content

Commit 3c0815b

Browse files
committed
add basic param type not match check
1 parent 131947a commit 3c0815b

File tree

14 files changed

+89
-22
lines changed

14 files changed

+89
-22
lines changed

crates/emmylua_ls/src/util/humanize_type.rs renamed to crates/code_analysis/src/db_index/type/humanize_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use code_analysis::{
1+
use crate::{
22
DbIndex, GenericTpl, LuaExistFieldType, LuaExtendedType, LuaFunctionType, LuaGenericType,
33
LuaInstanceType, LuaIntersectionType, LuaMemberKey, LuaMultiReturn, LuaObjectType,
44
LuaSignatureId, LuaStringTplType, LuaTupleType, LuaType, LuaTypeDeclId, LuaUnionType,

crates/code_analysis/src/db_index/type/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod humanize_type;
12
mod test;
23
mod type_assert;
34
mod type_decl;
@@ -10,8 +11,11 @@ use flagset::FlagSet;
1011
use rowan::TextRange;
1112
use std::collections::HashMap;
1213
pub use type_assert::TypeAssertion;
13-
pub use type_decl::{LuaDeclTypeKind, LuaTypeAttribute, LuaTypeDecl, LuaTypeDeclId, LuaDeclLocation};
14+
pub use type_decl::{
15+
LuaDeclLocation, LuaDeclTypeKind, LuaTypeAttribute, LuaTypeDecl, LuaTypeDeclId,
16+
};
1417
pub use types::*;
18+
pub use humanize_type::humanize_type;
1519

1620
#[derive(Debug)]
1721
pub struct LuaTypeIndex {

crates/code_analysis/src/diagnostic/checker/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod missing_parameter;
88
mod syntax_error;
99
mod undefined_global;
1010
mod unused;
11+
mod param_type_check;
1112

1213
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
1314
use rowan::TextRange;
@@ -45,6 +46,7 @@ pub fn check_file(
4546
check!(local_const_reassign);
4647
check!(discard_returns);
4748
check!(await_in_sync);
49+
check!(param_type_check);
4850

4951
Some(())
5052
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use emmylua_parser::{LuaAst, LuaAstNode, LuaCallExpr};
2+
3+
use crate::{humanize_type, DiagnosticCode, LuaType, SemanticModel};
4+
5+
use super::DiagnosticContext;
6+
7+
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::ParamTypeNotMatch];
8+
9+
/// a simple implementation of param type check, later we will do better
10+
pub fn check(context: &mut DiagnosticContext, semantic_model: &mut SemanticModel) -> Option<()> {
11+
let root = semantic_model.get_root().clone();
12+
for node in root.descendants::<LuaAst>() {
13+
match node {
14+
LuaAst::LuaCallExpr(call_expr) => {
15+
check_call_expr(context, semantic_model, call_expr);
16+
}
17+
_ => {}
18+
}
19+
}
20+
21+
Some(())
22+
}
23+
24+
fn check_call_expr(
25+
context: &mut DiagnosticContext,
26+
semantic_model: &mut SemanticModel,
27+
call_expr: LuaCallExpr,
28+
) -> Option<()> {
29+
let func = semantic_model.infer_call_expr_func(call_expr.clone(), None)?;
30+
let params = func.get_params();
31+
let args = call_expr.get_args_list()?.get_args().collect::<Vec<_>>();
32+
for (idx, param) in params.iter().enumerate() {
33+
if idx >= args.len() {
34+
break;
35+
}
36+
37+
if param.0 == "..." {
38+
} else {
39+
if param.1.is_none() {
40+
continue;
41+
}
42+
43+
let param_type = param.1.clone().unwrap();
44+
let arg = &args[idx];
45+
let expr_type = semantic_model
46+
.infer_expr(arg.clone())
47+
.unwrap_or(LuaType::Any);
48+
if !semantic_model.check_type_compact(&param_type, &expr_type) {
49+
let db = semantic_model.get_db();
50+
context.add_diagnostic(
51+
DiagnosticCode::ParamTypeNotMatch,
52+
arg.get_range(),
53+
format!(
54+
"expected {} but founded {}",
55+
humanize_type(db, &param_type),
56+
humanize_type(db, &expr_type)
57+
),
58+
None,
59+
);
60+
}
61+
}
62+
}
63+
64+
Some(())
65+
}

crates/code_analysis/src/diagnostic/lua_diagnostic_code.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub enum DiagnosticCode {
1414
TypeNotFound,
1515
/// Missing return statement
1616
MissingReturn,
17-
/// Type not match
18-
TypeNotMatch,
17+
/// Param Type not match
18+
ParamTypeNotMatch,
1919
/// Missing parameter
2020
MissingParameter,
2121
/// Inject field fail
@@ -63,7 +63,7 @@ pub fn get_default_severity(code: DiagnosticCode) -> DiagnosticSeverity {
6363
DiagnosticCode::SyntaxError => DiagnosticSeverity::ERROR,
6464
DiagnosticCode::TypeNotFound => DiagnosticSeverity::WARNING,
6565
DiagnosticCode::MissingReturn => DiagnosticSeverity::WARNING,
66-
DiagnosticCode::TypeNotMatch => DiagnosticSeverity::WARNING,
66+
DiagnosticCode::ParamTypeNotMatch => DiagnosticSeverity::WARNING,
6767
DiagnosticCode::MissingParameter => DiagnosticSeverity::WARNING,
6868
DiagnosticCode::InjectFieldFail => DiagnosticSeverity::ERROR,
6969
DiagnosticCode::UnreachableCode => DiagnosticSeverity::HINT,
@@ -82,7 +82,6 @@ pub fn get_default_severity(code: DiagnosticCode) -> DiagnosticSeverity {
8282

8383
pub fn is_code_default_enable(code: &DiagnosticCode) -> bool {
8484
match code {
85-
DiagnosticCode::TypeNotFound => false,
8685
DiagnosticCode::InjectFieldFail => false,
8786
DiagnosticCode::DisableGlobalDefine => false,
8887
DiagnosticCode::UndefinedField => false,

crates/code_analysis/src/semantic/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use semantic_info::{
2323
infer_node_property_owner, infer_node_semantic_info, infer_token_property_owner,
2424
infer_token_semantic_info,
2525
};
26-
use type_compact::is_sub_type_of;
26+
use type_compact::{check_type_compact, is_sub_type_of};
2727
use visibility::check_visibility;
2828

2929
use crate::LuaFunctionType;
@@ -90,6 +90,10 @@ impl<'a> SemanticModel<'a> {
9090
infer_members(self.db, prefix_type)
9191
}
9292

93+
pub fn check_type_compact(&mut self, source: &LuaType, compact_type: &LuaType) -> bool {
94+
check_type_compact(self.db, &mut self.infer_config,source, compact_type)
95+
}
96+
9397
pub fn infer_call_expr_func(
9498
&mut self,
9599
call_expr: LuaCallExpr,

crates/code_analysis/src/semantic/type_compact/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::db_index::{
1111
use super::{InferGuard, LuaInferConfig};
1212
pub use sub_type::is_sub_type_of;
1313

14-
#[allow(unused)]
1514
pub fn check_type_compact(
1615
db: &DbIndex,
1716
config: &mut LuaInferConfig,

crates/emmylua_ls/src/handlers/completion/add_completions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use lsp_types::CompletionItemKind;
88
use serde::{Deserialize, Serialize};
99
use serde_json::Value;
1010

11-
use crate::util::humanize_type;
11+
use code_analysis::humanize_type;
1212

1313
use super::completion_builder::CompletionBuilder;
1414

crates/emmylua_ls/src/handlers/completion/providers/type_special_provider.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ use emmylua_parser::{
88
};
99
use lsp_types::CompletionItem;
1010

11-
use crate::{
12-
handlers::{
13-
completion::completion_builder::CompletionBuilder,
14-
signature_helper::get_current_param_index,
15-
},
16-
util::humanize_type,
11+
use crate::handlers::{
12+
completion::completion_builder::CompletionBuilder, signature_helper::get_current_param_index,
1713
};
14+
use code_analysis::humanize_type;
1815

1916
pub fn add_completion(builder: &mut CompletionBuilder) -> Option<()> {
2017
if builder.is_cancelled() {

crates/emmylua_ls/src/handlers/hover/build_hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use code_analysis::{
55
use emmylua_parser::LuaSyntaxToken;
66
use lsp_types::{Hover, HoverContents, MarkedString, MarkupContent};
77

8-
use crate::util::humanize_type;
8+
use code_analysis::humanize_type;
99

1010
use super::hover_humanize::{hover_const_type, hover_function_type};
1111

0 commit comments

Comments
 (0)