Skip to content

Normalize integer literals to decimal - #348

Merged
ondrejmirtes merged 1 commit into
2.4.xfrom
integer-literal-numeric-value
Nov 25, 2026
Merged

ondrejmirtes merged 1 commit into
2.4.xfrom
integer-literal-numeric-value

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

ConstExprIntegerNode::$value kept the spelling of the literal, so consumers that cast it with (int) read 0x1F, 0b101 and 0o17 as 0 and the legacy octal 017 as 17. In PHPStan, @param 0x1F $x resolved to 0, int<0, 0xFF> to 0, and array{0x10: int} to array{0: int}.

The parser now stores the decimal number the literal denotes: 0x1F → '31', 017 and 0o17 → '15', -0b11 → '-3', +123 → '123'. This covers binary, octal (0o and a leading 0, like PHP), hexadecimal, signs and underscores, in any case. Numbers of any size are converted exactly, as strings, so a value beyond PHP_INT_MAX behaves like a decimal literal of that size did before.

The existing (int) casts in consumers then work as they are. Printing a node that came from a non-decimal literal now gives the decimal form.

ConstExprIntegerNode::$value kept the spelling of the literal, so
casting it with (int) read 0x1F, 0b101 and 0o17 as 0 and the legacy
octal 017 as 17. The parser now stores the decimal number the literal
denotes, of any size, so 0x1F is '31' and +123 is '123'.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1eeb7-0373-7460-8310-e6c03050dc58
@ondrejmirtes
ondrejmirtes force-pushed the integer-literal-numeric-value branch from 3fe1408 to 0cef6a9 Compare November 25, 2026 13:05
@ondrejmirtes ondrejmirtes changed the title Add ConstExprIntegerNode::getNumericValue() Normalize integer literals to decimal Nov 25, 2026
@ondrejmirtes
ondrejmirtes merged commit f80e753 into 2.4.x Nov 25, 2026
67 of 68 checks passed
@ondrejmirtes
ondrejmirtes deleted the integer-literal-numeric-value branch November 25, 2026 13:45
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