Skip to content

Commit bfc5295

Browse files
committed
update
1 parent 1859613 commit bfc5295

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

crates/emmylua_code_analysis/src/compilation/analyzer/flow/bind_analyze/stats.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use emmylua_parser::{
77
use crate::{
88
compilation::analyzer::flow::{
99
bind_analyze::{
10-
bind_block, bind_each_child,
11-
exprs::{bind_condition_expr, bind_expr},
12-
finish_flow_label,
10+
bind_block, bind_each_child, bind_node, exprs::{bind_condition_expr, bind_expr}, finish_flow_label
1311
},
1412
binder::FlowBinder,
1513
},
@@ -32,6 +30,11 @@ pub fn bind_local_stat(
3230
binder.decl_bind_flow_ref.insert(decl_id, flow_id);
3331
}
3432

33+
for value in values {
34+
// If there are more values than names, we still need to bind the values
35+
bind_expr(binder, value.clone(), current);
36+
}
37+
3538
let local_flow_id = binder.create_decl(local_stat.get_position());
3639
binder.add_antecedent(local_flow_id, current);
3740
local_flow_id
@@ -46,13 +49,13 @@ pub fn bind_assign_stat(
4649
// First bind the right-hand side expressions
4750
for expr in &values {
4851
if let Some(ast) = LuaAst::cast(expr.syntax().clone()) {
49-
bind_each_child(binder, ast, current);
52+
bind_node(binder, ast, current);
5053
}
5154
}
5255

5356
for var in &vars {
5457
if let Some(ast) = LuaAst::cast(var.syntax().clone()) {
55-
bind_each_child(binder, ast, current);
58+
bind_node(binder, ast, current);
5659
}
5760
}
5861

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn maybe_var_eq_narrow(
234234
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)
235235
}
236236
};
237-
return Ok(ResultTypeOrContinue::Result(result_type));
237+
Ok(ResultTypeOrContinue::Result(result_type))
238238
}
239239
LuaExpr::CallExpr(left_call_expr) => {
240240
match right_expr {
@@ -262,16 +262,38 @@ fn maybe_var_eq_narrow(
262262
_ => {}
263263
};
264264

265-
return Ok(ResultTypeOrContinue::Continue);
265+
Ok(ResultTypeOrContinue::Continue)
266266
}
267-
LuaExpr::IndexExpr(_) => {
268-
// todo
267+
LuaExpr::IndexExpr(left_index_expr) => {
268+
let Some(maybe_ref_id) =
269+
get_var_expr_var_ref_id(db, cache, LuaExpr::IndexExpr(left_index_expr.clone()))
270+
else {
271+
return Ok(ResultTypeOrContinue::Continue);
272+
};
273+
274+
if maybe_ref_id != *var_ref_id {
275+
// If the reference declaration ID does not match, we cannot narrow it
276+
return Ok(ResultTypeOrContinue::Continue);
277+
}
278+
279+
let right_expr_type = infer_expr(db, cache, right_expr)?;
280+
let antecedent_flow_id = get_single_antecedent(tree, flow_node)?;
281+
let antecedent_type =
282+
get_type_at_flow(db, tree, cache, root, &var_ref_id, antecedent_flow_id)?;
283+
284+
let result_type = match condition_flow {
285+
InferConditionFlow::TrueCondition => {
286+
TypeOps::Narrow.apply(db, &antecedent_type, &right_expr_type)
287+
}
288+
InferConditionFlow::FalseCondition => {
289+
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)
290+
}
291+
};
292+
Ok(ResultTypeOrContinue::Result(result_type))
269293
}
270294
_ => {
271295
// If the left expression is not a name or call expression, we cannot narrow it
272-
return Ok(ResultTypeOrContinue::Continue);
296+
Ok(ResultTypeOrContinue::Continue)
273297
}
274298
}
275-
276-
Ok(ResultTypeOrContinue::Continue)
277299
}

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
@@ -180,7 +180,7 @@ fn get_type_at_assign_stat(
180180
continue;
181181
};
182182

183-
if maybe_ref_id == *var_ref_id {
183+
if maybe_ref_id != *var_ref_id {
184184
// let typ = get_var_ref_type(db, cache, var_ref_id)?;
185185
continue;
186186
}

0 commit comments

Comments
 (0)