1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Variable declaration utilities and definite assignment traversal for `FlowAnalyzer`.
//!
//! Extracted from `control_flow.rs` to keep that file focused on the core
//! flow-type narrowing algorithm. This module provides:
//!
//! - **Definite assignment**: worklist-based graph traversal (`check_definite_assignment`)
//! - **Variable declaration inspection**: type annotation presence, mutability, destructuring
use rustc_hash::{FxHashMap, FxHashSet};
use tsz_binder::{FlowNodeId, flow_flags};
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::syntax_kind_ext;
use tsz_solver::TypeId;
use super::control_flow::FlowAnalyzer;
impl<'a> FlowAnalyzer<'a> {
/// Iterative flow graph traversal for definite assignment checks.
///
/// This replaces the recursive implementation to prevent stack overflow
/// on deeply nested control flow structures. Uses a worklist algorithm with
/// fixed-point iteration to determine if a variable is definitely assigned.
pub(crate) fn check_definite_assignment(
&self,
reference: NodeIndex,
flow_id: FlowNodeId,
_visited: &mut Vec<FlowNodeId>,
cache: &mut FxHashMap<FlowNodeId, bool>,
) -> bool {
// Helper: Add a node to the worklist if not already present
let add_to_worklist =
|node: FlowNodeId,
worklist: &mut Vec<FlowNodeId>,
in_worklist: &mut FxHashSet<FlowNodeId>| {
if !in_worklist.contains(&node) {
worklist.push(node);
in_worklist.insert(node);
}
};
// Result cache: flow_id -> is_assigned
// We use a local cache that we'll merge into the provided cache
let mut local_cache: FxHashMap<FlowNodeId, bool> = FxHashMap::default();
// Worklist for processing nodes
let mut worklist: Vec<FlowNodeId> = vec![flow_id];
let mut in_worklist: FxHashSet<FlowNodeId> = FxHashSet::default();
in_worklist.insert(flow_id);
// Track nodes that are waiting for their antecedents to be computed
// Map: node -> set of antecedents it's waiting for
let mut waiting_for: FxHashMap<FlowNodeId, FxHashSet<FlowNodeId>> = FxHashMap::default();
while let Some(current_flow) = worklist.pop() {
in_worklist.remove(¤t_flow);
// Skip if we already have a result
if local_cache.contains_key(¤t_flow) {
continue;
}
let Some(flow) = self.binder.flow_nodes.get(current_flow) else {
// Flow node doesn't exist - mark as assigned
local_cache.insert(current_flow, true);
// Notify any nodes waiting for this one
let ready: Vec<_> = waiting_for
.iter()
.filter(|(_, ants)| ants.contains(¤t_flow))
.map(|(&node, _)| node)
.collect();
for node in ready {
waiting_for.remove(&node);
add_to_worklist(node, &mut worklist, &mut in_worklist);
}
continue;
};
// Compute the result based on flow node type
let result = if flow.has_any_flags(flow_flags::UNREACHABLE) {
false
} else if flow.has_any_flags(flow_flags::ASSIGNMENT) {
if self.assignment_targets_reference(flow.node, reference) {
true
} else if let Some(&ant) = flow.antecedent.first() {
if let Some(&ant_result) = local_cache.get(&ant) {
ant_result
} else {
// Add antecedent to worklist and defer
add_to_worklist(ant, &mut worklist, &mut in_worklist);
waiting_for.entry(current_flow).or_default().insert(ant);
continue;
}
} else {
false
}
} else if flow.has_any_flags(flow_flags::BRANCH_LABEL) {
if flow.antecedent.is_empty() {
false
} else {
// Check if all antecedents have results
let mut all_ready = true;
let mut results = Vec::new();
for &ant in &flow.antecedent {
if let Some(ant_node) = self.binder.flow_nodes.get(ant)
&& ant_node.has_any_flags(flow_flags::UNREACHABLE)
{
// Unreachable branches satisfy the condition vacuously
results.push(true);
continue;
}
if let Some(&ant_result) = local_cache.get(&ant) {
results.push(ant_result);
} else {
all_ready = false;
add_to_worklist(ant, &mut worklist, &mut in_worklist);
waiting_for.entry(current_flow).or_default().insert(ant);
}
}
if !all_ready {
continue;
}
// All antecedents processed - compute result (all must be true)
results.iter().all(|&r| r)
}
} else if flow.has_any_flags(flow_flags::LOOP_LABEL | flow_flags::CONDITION) {
if let Some(&ant) = flow.antecedent.first() {
if let Some(&ant_result) = local_cache.get(&ant) {
ant_result
} else {
add_to_worklist(ant, &mut worklist, &mut in_worklist);
waiting_for.entry(current_flow).or_default().insert(ant);
continue;
}
} else {
false
}
} else if flow.has_any_flags(flow_flags::SWITCH_CLAUSE) {
if flow.antecedent.is_empty() {
false
} else {
// Similar to BRANCH_LABEL - check all antecedents
let mut all_ready = true;
let mut results = Vec::new();
for &ant in &flow.antecedent {
if let Some(ant_node) = self.binder.flow_nodes.get(ant)
&& ant_node.has_any_flags(flow_flags::UNREACHABLE)
{
results.push(true);
continue;
}
if let Some(&ant_result) = local_cache.get(&ant) {
results.push(ant_result);
} else {
all_ready = false;
add_to_worklist(ant, &mut worklist, &mut in_worklist);
waiting_for.entry(current_flow).or_default().insert(ant);
}
}
if !all_ready {
continue;
}
results.iter().all(|&r| r)
}
} else if flow.has_any_flags(flow_flags::START) {
false
} else if let Some(&ant) = flow.antecedent.first() {
if let Some(&ant_result) = local_cache.get(&ant) {
ant_result
} else {
add_to_worklist(ant, &mut worklist, &mut in_worklist);
waiting_for.entry(current_flow).or_default().insert(ant);
continue;
}
} else {
false
};
// Store the result
local_cache.insert(current_flow, result);
// Notify any nodes waiting for this one
let ready: Vec<_> = waiting_for
.iter()
.filter(|(_, ants)| ants.contains(¤t_flow))
.map(|(&node, _)| node)
.collect();
for node in ready {
waiting_for.remove(&node);
add_to_worklist(node, &mut worklist, &mut in_worklist);
}
}
// Get the final result
let final_result = *local_cache.get(&flow_id).unwrap_or(&false);
// Merge local cache into the provided cache
cache.extend(local_cache);
final_result
}
/// Check if an assignment node is a mutable variable declaration (let/var) without a type annotation.
/// Used to determine when literal types should be widened to their base types.
pub(crate) fn is_mutable_var_decl_without_annotation(&self, node: NodeIndex) -> bool {
let Some(node_data) = self.arena.get(node) else {
return false;
};
// Handle VARIABLE_DECLARATION directly
if node_data.kind == syntax_kind_ext::VARIABLE_DECLARATION {
let Some(decl) = self.arena.get_variable_declaration(node_data) else {
return false;
};
// If there's a type annotation, don't widen - the user specified the type
if decl.type_annotation.is_some() {
return false;
}
// Check if the parent declaration list is let/var (not const)
if let Some(ext) = self.arena.get_extended(node)
&& ext.parent.is_some()
&& let Some(parent_node) = self.arena.get(ext.parent)
{
use tsz_parser::parser::node_flags;
let flags = parent_node.flags as u32;
return (flags & node_flags::CONST) == 0;
}
return false;
}
// Handle VARIABLE_DECLARATION_LIST or VARIABLE_STATEMENT: check flags on the list
if node_data.kind == syntax_kind_ext::VARIABLE_DECLARATION_LIST
|| node_data.kind == syntax_kind_ext::VARIABLE_STATEMENT
{
use tsz_parser::parser::node_flags;
let flags = node_data.flags as u32;
if (flags & node_flags::CONST) != 0 {
return false;
}
// Check individual declarations for type annotations
if let Some(list) = self.arena.get_variable(node_data) {
for &decl_idx in &list.declarations.nodes {
let Some(decl_node) = self.arena.get(decl_idx) else {
continue;
};
if decl_node.kind == syntax_kind_ext::VARIABLE_DECLARATION
&& let Some(decl) = self.arena.get_variable_declaration(decl_node)
&& decl.type_annotation.is_none()
{
return true;
}
}
}
}
false
}
/// Check if an assignment flow node is a variable declaration with a type annotation.
///
/// When a variable has an explicit type annotation, the flow analysis should
/// use the declared type (not the initializer's structural type) for non-literal
/// assignments. This prevents the initializer's type from overriding the declared
/// type in the flow graph.
pub(crate) fn is_var_decl_with_type_annotation(&self, node: NodeIndex) -> bool {
let Some(node_data) = self.arena.get(node) else {
return false;
};
if node_data.kind == syntax_kind_ext::VARIABLE_DECLARATION
&& let Some(decl) = self.arena.get_variable_declaration(node_data)
{
return decl.type_annotation.is_some();
}
// Handle VARIABLE_DECLARATION_LIST or VARIABLE_STATEMENT
if (node_data.kind == syntax_kind_ext::VARIABLE_DECLARATION_LIST
|| node_data.kind == syntax_kind_ext::VARIABLE_STATEMENT)
&& let Some(list) = self.arena.get_variable(node_data)
{
for &decl_idx in &list.declarations.nodes {
let Some(decl_node) = self.arena.get(decl_idx) else {
continue;
};
if decl_node.kind == syntax_kind_ext::VARIABLE_DECLARATION
&& let Some(decl) = self.arena.get_variable_declaration(decl_node)
&& decl.type_annotation.is_some()
{
return true;
}
}
}
false
}
/// Get the declared annotation type for a variable declaration node, if available.
///
/// Returns `Some(type_id)` when `assignment_node` is a `VARIABLE_DECLARATION` with a
/// type annotation whose type has already been computed and cached in `node_types`.
/// Returns `None` otherwise (no annotation, wrong node kind, or not cached yet).
///
/// The type annotation node index is used as the cache key (not the declaration node),
/// matching how `get_type_from_type_node` caches in `node_types`.
pub(crate) fn annotation_type_from_var_decl_node(
&self,
assignment_node: NodeIndex,
) -> Option<TypeId> {
let decl_data = self.arena.get(assignment_node)?;
if decl_data.kind != syntax_kind_ext::VARIABLE_DECLARATION {
return None;
}
let var_decl = self.arena.get_variable_declaration(decl_data)?;
if var_decl.type_annotation.is_none() {
return None;
}
let node_types = self.node_types?;
node_types.get(&var_decl.type_annotation.0).copied()
}
/// Check if an assignment node represents a destructuring assignment.
/// Destructuring assignments widen literals to primitives, unlike direct assignments.
pub(crate) fn is_destructuring_assignment(&self, node: NodeIndex) -> bool {
let Some(node_data) = self.arena.get(node) else {
return false;
};
match node_data.kind {
syntax_kind_ext::BINARY_EXPRESSION => {
let Some(bin) = self.arena.get_binary_expr(node_data) else {
return false;
};
// Check if left side is a binding pattern OR array/object literal (for destructuring)
let left_is_binding = self.is_binding_pattern(bin.left);
let left_is_literal = self.contains_destructuring_pattern(bin.left);
left_is_binding || left_is_literal
}
syntax_kind_ext::VARIABLE_DECLARATION => {
let Some(decl) = self.arena.get_variable_declaration(node_data) else {
return false;
};
// Check if name is a binding pattern (destructuring in variable declaration)
self.is_binding_pattern(decl.name)
}
_ => false,
}
}
/// Check if a node is a binding pattern (array or object destructuring pattern)
fn is_binding_pattern(&self, node: NodeIndex) -> bool {
self.arena.get(node).is_some_and(|n| n.is_binding_pattern())
}
/// Check if a node contains a destructuring pattern (array/object literal with binding elements).
/// This handles cases like `[x] = [1]` where the left side is an array literal containing binding patterns.
///
/// Note: In TypeScript, if an array or object literal appears on the left side of an assignment,
/// it's ALWAYS a destructuring pattern, regardless of what elements it contains.
fn contains_destructuring_pattern(&self, node: NodeIndex) -> bool {
if node.is_none() {
return false;
}
let Some(node_data) = self.arena.get(node) else {
return false;
};
// If this is an array or object literal, it's a destructuring pattern when on the left side of an assignment
matches!(
node_data.kind,
syntax_kind_ext::ARRAY_LITERAL_EXPRESSION | syntax_kind_ext::OBJECT_LITERAL_EXPRESSION
)
}
}