Skip to content

Commit 71926f0

Browse files
committed
fix(diagnostic): continue checking generic constraints after unconstrained args
fix #1137
1 parent 24637d9 commit 71926f0

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,12 @@ fn check_doc_tag_type(
596596
.take(explicit_args.len())
597597
.enumerate()
598598
{
599-
let extend_type = generic_params.get(i)?.constraint.as_ref()?;
599+
let Some(extend_type) = generic_params
600+
.get(i)
601+
.and_then(|param| param.constraint.as_ref())
602+
else {
603+
continue;
604+
};
600605
let result = semantic_model.type_check_detail(extend_type, param_type);
601606
if result.is_err() {
602607
add_type_check_diagnostic(

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,40 @@ mod test {
192192
));
193193
}
194194

195+
#[test]
196+
fn test_alias_multi_generic_keyof_constraint_mismatch() {
197+
let mut ws = VirtualWorkspace::new();
198+
assert!(!ws.has_no_diagnostic(
199+
DiagnosticCode::GenericConstraintMismatch,
200+
r#"
201+
---@class A
202+
---@field one 1
203+
204+
---@alias B<T, K extends keyof A> A[K]
205+
206+
---@type B<A, 'two'>
207+
local tmp
208+
"#
209+
));
210+
}
211+
212+
#[test]
213+
fn test_alias_multi_generic_keyof_constraint_match() {
214+
let mut ws = VirtualWorkspace::new();
215+
assert!(ws.has_no_diagnostic(
216+
DiagnosticCode::GenericConstraintMismatch,
217+
r#"
218+
---@class A
219+
---@field one 1
220+
221+
---@alias B<T, K extends keyof A> A[K]
222+
223+
---@type B<A, 'one'>
224+
local tmp
225+
"#
226+
));
227+
}
228+
195229
#[test]
196230
fn test_class_generic_default_constraint_match() {
197231
let mut ws = VirtualWorkspace::new();

0 commit comments

Comments
 (0)