Skip to content
Merged

fix #804

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn check_cast_compatibility(
return Some(());
}

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

/// 允许父类转为子类
fn cast_type_check(
semantic_model: &SemanticModel,
origin_type: &LuaType,
Expand Down Expand Up @@ -197,8 +198,13 @@ fn cast_type_check(
} else if origin_type.is_number() && target_type.is_number() {
return Ok(());
}

semantic_model.type_check_detail(target_type, origin_type)
match semantic_model.type_check_detail(target_type, origin_type) {
Ok(_) => Ok(()),
Err(_) => match semantic_model.type_check_detail(origin_type, target_type) {
Ok(_) => Ok(()),
Err(reason) => Err(reason),
},
}
Comment on lines +201 to +207

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This nested match can be simplified using Result::or_else. This would make the code more idiomatic, readable, and concise by chaining the two type check attempts.

            semantic_model.type_check_detail(target_type, origin_type)
                .or_else(|_| semantic_model.type_check_detail(origin_type, target_type))

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,30 @@ mod test {
"#
));
}

#[test]
fn test_self_contain_tpl() {
let mut ws = VirtualWorkspace::new();
ws.def_file(
"test.lua",
r#"
---@class Observable<T>
Observable = {}

---@param ... Observable<any>
function zip(...)
end

"#,
);

assert!(ws.check_code_for_namespace(
DiagnosticCode::ParamTypeNotMatch,
r#"
function Observable:test()
zip(self)
end
"#
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,15 @@ fn check_generic_type_compact_ref_type(
}
}

// 如果泛型参数是`any`, 那么我们只需要匹配基础类型
if source_generic.get_params().iter().any(|p| p.is_any()) {
return check_general_type_compact(
context,
&source_generic.get_base_type(),
&LuaType::Ref(ref_id.clone()),
check_guard.next_level()?,
);
}

Err(TypeCheckFailReason::TypeNotMatch)
}