Skip to content

Commit c322b4b

Browse files
committed
optimize str tpl check
1 parent e835c32 commit c322b4b

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ Cannot use `...` outside a vararg function.:
245245
en: "the generic constraint must be a subclass of `%{source}`. %{reason}"
246246
zh_CN: "泛型约束要求类型为 `%{source}` 子类。%{reason}"
247247
zh_HK: "泛型約束要求類型為 `%{source}` 子類。%{reason}"
248+
"the string template type does not match any type declaration":
249+
en: "the string template type does not match any type declaration"
250+
zh_CN: "字符串模板类型与任何类型声明不匹配"
251+
zh_HK: "字串模板類型與任何類型聲明不匹配"
252+
"the string template type must be a string constant":
253+
en: "the string template type must be a string constant"
254+
zh_CN: "字符串模板类型必须是字符串常量"
255+
zh_HK: "字串模板類型必須是字串常量"

crates/emmylua_code_analysis/src/diagnostic/checker/generic/generic_constraint_mismatch.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rowan::TextRange;
44
use crate::diagnostic::checker::generic::infer_type::infer_type;
55
use crate::{
66
humanize_type, DiagnosticCode, GenericTplId, LuaMemberOwner, LuaSemanticDeclId, LuaSignature,
7-
LuaStringTplType, LuaType, LuaTypeDeclId, RenderLevel, SemanticDeclLevel, SemanticModel,
8-
TypeCheckFailReason, TypeCheckResult,
7+
LuaStringTplType, LuaType, RenderLevel, SemanticDeclLevel, SemanticModel, TypeCheckFailReason,
8+
TypeCheckResult,
99
};
1010

1111
use crate::diagnostic::checker::Checker;
@@ -101,20 +101,7 @@ fn check_call_expr(
101101

102102
match param_type {
103103
LuaType::StrTplRef(str_tpl_ref) => {
104-
let extend_type = get_extend_type(
105-
semantic_model,
106-
&call_expr,
107-
str_tpl_ref.get_tpl_id(),
108-
signature,
109-
);
110-
check_str_tpl_ref(
111-
context,
112-
semantic_model,
113-
&call_expr,
114-
i,
115-
&extend_type,
116-
str_tpl_ref,
117-
);
104+
check_str_tpl_ref(context, semantic_model, &call_expr, i, str_tpl_ref);
118105
}
119106
LuaType::TplRef(tpl_ref) => {
120107
let extend_type = get_extend_type(
@@ -173,43 +160,41 @@ fn check_str_tpl_ref(
173160
semantic_model: &SemanticModel,
174161
call_expr: &LuaCallExpr,
175162
param_index: usize,
176-
extend_type: &Option<LuaType>,
177163
str_tpl_ref: &LuaStringTplType,
178164
) -> Option<()> {
179-
let extend_type = extend_type.clone()?;
180-
181165
let arg_expr = call_expr.get_args_list()?.get_args().nth(param_index)?;
182166
let arg_type = semantic_model.infer_expr(arg_expr.clone()).ok()?;
183-
/* 兼容 luals 的语法:
184-
---@generic T: string
185-
---@param name `T`
186-
*/
187-
if extend_type.is_string() && arg_type.is_string() {
188-
return Some(());
189-
}
167+
let range = arg_expr.get_range();
190168
match arg_type {
191-
LuaType::StringConst(str) => {
169+
LuaType::StringConst(str) | LuaType::DocStringConst(str) => {
192170
let full_type_name = format!(
193171
"{}{}{}",
194172
str_tpl_ref.get_prefix(),
195173
str,
196174
str_tpl_ref.get_suffix()
197175
);
198-
let result = semantic_model.type_check(
199-
&extend_type,
200-
&LuaType::Ref(LuaTypeDeclId::new(&full_type_name)),
201-
);
202-
if !result.is_ok() {
203-
add_type_check_diagnostic(
204-
context,
205-
semantic_model,
206-
arg_expr.get_range(),
207-
&extend_type,
208-
result,
176+
let founded_type_decl = semantic_model
177+
.get_db()
178+
.get_type_index()
179+
.find_type_decl(semantic_model.get_file_id(), &full_type_name);
180+
if founded_type_decl.is_none() {
181+
context.add_diagnostic(
182+
DiagnosticCode::GenericConstraintMismatch,
183+
range,
184+
t!("the string template type does not match any type declaration").to_string(),
185+
None,
209186
);
210187
}
211188
}
212-
_ => {}
189+
LuaType::String | LuaType::Any | LuaType::Unknown => {}
190+
_ => {
191+
context.add_diagnostic(
192+
DiagnosticCode::GenericConstraintMismatch,
193+
range,
194+
t!("the string template type must be a string constant").to_string(),
195+
None,
196+
);
197+
}
213198
}
214199
Some(())
215200
}

0 commit comments

Comments
 (0)