Skip to content

Commit 690fd58

Browse files
committed
Fix #641
1 parent e95ed36 commit 690fd58

File tree

2 files changed

+26
-16
lines changed
  • crates/emmylua_code_analysis/src

2 files changed

+26
-16
lines changed

crates/emmylua_code_analysis/src/compilation/test/flow.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,22 @@ end
12401240
"#,
12411241
));
12421242
}
1243+
1244+
#[test]
1245+
fn test_issue_641() {
1246+
let mut ws = VirtualWorkspace::new_with_init_std_lib();
1247+
assert!(ws.check_code_for(
1248+
DiagnosticCode::AssignTypeMismatch,
1249+
r#"
1250+
local b --- @type boolean
1251+
local tar = b and 'a' or 'b'
1252+
1253+
if tar == 'a' then
1254+
end
1255+
1256+
--- @type 'a'|'b'
1257+
local _ = tar
1258+
"#,
1259+
));
1260+
}
12431261
}

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,12 @@ fn maybe_var_eq_narrow(
237237
}
238238

239239
let right_expr_type = infer_expr(db, cache, right_expr)?;
240-
let antecedent_flow_id = get_single_antecedent(tree, flow_node)?;
241-
let antecedent_type =
242-
get_type_at_flow(db, tree, cache, root, &var_ref_id, antecedent_flow_id)?;
243-
244240
let result_type = match condition_flow {
245-
InferConditionFlow::TrueCondition => {
246-
narrow_down_type(db, antecedent_type, right_expr_type.clone())
247-
.unwrap_or(right_expr_type)
248-
}
241+
InferConditionFlow::TrueCondition => right_expr_type,
249242
InferConditionFlow::FalseCondition => {
243+
let antecedent_flow_id = get_single_antecedent(tree, flow_node)?;
244+
let antecedent_type =
245+
get_type_at_flow(db, tree, cache, root, &var_ref_id, antecedent_flow_id)?;
250246
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)
251247
}
252248
};
@@ -293,16 +289,12 @@ fn maybe_var_eq_narrow(
293289
}
294290

295291
let right_expr_type = infer_expr(db, cache, right_expr)?;
296-
let antecedent_flow_id = get_single_antecedent(tree, flow_node)?;
297-
let antecedent_type =
298-
get_type_at_flow(db, tree, cache, root, &var_ref_id, antecedent_flow_id)?;
299-
300292
let result_type = match condition_flow {
301-
InferConditionFlow::TrueCondition => {
302-
narrow_down_type(db, antecedent_type, right_expr_type.clone())
303-
.unwrap_or(right_expr_type)
304-
}
293+
InferConditionFlow::TrueCondition => right_expr_type,
305294
InferConditionFlow::FalseCondition => {
295+
let antecedent_flow_id = get_single_antecedent(tree, flow_node)?;
296+
let antecedent_type =
297+
get_type_at_flow(db, tree, cache, root, &var_ref_id, antecedent_flow_id)?;
306298
TypeOps::Remove.apply(db, &antecedent_type, &right_expr_type)
307299
}
308300
};

0 commit comments

Comments
 (0)