Skip to content

Commit d9e4802

Browse files
committed
Expand tuple types with rest parameters in generic instantiation
Expand tuple types when encountering rest parameters (`...`) during generic instantiation close #738
1 parent 4c50b71 commit d9e4802

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

crates/emmylua_code_analysis/src/compilation/test/generic_test.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(test)]
22
mod test {
3-
use crate::VirtualWorkspace;
3+
use crate::{DiagnosticCode, VirtualWorkspace};
44

55
#[test]
66
fn test_issue_586() {
@@ -158,4 +158,17 @@ mod test {
158158
assert_eq!(a_ty, LuaType::Unknown);
159159
}
160160
*/
161+
162+
#[test]
163+
fn test_issue_738() {
164+
let mut ws = VirtualWorkspace::new();
165+
ws.def(
166+
r#"
167+
---@alias Predicate<A: any[]> fun(...: A): boolean
168+
---@type Predicate<[string, integer, table]>
169+
pred = function() end
170+
"#,
171+
);
172+
assert!(ws.check_code_for(DiagnosticCode::ParamTypeNotMatch, r#"pred('hello', 1, {})"#));
173+
}
161174
}

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type_generic.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ pub fn instantiate_doc_function(
161161
},
162162
_ => {
163163
let new_type = instantiate_type_generic(db, origin_param_type, substitutor);
164+
// 如果是 rest 参数且实例化后的类型是 tuple, 那么我们将展开 tuple
165+
if origin_param.0 == "..." && tpl_func_params.len() == i + 1 {
166+
if let LuaType::Tuple(tuple_type) = &new_type {
167+
let base_index = new_params.len();
168+
for (offset, tuple_element) in tuple_type.get_types().iter().enumerate() {
169+
let param_name = format!("args_{}", base_index + offset);
170+
new_params.push((param_name, Some(tuple_element.clone())));
171+
}
172+
continue;
173+
}
174+
}
164175
new_params.push((origin_param.0.clone(), Some(new_type)));
165176
}
166177
}

0 commit comments

Comments
 (0)