Static analysis of expl3 programs (13): Reaching definitions and expandability

Over the fourteen months since the last full devlog post, I have released twenty updates (1, 2, 3, …, 20) to expltools, the bundle that provides the explcheck static analyzer for the expl3 programming language.

These updates have brought two major features to flow analysis, the final processing step of explcheck that I teased in that earlier post: reaching definitions and expandability analysis.

In this post, I outline both features and the algorithms behind them, adapted to the dynamic scoping and inherent uncertainty of expl3 programs.

Reaching definitions

Reaching definitions is a standard data-flow analysis that determines which function and variable definitions may reach different points in a program. This lets us estimate which definition may be used by a particular function call or variable use, taking control flow into account rather than relying solely on the approximate name-based matching used by semantic analysis.

Reaching definitions requires a graph describing the program’s control flow. For a language like expl3, we also need to adapt the analysis to:

  1. account for function and variable definitions executed dynamically within other function calls, potentially through chains of calls; and
  2. propagate the uncertainty of statements recognized by semantic analysis alongside their reaching definitions.

I described the relevant data structures and algorithms in a “half-post” last November. I also discussed them more informally at TUG 2026. See the relevant parts of my presentation slides and the recording of my talk.

Expandability analysis

Expandability analysis estimates the expandability of function calls, function definitions, and the segments produced by syntactic analysis, such as function replacement texts and boolean expressions. The estimates distinguish between fully expandable, restricted-expandable, and unexpandable material, following the expl3 language specification.

I briefly introduced the approach at TUG 2026; see my presentation slides and the recording of my talk. Below, I describe the implementation in more detail.

Syntactic analysis

We begin in syntactic analysis by assigning optimistic expandability estimates to function calls and recognizing standalone variables and constants that constrain the expandability of their enclosing segments:

  • Calls to LaTeX3 standard-library functions are classified as fully expandable, restricted-expandable, or unexpandable using the expandability metadata extracted from the LaTeX3 .dtx sources by generate-explcheck-latex3.lua and recorded in the generated explcheck-latex3.lua module. Calls whose argument lists contain an x-type argument are also initially treated as unexpandable.

  • Standalone variables and constants of certain types, including bool, box, and coffin, are treated as unexpandable in ordinary material. The types are related to those discussed under the naming conventions in Section 1.1 of The LaTeX3 Interfaces.

    We must distinguish between expansion contexts, however. A boolean constant such as \c_true_bool is not expandable as ordinary top-level material, but it is valid within a boolean expression. We therefore keep a separate estimate for boolean-expression expansion, allowing this constant without treating it as generally expandable.

The enclosing segment’s initial estimates are weakened by the calls and standalone variables or constants it contains. Unknown user-defined function calls remain optimistically expandable until semantic analysis can resolve their possible definitions.

Consider this example:

\cs_new:Nn
  \example_actually_expandable:
  { \c_true_bool }
\cs_new_eq:NN
  \example_unexpandable:
  \example_actually_expandable:
\cs_set_protected:Nn
  \example_unexpandable:
  {
    \tl_set:Nn
      \l_tmpa_tl
      { foo }
  }
\bool_if:nTF
  { \example_unexpandable: }
  { bar }
  { baz }

In the initial syntactic analysis, the call to \example_unexpandable: in the boolean expression on line 15 is provisionally considered fully expandable: its definition has not yet been resolved. The boolean-expression segment inherits this optimistic estimate.

After semantic analysis recognizes the definitions, syntactic analysis also examines their replacement-text segments:

  • The standalone constant \c_true_bool in the definition of \example_actually_expandable: (lines 1–3) makes its replacement text unexpandable in ordinary material, but leaves it fully expandable in the boolean-expression context.
  • The call to \tl_set:Nn in the protected redefinition of \example_unexpandable: (lines 7–13) makes its replacement text unexpandable in both contexts, according to the LaTeX3 metadata.

Semantic analysis

Semantic analysis recognizes function calls as different kinds of statements, some of which receive expandability estimates of their own.

For example, a protected direct function definition (\cs_new_protected:Nn) is unexpandable even when its replacement text is otherwise expandable. An unprotected direct definition (\cs_new:Nn) inherits the constraints of its replacement-text segment. Functions whose names have x-type argument specifiers are likewise treated as unexpandable, including when they are introduced through indirect definitions or variants.

For indirect function definitions and function variants, we identify potential base definitions using their control sequence names. If no user-defined base can be found but the base is a known LaTeX3 standard-library function, we use the expandability recorded in explcheck-latex3.lua instead.

We then propagate expandability estimates between definitions, calls, and segments using a fixed-point worklist algorithm. For each expansion context, the algorithm proceeds as follows:

  1. Find possible base definitions of indirect definitions and function variants, and possible definitions of user-defined function calls. For each dependent statement, maintain two support counts: the numbers of possible definitions that may be fully expandable and restricted-expandable, respectively. Initialize a worklist of relevant definitions and calls.
  2. Take a statement from the worklist and check whether its current estimate still has sufficient support.
    • If an indirect definition or variant loses a property, weaken its estimate, reduce the support counts of dependent statements, and enqueue those whose support may have changed.
    • If a user-defined function call loses a property, weaken its estimate and that of its enclosing segment. If the segment is the replacement text of a direct function definition, weaken the definition too, so that the change can propagate to its dependents.
    • If a direct definition loses a property, reduce the support counts of statements that may depend on it and enqueue them as necessary.
  3. Continue until the worklist is empty and no estimate can be weakened further.

The process converges because estimates only become weaker: for each expansion context, each object can lose full expandability and restricted expandability at most once. Support counts only decrease. With D function definitions and C user-defined function calls, the worklist propagation has a worst-case bound of O(D² + DC) for a fixed number of expansion contexts.

The estimates are deliberately optimistic. Name-based matching considers potential definitions even when some of them cannot reach a particular call. If any candidate remains expandable, it can keep a call’s estimate expandable, although the definition that actually reaches the call may be unexpandable.

Let’s return to the example. After recognizing the statements, semantic analysis knows that:

  • The direct definition of \example_actually_expandable: (lines 1–3) is unexpandable in ordinary material but may be fully expandable in a boolean expression, following its replacement-text segment.
  • The protected direct redefinition of \example_unexpandable: (lines 7–13) is unexpandable in both contexts, due both to protection and to the \tl_set:Nn call in its replacement text.

When the worklist algorithm runs:

  1. The indirect definition of \example_unexpandable: (lines 4–6) is supported by \example_actually_expandable: (lines 1–3). The call on line 15 has two name-matched candidate definitions of \example_unexpandable: – the indirect definition on lines 4–6 and the protected direct redefinition on lines 7–13.
  2. The indirect definition inherits the estimates of its base: unexpandable in ordinary material but potentially fully expandable in a boolean expression. This can reduce the basic support count of the call, but the boolean-expression support remains.
  3. The call and its enclosing boolean-expression segment consequently remain potentially fully expandable in the boolean-expression context, because the name-based analysis cannot exclude the earlier alias definition.

This is still too optimistic. The protected redefinition on lines 7–13 replaces the earlier alias before the call on line 15. Only the protected definition reaches that call, so its boolean expression is actually unexpandable. To distinguish these two definitions, we need flow analysis.

Flow analysis

Flow analysis runs the same fixed-point algorithm again, now using user-defined function call edges derived from reaching definitions instead of approximate name-based matching. This narrows the set of candidate definitions and can further weaken the estimates without discarding the earlier analysis.

For our example:

  1. The indirect definition on lines 4–6 still has the direct definition of \example_actually_expandable: on lines 1–3 as its base. The call on line 15, however, has only the protected direct definition on lines 7–13 among its reaching definitions: the earlier alias has been overwritten.
  2. The indirect definition retains its distinction between basic and boolean-expression expandability, but it no longer provides support for the final call.
  3. Since the only reaching definition of \example_unexpandable: is unexpandable, the call loses both kinds of expandability in both contexts. Its enclosing boolean-expression segment becomes unexpandable too, as expected.

The important difference is not a new notion of expandability, but better information about which definition can actually be called. The semantic analysis errs on the side of considering too many possible definitions; the flow analysis uses control flow to rule some of them out.

What this means to users

Reaching definitions and expandability analysis make it possible for explcheck to detect additional issues and reduce false positives in existing diagnostics. In the example above, the final boolean expression is reported under E508 (Unexpandable or restricted-expandable boolean expression). Its explanatory chain can trace the problem from \example_unexpandable: to \tl_set:Nn.

The following warnings and errors are enabled or improved by the analyses, as described in explcheck’s documentation:

  • Calling an undefined function [E408 and E505]
  • Multiply defined function [E500]
  • Multiply defined function variant [W501]
  • Function variant for an undefined function [E405 and E504]
  • Indirect function definition from an undefined function [E411 and E506]
  • Setting a function before definition [W507]
  • Unexpandable or restricted-expandable boolean expression [E428 and E508]
  • Defined an unexpandable function as unprotected [W429 and W512]
  • Unused private function [W401 and W502]
  • Unused private function variant [W402 and W503]
  • Unused variable or constant [W415 and W517]

Together, the issues implemented so far account for approximately 38% of the warnings and errors planned for flow analysis.

Get involved!

Your feedback is invaluable. Feel free to contribute or share your thoughts by visiting the project repository. More improvements are on the way as explcheck continues to grow.

Written on November 24, 2026
Last updated on November 24, 2026