Skip to main content

MAX_AST_SERIALIZE_DEPTH

Constant MAX_AST_SERIALIZE_DEPTH 

Source
pub const MAX_AST_SERIALIZE_DEPTH: usize = 512 /* 512usize */;
Expand description

Greatest AST depth AstNode will serialize.

An AstNode tree is as deep as the parsed expression nesting, which a caller controls directly — 100 000 levels fit in a 200 KB payload, well inside bca-web’s body cap. serde cannot emit a tree without one native stack frame per level, and overflowing that stack aborts the process rather than raising a catchable panic (#1056), so the depth is bounded: a deeper tree fails serialization with an ordinary serializer error naming the limit.

The bound is set well clear of real source: the deepest AST across the ~8 000-file corpus under tests/repositories (TensorFlow, serde, DeepSpeech, …) is 188 levels. It is also set well clear of the stack: the earliest measured overflow of any emitted format was 2 000 levels on a debug build’s default 2 MiB thread.

Read that 188 as a corpus figure, not a conversion rate. The depth counted here is the AstNode tree after Alterator::alterate, and how many levels one level of source nesting costs is a per-language property of what that alterator flattens. The Tcl family is the outlier: since #1381 a script body keeps its children, so one brace level spans braced_word → command → word_list and costs three levels here — 196 levels over 64 nested eval braces, measured by a_tcl_brace_level_costs_three_ast_levels below — putting the ceiling near 170 nested braces rather than 512. Nothing in the corpus approaches either number — it holds no Tcl at all — but a caller re-deriving this bound from the 188 alone would be reasoning about the wrong quantity.