Skip to content

Commit a3df30f

Browse files
committed
优化泛型诊断提示
1 parent 0cf9364 commit a3df30f

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ Cannot use `...` outside a vararg function.:
241241
en: "Duplicate index `%{name}`."
242242
zh_CN: "重复定义的索引 `%{name}`."
243243
zh_HK: "重複定義的索引 `%{name}`."
244-
"the generic constraint must be a subclass of `%{source}`. %{reason}":
245-
en: "the generic constraint must be a subclass of `%{source}`. %{reason}"
246-
zh_CN: "泛型约束要求类型为 `%{source}` 子类。%{reason}"
247-
zh_HK: "泛型約束要求類型為 `%{source}` 子類。%{reason}"
244+
"type `%{found}` does not satisfy the constraint `%{source}`. %{reason}":
245+
en: "type `%{found}` does not satisfy the constraint `%{source}`. %{reason}"
246+
zh_CN: "泛型约束要求为 `%{source}` 的子类, 但找到了 `%{found}`. %{reason}"
247+
zh_HK: "泛型约束要求为 `%{source}` 的子类, 但找到了 `%{found}`. %{reason}"
248248
"the string template type does not match any type declaration":
249249
en: "the string template type does not match any type declaration"
250250
zh_CN: "字符串模板类型与任何类型声明不匹配"

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ fn check_doc_tag_type(
5959
semantic_model,
6060
doc_type.get_range(),
6161
&extend_type,
62+
&param_type,
6263
result,
6364
);
6465
}
@@ -223,6 +224,7 @@ fn check_str_tpl_ref(
223224
semantic_model,
224225
range,
225226
&extend_type,
227+
&ref_type,
226228
result,
227229
);
228230
}
@@ -252,7 +254,14 @@ fn check_tpl_ref(
252254
let arg_info = arg_info?;
253255
let result = semantic_model.type_check(&extend_type, &arg_info.0);
254256
if !result.is_ok() {
255-
add_type_check_diagnostic(context, semantic_model, arg_info.1, &extend_type, result);
257+
add_type_check_diagnostic(
258+
context,
259+
semantic_model,
260+
arg_info.1,
261+
&extend_type,
262+
&arg_info.0,
263+
result,
264+
);
256265
}
257266
Some(())
258267
}
@@ -262,6 +271,7 @@ fn add_type_check_diagnostic(
262271
semantic_model: &SemanticModel,
263272
range: TextRange,
264273
extend_type: &LuaType,
274+
expr_type: &LuaType,
265275
result: TypeCheckResult,
266276
) {
267277
let db = semantic_model.get_db();
@@ -279,8 +289,9 @@ fn add_type_check_diagnostic(
279289
DiagnosticCode::GenericConstraintMismatch,
280290
range,
281291
t!(
282-
"the generic constraint must be a subclass of `%{source}`. %{reason}",
292+
"type `%{found}` does not satisfy the constraint `%{source}`. %{reason}",
283293
source = humanize_type(db, &extend_type, RenderLevel::Simple),
294+
found = humanize_type(db, &expr_type, RenderLevel::Simple),
284295
reason = reason_message
285296
)
286297
.to_string(),

0 commit comments

Comments
 (0)