Skip to content

Fix unary minus rendering into -- line comments - #2532

Open
LucaCappelletti94 wants to merge 1 commit into
mainfrom
unary-minus-comment-guard
Open

LucaCappelletti94 wants to merge 1 commit into
mainfrom
unary-minus-comment-guard

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

Fuzz round-trip found SELECT - -1 rendering as SELECT --1, which re-parses as a line comment in every dialect except MySQL, and SELECT - @2 rendering as SELECT -@2 in PostgreSQL, where -@ retokenizes as one custom operator and parsing fails. Same fusion hits - ~1 (-~1), - !!2 (-!!2), - ||/2 (-||/2) and - #x (-# x).

Expr::UnaryOp display concatenates operator and operand in its final arm. The fix emits a space when the operator is - and the operand renders with a leading operator character. The first character is probed through a discarding write sink, so canonical output like -1 or -x is unchanged and nothing allocates. Backtick-quoted operands and -? placeholders keep their current rendering because those dialects have no fusion rule and the characters stay out of the set.

There are some other variants of this error, but they are sufficiently distinct that I believe it is best to adress them in distinct PRs.

Comment thread src/ast/mod.rs Outdated
| UnaryOperator::QuestionDash
| UnaryOperator::QuestionPipe
) {
) || (op == &UnaryOperator::Minus && starts_with_operator_char(expr))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as #2533 (comment) -- I think if you restructured this code to match it would be easier to undertstand the behavior and coverage

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.

2 participants