|
1 | | -use crate::semantic_index::use_def::FlowSnapshot; |
| 1 | +use crate::semantic_index::use_def::{FlowSnapshot, UseDefMapBuilder}; |
2 | 2 |
|
3 | 3 | /// An abstraction over the fact that each scope should have its own [`TryNodeContextStack`] |
4 | 4 | #[derive(Debug, Default)] |
@@ -37,22 +37,17 @@ impl TryNodeContextStackManager { |
37 | 37 | self.current_try_context_stack().pop_context() |
38 | 38 | } |
39 | 39 |
|
40 | | - /// Returns `true` if the scope at `scope_index` is currently inside at least |
41 | | - /// one `try` block and therefore needs definition snapshots recorded. |
42 | | - pub(super) fn has_active_try_context(&self, scope_index: usize) -> bool { |
43 | | - self.0 |
44 | | - .get(scope_index) |
45 | | - .is_some_and(|stack| !stack.0.is_empty()) |
46 | | - } |
47 | | - |
48 | 40 | /// Record a definition in the try-node context at `scope_index`. |
49 | 41 | /// |
50 | | - /// For each active `try` block in that scope's context stack, a clone of `snapshot` |
51 | | - /// is pushed so that `except`/`finally` handlers can model which definitions may |
52 | | - /// have been reached before an exception. |
53 | | - pub(super) fn record_definition(&mut self, scope_index: usize, snapshot: &FlowSnapshot) { |
| 42 | + /// A snapshot is created from `use_def_map` only if the scope has at least |
| 43 | + /// one active `try` block; otherwise this is a no-op. |
| 44 | + pub(super) fn record_definition( |
| 45 | + &mut self, |
| 46 | + scope_index: usize, |
| 47 | + use_def_map: &UseDefMapBuilder<'_>, |
| 48 | + ) { |
54 | 49 | if let Some(stack) = self.0.get_mut(scope_index) { |
55 | | - stack.record_definition(snapshot); |
| 50 | + stack.record_definition(use_def_map); |
56 | 51 | } |
57 | 52 | } |
58 | 53 |
|
@@ -87,10 +82,13 @@ impl TryNodeContextStack { |
87 | 82 | try_suite_snapshots |
88 | 83 | } |
89 | 84 |
|
90 | | - /// For each `try` block on the stack, push the snapshot onto the `try` block |
91 | | - fn record_definition(&mut self, snapshot: &FlowSnapshot) { |
| 85 | + /// For each `try` block on the stack, create a snapshot and push it. |
| 86 | + /// |
| 87 | + /// The snapshot is created lazily per context, so this is a no-op when |
| 88 | + /// no `try` block is active. |
| 89 | + fn record_definition(&mut self, use_def_map: &UseDefMapBuilder<'_>) { |
92 | 90 | for context in &mut self.0 { |
93 | | - context.record_definition(snapshot.clone()); |
| 91 | + context.record_definition(use_def_map.snapshot()); |
94 | 92 | } |
95 | 93 | } |
96 | 94 | } |
|
0 commit comments