Skip to content

Commit 62ee85e

Browse files
committed
explain how control flow is not acyclic
1 parent 8683f92 commit 62ee85e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

codebase/compiler/Codebase-Compiler-Binder.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ Containers and declarations are the same as for symbol binding, so those concept
143143
Declarations introduce type information; containers form the scope where type information is relevant.
144144
Other nodes may narrow, so they also interact with control flow.
145145

146-
The control flow graph is a directed acyclic graph; that means each relevant node points to its antecedents (parents).
146+
The control flow graph is a directed graph; that means each relevant node points to its antecedents (parents)—the nodes that come *before* it in control flow.
147147
Specifically, each node can have a `flowNode`; this flow node has a `kind` and one or more `antecedents`.
148148
As the binder walks the tree, `bindWorker` assigns the current flow node to specific nodes that can introduce type information.
149149
Specific nodes that affect control flow alter the current flow node, such as `bindWhileStatement`.
150150

151+
Notably, loops make the graph cyclic: the pre-while `FlowLabel` points both to the flow node before the loop and also the flow node at the bottom of the loop.
152+
That's because nodes inside loops might introduce type information, and this information should affect the next iteration of the loop.
153+
151154
Here's an example:
152155

153156
```ts
@@ -175,7 +178,6 @@ That check narrows `string | number` to `string`.
175178
Finally, the last `return x` starts with the post-if flow node, which unions the types that result from the `then` and `else` branches.
176179
But because the `then` branch returns, it doesn't contribute anything to the union; the resulting type is just `number`.
177180

178-
179181
## Emit flags
180182

181183
TODO: Missing completely

0 commit comments

Comments
 (0)