Skip to content

Commit 6abb0f7

Browse files
committed
type_check 支持 enum -> calss
1 parent ac52d6e commit 6abb0f7

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,56 @@ mod test {
704704
"#
705705
));
706706
}
707+
708+
#[test]
709+
fn test_super_and_enum_1() {
710+
let mut ws = VirtualWorkspace::new();
711+
assert!(ws.check_code_for(
712+
DiagnosticCode::ParamTypeNotMatch,
713+
r#"
714+
---@enum AbilityType
715+
local AbilityType = {
716+
HIDE = 0,
717+
NORMAL = 1,
718+
COMMON = 2,
719+
HERO = 3,
720+
}
721+
---@class py.AbilityType: integer
722+
723+
---@param ability_type py.AbilityType
724+
local function a(ability_type) end
725+
726+
---@param type AbilityType
727+
local function get(type)
728+
local py_list = a(type)
729+
end
730+
"#
731+
));
732+
}
733+
734+
#[test]
735+
fn test_super_and_enum_2() {
736+
let mut ws = VirtualWorkspace::new();
737+
assert!(!ws.check_code_for(
738+
DiagnosticCode::ParamTypeNotMatch,
739+
r#"
740+
---@enum AbilityType
741+
local AbilityType = {
742+
HIDE = "a",
743+
NORMAL = 1,
744+
COMMON = 2,
745+
HERO = 3,
746+
}
747+
---@class py.AbilityType: integer
748+
749+
---@param ability_type py.AbilityType
750+
local function a(ability_type) end
751+
752+
---@param type AbilityType
753+
local function get(type)
754+
local py_list = a(type)
755+
end
756+
"#
757+
));
758+
}
707759
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn check_ref_type_compact(
137137
for typ in union_types {
138138
match check_general_type_compact(
139139
db,
140-
compact_type,
140+
&LuaType::Ref(source_id.clone()),
141141
&typ,
142142
check_guard.next_level()?,
143143
) {
@@ -168,6 +168,29 @@ pub fn check_ref_type_compact(
168168
if is_sub_type_of(db, source_id, &compact_id) {
169169
return Ok(());
170170
}
171+
172+
// `compact`为枚举时也需要额外处理
173+
if let LuaType::Ref(compact_id) = compact_type {
174+
if let Some(compact_decl) = db.get_type_index().get_type_decl(compact_id) {
175+
if compact_decl.is_enum() {
176+
let source = LuaType::Ref(source_id.clone());
177+
if let Some(LuaType::Union(enum_fields)) = compact_decl.get_enum_field_type(db)
178+
{
179+
let is_match = enum_fields.get_types().iter().all(|field| {
180+
let next_guard = check_guard.next_level();
181+
if next_guard.is_err() {
182+
return false;
183+
}
184+
check_general_type_compact(db, &source, field, next_guard.unwrap())
185+
.is_ok()
186+
});
187+
if is_match {
188+
return Ok(());
189+
}
190+
}
191+
}
192+
}
193+
}
171194
}
172195

173196
Err(TypeCheckFailReason::TypeNotMatch)

0 commit comments

Comments
 (0)