Skip to content

Commit d8156ce

Browse files
committed
update generic infer
1 parent f0fed01 commit d8156ce

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,38 @@ mod test {
125125
"#
126126
));
127127
}
128+
129+
#[test]
130+
fn test_generic_infer_function() {
131+
let mut ws = VirtualWorkspace::new();
132+
ws.def(
133+
r#"
134+
---@alias Parameters<T extends function> T extends (fun(...: infer P): any) and P or never
135+
136+
---@alias Procedure fun(...: any[]): any
137+
138+
---@alias MockParameters<T> T extends Procedure and Parameters<T> or never
139+
140+
---@class Mock<T>
141+
---@field calls MockParameters<T>[]
142+
---@overload fun(...: MockParameters<T>...)
143+
144+
---@generic T: Procedure
145+
---@param a T
146+
---@return Mock<T>
147+
function fn(a)
148+
end
149+
150+
sum = fn(function(a, b)
151+
return a + b
152+
end)
153+
"#,
154+
);
155+
assert!(!ws.check_code_for(
156+
DiagnosticCode::RedundantParameter,
157+
r#"
158+
sum(1, 2, 3)
159+
"#
160+
));
161+
}
128162
}

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@ fn collect_infer_assignments(
634634
};
635635
let mut rest_types = Vec::with_capacity(rest.len());
636636
for (_, source_param) in rest {
637-
let Some(source_ty) = source_param.as_ref() else {
638-
return false;
639-
};
640-
rest_types.push(source_ty.clone());
637+
// 如果来源没有类型, 那么将其设为 Any 而不是 Never
638+
rest_types.push(
639+
source_param.as_ref().unwrap_or(&LuaType::Any).clone(),
640+
);
641641
}
642642
let ty = match rest_types.len() {
643643
0 => LuaType::Never,

0 commit comments

Comments
 (0)