Skip to content

Commit 36588b6

Browse files
committed
调整测试
1 parent e916ea1 commit 36588b6

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/type_def_tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub fn analyze_func_generic(analyzer: &mut DocAnalyzer, tag: LuaDocTagGeneric) -
333333
params_result.push(GenericParam::new(
334334
SmolStr::new(name.as_str()),
335335
type_ref.clone(),
336-
false,
336+
param.is_variadic(),
337337
));
338338
param_info.push((name, type_ref));
339339
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,15 @@ mod test {
407407
fn test_issue_787() {
408408
let mut ws = VirtualWorkspace::new();
409409

410-
// TODO: 我们应该删除`T...`功能, 改为泛型`T`遇到 ... 会自动收集其所有参数合并为 Tuple 类型
411410
ws.def(
412411
r#"
413412
---@class Wrapper<T>
414413
415414
---@alias UnwrapUnion<T> { [K in keyof T]: T[K] extends Wrapper<infer U> and U or unknown; }
416415
417-
---@generic T
416+
---@generic T...
418417
---@param ... T...
419-
---@return UnwrapUnion<[T...]>
418+
---@return UnwrapUnion<T>...
420419
function unwrap(...) end
421420
"#,
422421
);

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub fn instantiate_func_generic(
4747
});
4848

4949
let origin_params = func.get_params();
50-
let mut func_param_types: Vec<_> = origin_params
50+
let mut func_params: Vec<_> = origin_params
5151
.iter()
52-
.map(|(_, t)| t.clone().unwrap_or(LuaType::Unknown))
52+
.map(|(name, t)| (name.clone(), t.clone().unwrap_or(LuaType::Unknown)))
5353
.collect();
5454

5555
let arg_exprs = call_expr
@@ -72,18 +72,18 @@ pub fn instantiate_func_generic(
7272
let colon_define = func.is_colon_define();
7373
match (colon_define, colon_call) {
7474
(true, false) => {
75-
func_param_types.insert(0, LuaType::Any);
75+
func_params.insert(0, ("self".to_string(), LuaType::Any));
7676
}
7777
(false, true) => {
78-
if !func_param_types.is_empty() {
79-
func_param_types.remove(0);
78+
if !func_params.is_empty() {
79+
func_params.remove(0);
8080
}
8181
}
8282
_ => {}
8383
}
8484

8585
let mut unresolve_tpls = vec![];
86-
for i in 0..func_param_types.len() {
86+
for i in 0..func_params.len() {
8787
if i >= arg_exprs.len() {
8888
break;
8989
}
@@ -92,7 +92,7 @@ pub fn instantiate_func_generic(
9292
break;
9393
}
9494

95-
let func_param_type = &func_param_types[i];
95+
let (_, func_param_type) = &func_params[i];
9696
let call_arg_expr = &arg_exprs[i];
9797
if !func_param_type.contain_tpl() {
9898
continue;
@@ -107,6 +107,8 @@ pub fn instantiate_func_generic(
107107
}
108108

109109
let arg_type = infer_expr(db, context.cache, call_arg_expr.clone())?;
110+
dbg!(&func_param_type);
111+
dbg!(&arg_type);
110112

111113
match (func_param_type, &arg_type) {
112114
(LuaType::Variadic(variadic), _) => {
@@ -119,9 +121,14 @@ pub fn instantiate_func_generic(
119121
break;
120122
}
121123
(_, LuaType::Variadic(variadic)) => {
124+
let func_param_types = func_params[i..]
125+
.iter()
126+
.map(|(_, t)| t)
127+
.cloned()
128+
.collect::<Vec<_>>();
122129
multi_param_tpl_pattern_match_multi_return(
123130
&mut context,
124-
&func_param_types[i..],
131+
&func_param_types,
125132
variadic,
126133
)?;
127134
break;
@@ -145,6 +152,10 @@ pub fn instantiate_func_generic(
145152
substitutor.add_self_type(self_type);
146153
}
147154
if let LuaType::DocFunction(f) = instantiate_doc_function(db, func, &substitutor) {
155+
dbg!(&func);
156+
dbg!(&substitutor);
157+
dbg!(&call_expr);
158+
dbg!(&f);
148159
Ok(f.deref().clone())
149160
} else {
150161
Ok(func.clone())

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,21 @@ pub fn instantiate_doc_function(
188188
// 将 substitutor 中存储的类型的 def 转为 ref
189189
let mut modified_substitutor = substitutor.clone();
190190
modified_substitutor.convert_def_to_ref();
191-
let inst_ret_type = instantiate_type_generic(db, tpl_ret, &modified_substitutor);
191+
let mut inst_ret_type = instantiate_type_generic(db, tpl_ret, &modified_substitutor);
192+
// 对于可变返回值, 如果实例化是 tuple, 那么我们将展开 tuple
193+
if let LuaType::Variadic(_) = &&tpl_ret
194+
&& let LuaType::Tuple(tuple) = &inst_ret_type
195+
{
196+
match tuple.len() {
197+
0 => {}
198+
1 => inst_ret_type = tuple.get_types()[0].clone(),
199+
_ => {
200+
inst_ret_type =
201+
LuaType::Variadic(VariadicType::Multi(tuple.get_types().to_vec()).into())
202+
}
203+
}
204+
}
205+
192206
LuaType::DocFunction(
193207
LuaFunctionType::new(async_state, colon_define, new_params, inst_ret_type).into(),
194208
)
@@ -302,6 +316,7 @@ fn instantiate_table_generic(
302316
}
303317

304318
fn instantiate_tpl_ref(_: &DbIndex, tpl: &GenericTpl, substitutor: &TypeSubstitutor) -> LuaType {
319+
// 泛型是否以`T...`定义(非使用), 以`T...`定义的泛型我们应该将其视为一个元组
305320
if tpl.is_variadic() {
306321
if let Some(generics) = substitutor.get_variadic(tpl.get_tpl_id()) {
307322
if generics.len() == 1 {
@@ -745,6 +760,9 @@ fn instantiate_mapped_type(
745760
.type_constraint
746761
.as_ref()
747762
.map(|ty| instantiate_type_generic(db, ty, substitutor));
763+
dbg!(&mapped);
764+
dbg!(&substitutor);
765+
dbg!(&constraint);
748766

749767
if let Some(constraint) = constraint {
750768
let mut key_types = Vec::new();
@@ -777,10 +795,10 @@ fn instantiate_mapped_type(
777795
if !fields.is_empty() || !index_access.is_empty() {
778796
if constraint.is_tuple() {
779797
let types = fields.into_iter().map(|(_, ty)| ty).collect();
780-
return LuaType::Variadic(VariadicType::Multi(types).into());
781-
// return LuaType::Tuple(
782-
// LuaTupleType::new(types, LuaTupleStatus::InferResolve).into(),
783-
// );
798+
// return LuaType::Variadic(VariadicType::Multi(types).into());
799+
return LuaType::Tuple(
800+
LuaTupleType::new(types, LuaTupleStatus::InferResolve).into(),
801+
);
784802
}
785803
let field_map: HashMap<LuaMemberKey, LuaType> = fields.into_iter().collect();
786804
return LuaType::Object(LuaObjectType::new_with_fields(field_map, index_access).into());

0 commit comments

Comments
 (0)