Skip to content

Fix missing parentheses for negative exponentiation bases - #1173

Open
ydah wants to merge 1 commit into
nikic:masterfrom
ydah:fix-negative-pow-base
Open

ydah wants to merge 1 commit into
nikic:masterfrom
ydah:fix-negative-pow-base

Conversation

@ydah

@ydah ydah commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Currently, negative Int_ and Float_ nodes on the left of ** are printed without parentheses, changing the value of the expression:

$expr = new Pow(new Int_(-5), new Int_(2));
$printer->prettyPrintExpr($expr);
// Before: -5 ** 2   (evaluates to -25)
// After:  (-5) ** 2 (evaluates to 25)
Base Before After
new Int_(-5) -5 ** 2 → -25 (-5) ** 2 → 25
$factory->val(-5) -5 ** 2 → -25 (-5) ** 2 → 25
new Float_(-1.5) -1.5 ** 2 → -2.25 (-1.5) ** 2 → 2.25

Apply unary-minus precedence to numeric scalars that print with a leading -. Also check precedence when numeric values are changed in place during format-preserving printing.

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