fix(parser): parenthesize unary minus operand to avoid C++ pre-decrement pasting - #20
Open
AlessioGiacobbe wants to merge 1 commit into
Open
fix(parser): parenthesize unary minus operand to avoid C++ pre-decrement pasting#20AlessioGiacobbe wants to merge 1 commit into
AlessioGiacobbe wants to merge 1 commit into
Conversation
parseUnaryMinus emitted '-' . $code without guarding against an operand
that itself starts with '-', pasting into the C++ pre-decrement token:
`- -$x` compiled to `--x`. On a php::Var operand the generated
translation unit fails to build ("expression is not assignable"); on a
native int operand it builds and silently decrements: a function
`(int $x) => - -$x` returned 8 for input 9.
Parenthesize the operand exactly when its emitted code starts with '-'
(a nested unary minus or a negative literal), so plain literals keep
their compact form (`-7L`). Binary operands are already self-wrapped in
parentheses, and unary plus needs no change since it returns the
operand unchanged.
AlessioGiacobbe
force-pushed
the
fix/unary-minus-parens
branch
from
August 28, 2026 16:05
0f35790 to
97069e5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parseUnaryMinusemits'-' . $codewithout guarding against an operand that itself starts with-, so a nested unary minus pastes into the C++ pre-decrement token:- -$xcompiles to--x.php::Varoperand the generated translation unit fails to build:error: expression is not assignable.Fix
Parenthesize the operand exactly when its emitted code starts with
-(a nested unary minus or a negative literal):- -$x→-(-(x)). Plain literals keep their compact form (-7L), so existing generated output is unchanged everywhere the old code was correct. Binary operands are already self-wrapped in parentheses, and unary plus needs no change since it returns the operand unchanged.Tests
tests/compiler/operator/unary-minus-nested.phpt: nested minus onphp::Var, float, constant (-(-7)), triple chain, and the native-int function case above. Expected output verified against PHP 8.4.operator+float_edge+bigintsuites: 58/59 pass; the single failure (runtime-int-overflow-return.phpt) is a pre-existing environment issue (php::newClosureWithParametersmissing from the local phpx build) and fails identically on master.LocalVariableInitializerTestliteral expectations (php::Var negative = -7L;) are preserved — negative literals still emit without parentheses. No new PHPStan errors.