Skip to content

fix(pretty_print): print an unexpected AST instead of panicking - #1001

Open
psteinroe wants to merge 1 commit into
refactor/pretty-print-clausesfrom
fix/pretty-print-ast-panics
Open

psteinroe wants to merge 1 commit into
refactor/pretty-print-clausesfrom
fix/pretty-print-ast-panics

Conversation

@psteinroe

@psteinroe psteinroe commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #999.

The printer panicked on ASTs the parser doesn't build: assert_node_variant! (22 call sites) panicked when a list held a node of another kind, unreachable! fired on the Undefined values of parser enums, a NOT with other than one argument panicked, and unwrap / expect were used on optional children (the offset of a window frame bound, the source of (a, b) = ..., the last argument of a VARIADIC call). A panic in the formatter takes down the request that triggered it.

What changed

  • assert_node_variant! is gone. Where a list holds a node of an unexpected kind, the printer writes it with emit_node.
  • Where there is nothing to write, it writes nothing: an Undefined set operation, ON CONFLICT action or boolean operator, a constant without a value, a missing frame offset.
  • (a, b) = ... without a source is written as a plain SET item. A source that claims more columns than the list has writes the ones there are, instead of slicing past the end.

Either way the output has a different AST than the input, so format_statement refuses it and leaves the statement as written, which is what it should do with such an AST.

Not changed: the debug_assert!s that document what the parser builds stay, so debug builds and tests still notice a shape the printer doesn't expect. The renderer's checks that groups are balanced stay too: they check the emitter, not the AST.

Tests

  • tests/malformed_ast.rs takes the ASTs of valid statements, breaks them (an Integer in the values of an enum, a String among ALTER TABLE commands, a NOT with two arguments, a constant without a value, a frame bound without its offset), and checks that they print. Each of them panicked before.
  • cargo test -p pgls_pretty_print: no snapshot changes.
  • cargo test -p pgls_pretty_print --release --test option_matrix -- --ignored
  • cargo clippy --workspace --all-targets -- -D warnings

assert_node_variant!, the unreachable! on parser enums and the unwrap and
expect on optional children are gone. A node of an unexpected kind is
printed through emit_node, and a missing one is skipped. The output then
has a different AST, which format_statement refuses.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant