Repository navigation
fix: throw ArithmeticError when a PHP# int is raised to a negative power - #97
Conversation
Problem:
The checker types `int ** int` as `int`, but the engine followed PHP for a
negative exponent: `2 ** -1` in PHP# code became the float 0.5. A value typed
`int` could hold a float at runtime, the silent change to float that spec
section 24 already refuses for an overflow.
Solution:
`checked_pow_function`, the one path every PHP# `**` and `**=` takes in the
VM, the opcache and both JITs, throws "Integer raised to a negative power"
before it computes the power, and keeps the left operand's int in the result
as an overflow does. `zend_binary_op_produces_error` refuses to fold a PHP#
`int ** int` with a negative exponent, so the compiler and the optimizer leave
it to the runtime. A float on either side and plain PHP keep PHP's result.
The spec states the rule in section 24, and decision 75 records it with the
choice to keep `**`.
Zend/
├── zend_operators.c <- checked_pow_function throws on a negative int exponent
├── zend_compile.c <- zend_binary_op_produces_error: no compile-time fold of it
└── tests/sharp/
├── Overflow.sharp <- inverse and half take a float base; floatExponent, constantNegativePower
├── overflow_pow.inc <- negative exponents for **, **=, typed and untyped properties,
│ dead code, a float operand, and plain PHP
└── overflow_pow*.phpt <- plain, function JIT and tracing JIT expectations
docs/
├── spec.md <- section 24: int ** int is an int, a negative exponent throws
└── decisions/075-int-power.md <- the decision, its options and precedent
|
Review verdict: CHANGES NEEDED The engine is correct. I found no path that turns a PHP# Changes
Verified on the engineThe engine was built at Every PHP# case throws
The opcache file cache is covered by the priming and using modes. The checker refuses every array or collection element Plain PHP keeps PHP's result:
Code, spec and decision
|
Problem: `zend_binary_op_produces_error` refuses to fold a PHP# `int ** int` with a negative exponent, but no test covered it. Without those three lines, the optimizer folds `0 ** -1` with PHP's pow. PHP prints "Power of base 0 and negative exponent is deprecated" at compile time, under opcache, in PHP# code the spec says only throws. Every test still passed with the guard removed, because no case raised a constant zero base to a negative power. Solution: `Overflow::zeroNegativePower` raises a constant-propagated `0 ** -1`. Each of the three pow tests expects one ArithmeticError "Integer raised to a negative power" at its line and no Deprecated line. With the guard removed, the tracing JIT and function JIT runs of overflow_pow.phpt fail on that Deprecated line. Zend/tests/sharp/ ├── Overflow.sharp <- zeroNegativePower, appended so no earlier line moves ├── overflow_pow.inc <- calls it after constantNegativePower └── overflow_pow*.phpt <- plain, function JIT and tracing JIT expect its throw
|
team-lead approval: APPROVED at head 26da919, once
Merge after |
Problem
The checker types
int ** intasint, but the engine followed PHP for a negative exponent:2 ** -1in PHP# code became the float0.5. A value typedintcould hold a float at runtime. Spec section 24 already refuses that silent change tofloatfor an overflow.Solution
checked_pow_functionis the one path every PHP#**and**=takes, in the VM, the opcache and both JITs. It now throwsArithmeticError, "Integer raised to a negative power", before it computes the power. As an overflow does, it keeps the left operand's int in the result, so a typed property keeps its old value.zend_binary_op_produces_errorrefuses to fold a PHP#int ** intwith a negative exponent, so neither the compiler nor the optimizer turns it into0.5. Afloaton either side, and plain PHP code, keep PHP's result.The spec states the rule in section 24, and decision 75 records it, with the choice to keep
**. The checker's compile error for a negative literal exponent is mago-sharp #129. This PR does not re-pin Mago.Tests
overflow_pow.phpt,overflow_pow_jit_function.phptandoverflow_pow_jit_tracing.phptrun the cases inoverflow_pow.inc. Each failed atorigin/masterand passes here.**on two ints, including0 ** -1.**=on a typed property, which keeps its old int.**=on an untyped property.**and dead**=.((float) a) ** b,2.0 ** -1and an int raised to a float exponent each give a float.2 ** -1givesfloat(0.5), from a function and from a literal.Local runs:
sharp/bin/test --opcache --repeat --passes --census --differentialon the power tests: all 22 modes pass.sharp/bin/test: 171 of 171 pass.