Commit 96fda3f
sql: parenthesize postfix-access receivers (
The receiver helpers for the postfix `.field`/`.*` and `[…]` operators
whitelisted variants that are only ever produced by the parser wrapped in
`Expr::Nested`, so a `Nested`-stripped AST (an implicit rewrite, a fuzzed
round trip) printed them bare and the trailing token re-bound:
* `write_dot_receiver` treated a bare `Identifier`/`QualifiedWildcard`
receiver as safe, but `a` followed by `.b`/`.*` prints `a.b`/`a.*`, which
reparses as the qualified identifier `Identifier([a, b])` /
`QualifiedWildcard([a])` instead of a field/wildcard access. The parser
only builds those accesses over a parenthesized receiver, so it always
wraps the name in `Nested`. Drop both from the safe set. `FieldAccess` /
`WildcardAccess` receivers stay safe: their own printing parenthesizes a
bare-name base (`(a).b.c`), so the chain remains self-delimiting and
parser-shaped `(x).a.b` does not gain spurious parens.
* `write_subscript_receiver` only parenthesized keyword identifiers. A `Cast`
receiver is unsafe because the type parser eats the following `[…]` as an
array suffix (`(a::int4)[1]` -> `a::int4[1]` is `a` cast to `int4[]`), and a
nested `Subscript` receiver is unsafe because consecutive `[…]` flatten into
one node (`(a[1])[2]` -> `a[1][2]` is a single two-position subscript).
Replace the keyword check with an explicit safe whitelist that
parenthesizes `Cast`, nested `Subscript`, and the operator/comparison
constructs by default.
Both are churn-free: the parser never emits these receivers bare (it eats the
`[…]` into the cast type, flattens subscripts, and wraps dotted names in
`Nested`), so the parser and pretty-printer datadriven suites are unchanged.
Found by extending the round-trip grammar fuzzer to cover subscripts, field
access, `COLLATE`, `AT TIME ZONE`, and the `position`/`extract`/`substring`
special forms.
Tests: adds `postfix_access_receiver_reparenthesized_after_nested_stripped`
to `sqlparser_common.rs`.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>. and []) by self-delimitation1 parent e4e1fbf commit 96fda3f
2 files changed
Lines changed: 109 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
561 | 561 | | |
562 | 562 | | |
563 | 563 | | |
564 | | - | |
565 | | - | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
566 | 575 | | |
567 | 576 | | |
568 | 577 | | |
569 | | - | |
570 | | - | |
571 | | - | |
| 578 | + | |
572 | 579 | | |
573 | 580 | | |
574 | 581 | | |
| |||
879 | 886 | | |
880 | 887 | | |
881 | 888 | | |
882 | | - | |
883 | | - | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
884 | 894 | | |
885 | 895 | | |
886 | 896 | | |
887 | | - | |
888 | | - | |
889 | | - | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
890 | 928 | | |
891 | 929 | | |
892 | 930 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
990 | 990 | | |
991 | 991 | | |
992 | 992 | | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
0 commit comments