Summary
The public dump_node / dump_node_with_color slice code by the node's
byte range without documenting that code must be the exact source the
node was parsed from; a mismatched pair panics (index out of bounds)
instead of returning the io::Error the docs claim is the only error mode.
Location
src/output/dump.rs:43-71 (public signatures + # Errors docs)
src/output/dump.rs:264 (let snippet = &code[node.start_byte()..node.end_byte()];)
Evidence
write_node_snippet indexes the caller-supplied slice directly:
let snippet = &code[node.start_byte()..node.end_byte()];
dump_node/dump_node_with_color take code: &[u8] and node: &Node
as independent parameters. There is no compile-time tie between them: a
Node borrows from one Ast, and code is a separate &[u8]. Nothing
prevents a caller from passing ast_a.source() with a node from a
different (or differently-sized) tree. When node.end_byte() exceeds
code.len(), the slice expression panics.
The # Errors docs on both functions state only:
Propagates any [std::io::Error] produced by the color-aware writer …
i.e. they promise the function either succeeds or returns Err(io::Error).
A panic on a mismatched code/node pair violates that contract and the
project's no-panic-in-non-test-code rule for a published 1.x API surface.
All in-repo callers (big-code-analysis-cli/src/dispatch.rs,
c_langs_macros) correctly pair ast.source() with ast.root_node(),
so this is not reachable from the CLI today — but the public API offers
no guard or documented precondition for external callers.
Expected Behavior
Either (a) the docs state the precondition ("code must be the source
the node was parsed from") explicitly, or (b) the snippet read uses a
bounds-checked access (code.get(start..end)) and renders nothing /
a placeholder when the range is out of bounds, keeping the documented
"only returns io::Error" contract truthful.
Actual Behavior
A mismatched code/node pair panics with an index-out-of-bounds slice,
contradicting the # Errors documentation.
Impact
External library consumers of the published dump_node /
dump_node_with_color API can trigger an undocumented panic by passing a
code slice that does not match the node's tree. Low likelihood (in-repo
usage is correct), but it is a documented-contract violation on a stable
public surface.
Resolution
Fixed in 7a41184: added # Panics precondition docs to dump_node and dump_node_with_color (code must be the exact source the node was parsed from). Doc-only.
Summary
The public
dump_node/dump_node_with_colorslicecodeby the node'sbyte range without documenting that
codemust be the exact source thenodewas parsed from; a mismatched pair panics (index out of bounds)instead of returning the
io::Errorthe docs claim is the only error mode.Location
src/output/dump.rs:43-71(public signatures +# Errorsdocs)src/output/dump.rs:264(let snippet = &code[node.start_byte()..node.end_byte()];)Evidence
write_node_snippetindexes the caller-supplied slice directly:dump_node/dump_node_with_colortakecode: &[u8]andnode: &Nodeas independent parameters. There is no compile-time tie between them: a
Nodeborrows from oneAst, andcodeis a separate&[u8]. Nothingprevents a caller from passing
ast_a.source()with a node from adifferent (or differently-sized) tree. When
node.end_byte()exceedscode.len(), the slice expression panics.The
# Errorsdocs on both functions state only:i.e. they promise the function either succeeds or returns
Err(io::Error).A panic on a mismatched
code/nodepair violates that contract and theproject's no-panic-in-non-test-code rule for a published 1.x API surface.
All in-repo callers (
big-code-analysis-cli/src/dispatch.rs,c_langs_macros) correctly pairast.source()withast.root_node(),so this is not reachable from the CLI today — but the public API offers
no guard or documented precondition for external callers.
Expected Behavior
Either (a) the docs state the precondition ("
codemust be the sourcethe
nodewas parsed from") explicitly, or (b) the snippet read uses abounds-checked access (
code.get(start..end)) and renders nothing /a placeholder when the range is out of bounds, keeping the documented
"only returns
io::Error" contract truthful.Actual Behavior
A mismatched
code/nodepair panics with an index-out-of-bounds slice,contradicting the
# Errorsdocumentation.Impact
External library consumers of the published
dump_node/dump_node_with_colorAPI can trigger an undocumented panic by passing acodeslice that does not match the node's tree. Low likelihood (in-repousage is correct), but it is a documented-contract violation on a stable
public surface.
Resolution
Fixed in 7a41184: added
# Panicsprecondition docs to dump_node and dump_node_with_color (code must be the exact source the node was parsed from). Doc-only.