Skip to content

Commit 6f2e7ac

Browse files
committed
reassign type allow nil
1 parent 76b113b commit 6f2e7ac

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

crates/emmylua_code_analysis/src/db_index/type/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ impl LuaType {
400400
pub fn is_member_owner(&self) -> bool {
401401
matches!(self, LuaType::Ref(_) | LuaType::TableConst(_))
402402
}
403+
404+
pub fn has_nil(&self) -> bool {
405+
match self {
406+
LuaType::Nil => true,
407+
LuaType::Union(u) => u.types.iter().any(|t| t.has_nil()),
408+
_ => false,
409+
}
410+
}
403411
}
404412

405413
#[derive(Debug, Clone, Hash, PartialEq, Eq)]

crates/emmylua_code_analysis/src/semantic/infer/infer_index.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,17 @@ fn infer_member_type_pass_flow(
9797
let root = index_expr.get_root();
9898
if let Some(path) = index_expr.get_access_path() {
9999
for type_assert in flow_chain.get_type_asserts(&path, index_expr.get_position(), None) {
100+
let new_type = type_assert
101+
.tighten_type(db, cache, &root, member_type.clone())
102+
.unwrap_or(LuaType::Unknown);
100103
if type_assert.is_reassign() && !allow_reassign {
104+
// 允许仅去除 nil
105+
if member_type.has_nil() && !new_type.is_nil() {
106+
member_type = new_type;
107+
}
101108
continue;
102109
}
103-
member_type = type_assert
104-
.tighten_type(db, cache, &root, member_type)
105-
.unwrap_or(LuaType::Unknown);
110+
member_type = new_type;
106111
}
107112
}
108113
}

0 commit comments

Comments
 (0)