<p>How do statically typed programming languages build CFGs for programs containing expression-level control flow (e.g. ternary expressions, match expressions) which require transformation or extra information (i.e. type information, etc.)?</p>
<p>Here’s a concrete example:</p>
<pre><code>let foo = &quot;bar&quot;
let k = match foo {
    &quot;bar&quot; -&gt; 1,
    _ -&gt; 0,
 }
print(foo, k)
</code></pre>
<p>My assumption is that the CFG for the whole program is first built and then patched afterwards when enough information becomes available for these kinds of expressions. I am just not sure how correct this is or how well it works in practice for efficiency reasons.</p>

<control-flow><static-analysis>
How can I account for expressions when building control-flow graphs?
CC BY-SA 4.0
2023-12-31T11:21:21.377Z
Welcome to the site! This is a good question, but can you be a bit clearer about how your concrete example shows extra information (e.g. type information) being needed?
kaya3
<p>The simple answer is that many don't. Production compiler typically lower the initial AST into a simpler subset of the language (often more than once).  In many cases, typechecking and linting and other analysis steps that might require constructing a CFG happen <em>after</em> one or more of these lowering phases. A consequence of lowering is that expression-level control flow blocks (and other syntactic sugar) are removed.</p>
<p>For example, your code sample might be rewritten as such:</p>
<pre><code>let foo = “bar”
let k; // k is declared, but not assigned yet
match foo {
    “bar” -&gt; k = 1,
    _ -&gt; k = 0,
}
print(foo, k)
</code></pre>
<p>From there, it should be more obvious as to how a CFG would be constructed.</p>
<p>The Rust compiler has a good, fairly straightforward overview of this process. You can start reading about it <a href="https://rustc-dev-guide.rust-lang.org/overview.html" rel="noreferrer">here</a> if you wish. Check out the sections on lowering, and try exploring some of the source code (it is very well documented).</p>

CC BY-SA 4.0
2023-12-31T16:02:10.830Z
2023-12-31T16:02:10.830Z
Luke LaBonte
# How can I account for expressions when building control-flow graphs?
Posted by: username_0 on 2023-12-31
How do statically typed programming languages build CFGs for programs containing expression-level control flow (e.g. ternary expressions, match expressions) which require transformation or extra information (i.e. type information, etc.)?
Here’s a concrete example:
let foo = "bar"
let k = match foo {
    "bar" -> 1,
    _ -> 0,
 }
print(foo, k)

My assumption is that the CFG for the whole program is first built and then patched afterwards when enough information becomes available for these kinds of expressions. I am just not sure how correct this is or how well it works in practice for efficiency reasons.
**Tags:** control-flow static-analysis
> **Comments:**
> **username_1**: Welcome to the site! This is a good question, but can you be a bit clearer about how your concrete example shows extra information (e.g. type information) being needed?  
---
## 1 answers
### Answer by username_2 on 2023-12-31. Score: 10 | Accepted answer
The simple answer is that many don't. Production compiler typically lower the initial AST into a simpler subset of the language (often more than once).  In many cases, typechecking and linting and other analysis steps that might require constructing a CFG happen after one or more of these lowering phases. A consequence of lowering is that expression-level control flow blocks (and other syntactic sugar) are removed.
For example, your code sample might be rewritten as such:
let foo = “bar”
let k; // k is declared, but not assigned yet
match foo {
    “bar” -> k = 1,
    _ -> k = 0,
}
print(foo, k)

From there, it should be more obvious as to how a CFG would be constructed.
The Rust compiler has a good, fairly straightforward overview of this process. You can start reading about it here if you wish. Check out the sections on lowering, and try exploring some of the source code (it is very well documented).
---
