Skip to content

Commit c64c7d6

Browse files
[ty] Model try exception flow with operation checkpoints (#27471)
## Summary Previously, we treated every definition inside a `try` block as a possible exception point, allowing handlers to observe states that could never occur: ```py def may_raise(value: object) -> None: ... state = 0 try: may_raise(state := 1) except: reveal_type(state) # Before: Literal[0, 1] # After: Literal[1] ``` We now record exception checkpoints immediately before operations that can raise, after evaluating their operands. This includes calls, imports, name lookups, attribute access, operators, iteration, and other potentially raising operations. (This is based on Carl's draft: #27221.) --------- Co-authored-by: Carl Meyer <[email protected]>
1 parent 9dea5ef commit c64c7d6

10 files changed

Lines changed: 1719 additions & 272 deletions

File tree

‎crates/ruff_benchmark/benches/ty.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,26 @@ fn benchmark_repeated_statement_calls(criterion: &mut Criterion) {
13581358
);
13591359
});
13601360
}
1361+
1362+
let mut code = String::from("def f(value: str) -> None:\n");
1363+
for index in 0..800 {
1364+
writeln!(&mut code, " local_{index} = {index}").ok();
1365+
}
1366+
code.push_str(" try:\n");
1367+
code.push_str(&" value.upper()\n".repeat(800));
1368+
code.push_str(" except Exception:\n pass\n");
1369+
1370+
criterion.bench_function("ty_micro[repeated_statement_calls_in_try]", |b| {
1371+
b.iter_batched_ref(
1372+
|| setup_micro_case(&code),
1373+
|case| {
1374+
let Case { db } = case;
1375+
let result = db.check();
1376+
assert_eq!(result.len(), 0);
1377+
},
1378+
BatchSize::SmallInput,
1379+
);
1380+
});
13611381
}
13621382

13631383
struct ProjectBenchmark<'a> {

‎crates/ty_python_core/src/builder.rs‎

Lines changed: 359 additions & 60 deletions
Large diffs are not rendered by default.

‎crates/ty_python_core/src/builder/except_handlers.rs‎

Lines changed: 156 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
use crate::use_def::FlowSnapshot;
1+
use crate::reachability_constraints::ScopedReachabilityConstraintId;
2+
use crate::use_def::{ControlFlowRevision, FlowSnapshot, ScopedDefinitionId, UseDefMapBuilder};
23

34
use super::SemanticIndexBuilder;
45

6+
/// Active exception handlers and the flow states from which they can be entered.
7+
#[derive(Debug, Default)]
8+
pub(super) enum ExceptionHandlers {
9+
/// No handlers are active, including after their snapshots have been taken.
10+
#[default]
11+
None,
12+
/// Handlers may catch the exception, but it can also propagate to an enclosing handler.
13+
Propagating(Vec<FlowSnapshot>),
14+
/// A bare handler catches every exception and stops outward propagation.
15+
CatchAll(Vec<FlowSnapshot>),
16+
}
17+
18+
impl ExceptionHandlers {
19+
pub(super) fn propagating() -> Self {
20+
Self::Propagating(Vec::new())
21+
}
22+
23+
pub(super) fn catch_all() -> Self {
24+
Self::CatchAll(Vec::new())
25+
}
26+
27+
fn is_active(&self) -> bool {
28+
!matches!(self, Self::None)
29+
}
30+
31+
fn is_catch_all(&self) -> bool {
32+
matches!(self, Self::CatchAll(_))
33+
}
34+
}
35+
536
/// An abstraction over the fact that each scope should have its own [`TryNodeContextStack`]
637
#[derive(Debug, Default)]
738
pub(super) struct TryNodeContextStackManager(Vec<TryNodeContextStack>);
@@ -26,10 +57,14 @@ impl TryNodeContextStackManager {
2657
);
2758
}
2859

29-
/// Push a [`TryNodeContext`] onto the [`TryNodeContextStack`]
30-
/// at the top of our stack of stacks
31-
pub(super) fn push_context(&mut self) {
32-
self.current_try_context_stack().push_context();
60+
/// Push a [`TryNodeContext`] onto the [`TryNodeContextStack`] at the top of our stack of
61+
/// stacks.
62+
///
63+
/// Only suites with handlers collect exception checkpoints; a bare handler prevents those
64+
/// exceptions from propagating to enclosing suites.
65+
pub(super) fn push_context(&mut self, exception_handlers: ExceptionHandlers) {
66+
self.current_try_context_stack()
67+
.push_context(exception_handlers);
3368
}
3469

3570
/// Pop a [`TryNodeContext`] off the [`TryNodeContextStack`] at the top of our stack of stacks.
@@ -39,14 +74,57 @@ impl TryNodeContextStackManager {
3974

4075
/// Retrieve the [`TryNodeContext`] that is currently at the top of the stack, and take all
4176
/// snapshots recorded while visiting the `try` suite.
42-
pub(super) fn take_try_suite_snapshots(&mut self) -> Vec<FlowSnapshot> {
43-
self.current_try_context_stack().take_try_suite_snapshots()
77+
///
78+
/// Taking the snapshots deactivates the suite's handlers before their bodies are visited.
79+
pub(super) fn end_try_suite(&mut self) -> Vec<FlowSnapshot> {
80+
self.current_try_context_stack().end_try_suite()
4481
}
4582

46-
/// Retrieve the stack that is at the top of our stack of stacks.
47-
/// For each `try` block on that stack, push the snapshot onto the `try` block
48-
pub(super) fn record_definition(&mut self, builder: &SemanticIndexBuilder) {
49-
self.current_try_context_stack().record_definition(builder);
83+
/// Record a checkpoint for every active `try` suite that could handle an exception raised at
84+
/// the current point in control flow.
85+
///
86+
/// Crosses eager scopes, but stops at lazy scopes, unreachable flow, and bare handlers.
87+
pub(super) fn record_exception_checkpoint(&mut self, builder: &mut SemanticIndexBuilder) {
88+
debug_assert_eq!(self.0.len(), builder.scope_stack.len());
89+
90+
for (scope_stack_index, try_context_stack) in self.0.iter_mut().enumerate().rev() {
91+
let scope_id = builder.scope_stack[scope_stack_index].file_scope_id;
92+
let use_def_map = &builder.use_def_maps[scope_id];
93+
94+
// Each scope has an independent flow state, so an enclosing scope can still be
95+
// reachable while we analyze an unreachable nested scope.
96+
if use_def_map.reachability == ScopedReachabilityConstraintId::ALWAYS_FALSE {
97+
break;
98+
}
99+
100+
if !try_context_stack.record_exception_checkpoint(use_def_map) {
101+
break;
102+
}
103+
104+
if !builder.exception_checkpoint_crosses_scope_boundary(scope_id) {
105+
break;
106+
}
107+
}
108+
}
109+
110+
/// Returns whether an active `try` suite can receive an exception from the current scope.
111+
///
112+
/// A context can remain on the stack for its `finally` suite after its handlers become inactive.
113+
pub(super) fn has_active_exception_handler(&self, builder: &SemanticIndexBuilder) -> bool {
114+
debug_assert_eq!(self.0.len(), builder.scope_stack.len());
115+
116+
for (scope_stack_index, try_context_stack) in self.0.iter().enumerate().rev() {
117+
if try_context_stack.has_active_exception_handler() {
118+
return true;
119+
}
120+
121+
let scope_id = builder.scope_stack[scope_stack_index].file_scope_id;
122+
if !builder.exception_checkpoint_crosses_scope_boundary(scope_id) {
123+
return false;
124+
}
125+
}
126+
127+
false
50128
}
51129

52130
/// Retrieve the stack that is at the top of our stack of stacks.
@@ -70,10 +148,20 @@ impl TryNodeContextStackManager {
70148
struct TryNodeContextStack(Vec<TryNodeContext>);
71149

72150
impl TryNodeContextStack {
73-
/// Push a new [`TryNodeContext`] for recording intermediate states
74-
/// while visiting a [`ruff_python_ast::StmtTry`] node that has a `finally` branch.
75-
fn push_context(&mut self) {
76-
self.0.push(TryNodeContext::default());
151+
/// Returns whether a `try` suite in this scope is still collecting exception checkpoints.
152+
fn has_active_exception_handler(&self) -> bool {
153+
self.0
154+
.iter()
155+
.any(|context| context.exception_handlers.is_active())
156+
}
157+
158+
/// Push a new [`TryNodeContext`] for recording exception checkpoints and terminal entries
159+
/// while visiting a [`ruff_python_ast::StmtTry`] node.
160+
fn push_context(&mut self, exception_handlers: ExceptionHandlers) {
161+
self.0.push(TryNodeContext {
162+
exception_handlers,
163+
..TryNodeContext::default()
164+
});
77165
}
78166

79167
/// Pop a [`TryNodeContext`] off the stack.
@@ -83,22 +171,51 @@ impl TryNodeContextStack {
83171
.expect("Cannot pop a `try` block off an empty `TryBlockContexts` stack")
84172
}
85173

86-
/// Take all snapshots recorded while visiting the `try` suite.
87-
fn take_try_suite_snapshots(&mut self) -> Vec<FlowSnapshot> {
88-
std::mem::take(
89-
&mut self
90-
.0
91-
.last_mut()
92-
.expect("Cannot take snapshots from an empty `TryBlockContexts` stack")
93-
.try_suite_snapshots,
94-
)
174+
/// Take all snapshots recorded while visiting the `try` suite and deactivate its handlers.
175+
fn end_try_suite(&mut self) -> Vec<FlowSnapshot> {
176+
let context = self
177+
.0
178+
.last_mut()
179+
.expect("Cannot take snapshots from an empty `TryBlockContexts` stack");
180+
match std::mem::take(&mut context.exception_handlers) {
181+
ExceptionHandlers::None => Vec::new(),
182+
ExceptionHandlers::Propagating(snapshots) | ExceptionHandlers::CatchAll(snapshots) => {
183+
snapshots
184+
}
185+
}
95186
}
96187

97-
/// For each `try` block on the stack, push the snapshot onto the `try` block
98-
fn record_definition(&mut self, builder: &SemanticIndexBuilder) {
99-
for context in &mut self.0 {
100-
context.record_definition(builder.flow_snapshot());
188+
/// Records the checkpoint for all enclosing active `try` suites in this scope. Returns whether
189+
/// the checkpoint should continue propagating to an enclosing scope.
190+
///
191+
/// A bare handler consumes the exception, preventing any outer handler from seeing it. The
192+
/// snapshot is constructed only if a handler has not already observed the current flow state.
193+
fn record_exception_checkpoint(&mut self, use_def_map: &UseDefMapBuilder<'_>) -> bool {
194+
let checkpoint_key = use_def_map.exception_checkpoint_key();
195+
let mut snapshot = None;
196+
197+
for context in self.0.iter_mut().rev() {
198+
match &mut context.exception_handlers {
199+
ExceptionHandlers::None => context.has_escaping_exception = true,
200+
ExceptionHandlers::Propagating(snapshots)
201+
| ExceptionHandlers::CatchAll(snapshots) => {
202+
if context.last_checkpoint_key != Some(checkpoint_key) {
203+
snapshots.push(
204+
snapshot
205+
.get_or_insert_with(|| use_def_map.snapshot())
206+
.clone(),
207+
);
208+
context.last_checkpoint_key = Some(checkpoint_key);
209+
}
210+
if context.exception_handlers.is_catch_all() {
211+
return false;
212+
}
213+
context.has_escaping_exception = true;
214+
}
215+
}
101216
}
217+
218+
true
102219
}
103220

104221
/// Push the snapshot onto the innermost `try` block's terminal-entry snapshots for its
@@ -110,25 +227,26 @@ impl TryNodeContextStack {
110227
}
111228
}
112229

113-
/// Context for tracking definitions over the course of a single
114-
/// [`ruff_python_ast::StmtTry`] node
230+
/// Context for tracking exception and `finally` entry states for a single
231+
/// [`ruff_python_ast::StmtTry`] node.
115232
///
116233
/// It will likely be necessary to add more fields to this struct in the future
117234
/// when we add more advanced handling of `finally` branches.
118235
#[derive(Debug, Default)]
119236
pub(super) struct TryNodeContext {
120-
try_suite_snapshots: Vec<FlowSnapshot>,
237+
exception_handlers: ExceptionHandlers,
238+
last_checkpoint_key: Option<(ScopedDefinitionId, ControlFlowRevision)>,
239+
/// Whether an exception escaped this suite and must also propagate after its cleanup.
240+
has_escaping_exception: bool,
121241
terminal_finally_entry_snapshots: Vec<FlowSnapshot>,
122242
}
123243

124244
impl TryNodeContext {
125-
pub(super) fn into_terminal_finally_entry_snapshots(self) -> Vec<FlowSnapshot> {
126-
self.terminal_finally_entry_snapshots
127-
}
128-
129-
/// Take a record of what the internal state looked like after a definition
130-
fn record_definition(&mut self, snapshot: FlowSnapshot) {
131-
self.try_suite_snapshots.push(snapshot);
245+
pub(super) fn into_finally_entry_state(self) -> (Vec<FlowSnapshot>, bool) {
246+
(
247+
self.terminal_finally_entry_snapshots,
248+
self.has_escaping_exception,
249+
)
132250
}
133251

134252
/// Take a record of what the internal state looked like before a terminal control-flow

‎crates/ty_python_core/src/use_def.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,16 @@ pub(super) struct SingleSymbolSnapshot {
17571757
associated_member_states: FxHashMap<ScopedMemberId, PlaceState>,
17581758
}
17591759

1760+
/// Identifies a control-flow path within a single scope.
1761+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
1762+
pub(super) struct ControlFlowRevision(u64);
1763+
1764+
impl ControlFlowRevision {
1765+
fn advance(&mut self) {
1766+
self.0 += 1;
1767+
}
1768+
}
1769+
17601770
#[derive(Debug)]
17611771
pub(super) struct UseDefMapBuilder<'db> {
17621772
/// Append-only array of [`DefinitionState`].
@@ -1794,6 +1804,12 @@ pub(super) struct UseDefMapBuilder<'db> {
17941804
/// keyed by their text range.
17951805
range_reachability: Vec<(TextRange, RangeInfo)>,
17961806

1807+
/// Distinguishes control-flow paths that can have the same set of recorded definitions.
1808+
///
1809+
/// This only moves forward when restoring switches paths or merging widens the current path.
1810+
/// Revisions are not restored from snapshots, so separate branches cannot appear equivalent.
1811+
control_flow_revision: ControlFlowRevision,
1812+
17971813
/// Live bindings for each so-far-recorded definition and, for binding-only definitions, the
17981814
/// live declarations.
17991815
definitions_by_definition:
@@ -1836,6 +1852,7 @@ impl<'db> UseDefMapBuilder<'db> {
18361852
multi_bindings_by_use: FxHashMap::default(),
18371853
reachability: ScopedReachabilityConstraintId::ALWAYS_TRUE,
18381854
range_reachability: Vec::new(),
1855+
control_flow_revision: ControlFlowRevision::default(),
18391856
definitions_by_definition: FxHashMap::default(),
18401857
symbol_states: IndexVec::new(),
18411858
member_states: IndexVec::new(),
@@ -1908,6 +1925,11 @@ impl<'db> UseDefMapBuilder<'db> {
19081925
self.all_definitions.next_index()
19091926
}
19101927

1928+
/// Identifies the latest definitions and control-flow path observed by an exception handler.
1929+
pub(super) fn exception_checkpoint_key(&self) -> (ScopedDefinitionId, ControlFlowRevision) {
1930+
(self.next_definition_id(), self.control_flow_revision)
1931+
}
1932+
19111933
pub(super) fn record_binding(
19121934
&mut self,
19131935
place: ScopedPlaceId,
@@ -2680,6 +2702,7 @@ impl<'db> UseDefMapBuilder<'db> {
26802702

26812703
/// Restore the current builder places state to the given snapshot.
26822704
pub(super) fn restore(&mut self, snapshot: FlowSnapshot) {
2705+
self.control_flow_revision.advance();
26832706
// We never remove places from `place_states` (it's an IndexVec, and the place
26842707
// IDs must line up), so the current number of known places must always be equal to or
26852708
// greater than the number of known places in a previously-taken snapshot.
@@ -2723,6 +2746,7 @@ impl<'db> UseDefMapBuilder<'db> {
27232746
return;
27242747
}
27252748

2749+
self.control_flow_revision.advance();
27262750
// We never remove places from `place_states` (it's an IndexVec, and the place
27272751
// IDs must line up), so the current number of known places must always be equal to or
27282752
// greater than the number of known places in a previously-taken snapshot.

‎crates/ty_python_semantic/resources/mdtest/exception/basic.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ def silence4[T: type[BaseException] | tuple[type[BaseException], ...]](
171171

172172
```py
173173
try:
174-
pass
174+
raise Exception
175175
# error: [invalid-exception-caught]
176176
except 3 as e:
177177
reveal_type(e) # revealed: Unknown
178178

179179
try:
180-
pass
180+
raise Exception
181181
# error: [invalid-exception-caught]
182182
except (ValueError, OSError, "foo", b"bar") as e:
183183
reveal_type(e) # revealed: ValueError | OSError | Unknown

0 commit comments

Comments
 (0)