Skip to content

Commit a218090

Browse files
authored
Merge pull request #804 from xuhuanzy/fix
fix
2 parents fd858f5 + 6fd49cf commit a218090

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

crates/emmylua_code_analysis/src/diagnostic/checker/cast_type_mismatch.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn check_cast_compatibility(
7070
return Some(());
7171
}
7272

73-
// 检查是否可以从原始类型转换为目标类型
73+
// 检查是否可以从原始类型转换为目标类型, 允许父类转为子类
7474
let result = match origin_type {
7575
LuaType::Union(union_type) => {
7676
for member_type in union_type.into_vec() {
@@ -137,6 +137,7 @@ fn add_cast_type_mismatch_diagnostic(
137137
}
138138
}
139139

140+
/// 允许父类转为子类
140141
fn cast_type_check(
141142
semantic_model: &SemanticModel,
142143
origin_type: &LuaType,
@@ -197,8 +198,13 @@ fn cast_type_check(
197198
} else if origin_type.is_number() && target_type.is_number() {
198199
return Ok(());
199200
}
200-
201-
semantic_model.type_check_detail(target_type, origin_type)
201+
match semantic_model.type_check_detail(target_type, origin_type) {
202+
Ok(_) => Ok(()),
203+
Err(_) => match semantic_model.type_check_detail(origin_type, target_type) {
204+
Ok(_) => Ok(()),
205+
Err(reason) => Err(reason),
206+
},
207+
}
202208
}
203209
}
204210
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,4 +1308,30 @@ mod test {
13081308
"#
13091309
));
13101310
}
1311+
1312+
#[test]
1313+
fn test_self_contain_tpl() {
1314+
let mut ws = VirtualWorkspace::new();
1315+
ws.def_file(
1316+
"test.lua",
1317+
r#"
1318+
---@class Observable<T>
1319+
Observable = {}
1320+
1321+
---@param ... Observable<any>
1322+
function zip(...)
1323+
end
1324+
1325+
"#,
1326+
);
1327+
1328+
assert!(ws.check_code_for_namespace(
1329+
DiagnosticCode::ParamTypeNotMatch,
1330+
r#"
1331+
function Observable:test()
1332+
zip(self)
1333+
end
1334+
"#
1335+
));
1336+
}
13111337
}

crates/emmylua_code_analysis/src/semantic/type_check/generic_type.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,15 @@ fn check_generic_type_compact_ref_type(
284284
}
285285
}
286286

287+
// 如果泛型参数是`any`, 那么我们只需要匹配基础类型
288+
if source_generic.get_params().iter().any(|p| p.is_any()) {
289+
return check_general_type_compact(
290+
context,
291+
&source_generic.get_base_type(),
292+
&LuaType::Ref(ref_id.clone()),
293+
check_guard.next_level()?,
294+
);
295+
}
296+
287297
Err(TypeCheckFailReason::TypeNotMatch)
288298
}

0 commit comments

Comments
 (0)