Skip to content

Commit 8408be7

Browse files
committed
update
1 parent f2211c6 commit 8408be7

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

crates/emmylua_code_analysis/src/semantic/infer/narrow/condition_flow/binary_flow.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn maybe_type_guard_binary(
191191

192192
let result_type = match condition_flow {
193193
InferConditionFlow::TrueCondition => {
194-
narrow_down_type(db, antecedent_type.clone(), narrow).unwrap_or(antecedent_type)
194+
narrow_down_type(db, antecedent_type.clone(), narrow.clone()).unwrap_or(narrow)
195195
}
196196
InferConditionFlow::FalseCondition => TypeOps::Remove.apply(db, &antecedent_type, &narrow),
197197
};
@@ -231,8 +231,8 @@ fn maybe_var_eq_narrow(
231231

232232
let result_type = match condition_flow {
233233
InferConditionFlow::TrueCondition => {
234-
narrow_down_type(db, antecedent_type.clone(), right_expr_type)
235-
.unwrap_or(antecedent_type)
234+
narrow_down_type(db, antecedent_type, right_expr_type.clone())
235+
.unwrap_or(right_expr_type)
236236
}
237237
InferConditionFlow::FalseCondition => {
238238
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)
@@ -287,8 +287,8 @@ fn maybe_var_eq_narrow(
287287

288288
let result_type = match condition_flow {
289289
InferConditionFlow::TrueCondition => {
290-
narrow_down_type(db, antecedent_type.clone(), right_expr_type)
291-
.unwrap_or(antecedent_type)
290+
narrow_down_type(db, antecedent_type, right_expr_type.clone())
291+
.unwrap_or(right_expr_type)
292292
}
293293
InferConditionFlow::FalseCondition => {
294294
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)

crates/emmylua_code_analysis/src/semantic/infer/narrow/get_type_at_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn get_type_at_assign_stat(
214214
get_type_at_flow(db, tree, cache, root, var_ref_id, antecedent_flow_id)?;
215215

216216
return Ok(ResultTypeOrContinue::Result(
217-
narrow_down_type(db, antecedent_type.clone(), expr_type).unwrap_or(antecedent_type),
217+
narrow_down_type(db, antecedent_type, expr_type.clone()).unwrap_or(expr_type),
218218
));
219219
}
220220

crates/emmylua_code_analysis/src/semantic/infer/narrow/narrow_type/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod false_or_nil_type;
22

3-
use crate::{get_real_type, DbIndex, LuaType, LuaUnionType, TypeOps};
3+
use crate::{
4+
get_real_type, semantic::type_check::is_sub_type_of, DbIndex, LuaType, LuaUnionType, TypeOps,
5+
};
46
pub use false_or_nil_type::{narrow_false_or_nil, remove_false_or_nil};
57

68
// need to be optimized
@@ -10,7 +12,6 @@ pub fn narrow_down_type(db: &DbIndex, source: LuaType, target: LuaType) -> Optio
1012
}
1113

1214
let real_source_ref = get_real_type(db, &source).unwrap_or(&source);
13-
let mut opt_result = None;
1415
match &target {
1516
LuaType::Number => {
1617
if real_source_ref.is_number() {
@@ -160,9 +161,16 @@ pub fn narrow_down_type(db: &DbIndex, source: LuaType, target: LuaType) -> Optio
160161
return Some(result_type);
161162
}
162163
LuaType::Variadic(_) => return Some(source),
163-
_ => {
164-
opt_result = Some(source.clone());
165-
}
164+
LuaType::Def(type_id) | LuaType::Ref(type_id) => match real_source_ref {
165+
LuaType::Def(ref_id) | LuaType::Ref(ref_id) => {
166+
if is_sub_type_of(db, ref_id, type_id) || is_sub_type_of(db, type_id, ref_id) {
167+
return Some(source);
168+
}
169+
}
170+
_ => {}
171+
},
172+
173+
_ => {}
166174
}
167175

168176
match real_source_ref {
@@ -183,5 +191,5 @@ pub fn narrow_down_type(db: &DbIndex, source: LuaType, target: LuaType) -> Optio
183191
_ => {}
184192
}
185193

186-
opt_result
194+
None
187195
}

0 commit comments

Comments
 (0)