mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
[red-knot] Add narrowing for 'while' loops (#14947)
## Summary Add type narrowing for `while` loops and corresponding `else` branches. closes #14861 ## Test Plan New Markdown tests.
This commit is contained in:
parent
be4ce16735
commit
d7ce548893
3 changed files with 61 additions and 1 deletions
|
@ -833,6 +833,7 @@ where
|
|||
self.visit_expr(test);
|
||||
|
||||
let pre_loop = self.flow_snapshot();
|
||||
let constraint = self.record_expression_constraint(test);
|
||||
|
||||
// Save aside any break states from an outer loop
|
||||
let saved_break_states = std::mem::take(&mut self.loop_break_states);
|
||||
|
@ -852,6 +853,7 @@ where
|
|||
// We may execute the `else` clause without ever executing the body, so merge in
|
||||
// the pre-loop state before visiting `else`.
|
||||
self.flow_merge(pre_loop);
|
||||
self.record_negated_constraint(constraint);
|
||||
self.visit_body(orelse);
|
||||
|
||||
// Breaking out of a while loop bypasses the `else` clause, so merge in the break
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue