Conversation
…etic and unary minus
Native bool values reached raw C++ operators that treat them as the integers
0 and 1, which differs from PHP in three places:
- Relational comparison. PHP compares a bool with any other scalar as two
bools (false < true); the C++ operator compared numerically:
function le(bool $a, int $b): bool { return $a <= $b; }
le(false, -7); // PHP: true compiled: false
<, <=, > and >= now convert the non-bool scalar operand to bool. == and
<=> already used php::equals()/php::compare().
- Arithmetic. A bool operand of +, -, * or / was not treated as an int, so
the checked varint paths did not apply and `7 / false` divided by a raw
bool: a SIGFPE even with `use varint_types`, where PHP raises
DivisionByZeroError. Bool operands are now converted to int first. % is
unchanged: a non-int operand already takes php::fn::mod.
- Unary minus. -$bool stayed a bool (-true printed `true`, PHP gives -1). It
is now an int, and a non-numeric operand (e.g. a string) is lowered like
Zend does, as multiplication by -1, matching what unary plus already does.
The inferred types follow (bitwise/shift/arithmetic operators treat a bool
operand as int; -bool is int; minus of a non-numeric value is dynamic).
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.
Fixes #145
Native bool values reached raw C++ operators that treat them as the integers 0 and 1.
The change
BinaryOpTrait::parseBinaryOp()): when exactly one operand of<,<=,>or>=is a bool, the other int/float/string operand is converted to bool, as PHP compares them.==and<=>already go throughphp::equals()/php::compare()and are unchanged.+,-,*or/is converted to int first, so the existing int paths apply — in particular the checked varint division, which turns7 / falsefrom a SIGFPE intoDivisionByZeroError.%is left alone on purpose: a non-int operand already takesphp::fn::mod(converting it would move native-mode%onto the raw C++ operator). Native-mode/keeps the documented raw int division (see Native mode: a runtime zero divisor or PHP_INT_MIN % -1 kills the process (SIGFPE) #142).UnaryExpressionTrait::parseUnaryMinus()):-$boolis an int, and a non-numeric operand is lowered like Zend does, as multiplication by-1— the same approach unary plus already uses (fix(compiler): apply unary plus numeric conversion #132).-boolis int, and minus of a non-numeric value is dynamic.Verified on a binary
PHP 8.4.26 ZTS with embed, GCC 11, Linux x64. 25 cases (bool vs int/float/string for each relational operator in both orders,
bool + int,bool * bool,float - bool,int / bool,int % bool,-true,-false,-"5",-"1.5",-"abc",$x = -true, division/modulo byfalse):use varint_types7 / false7 / false, the documented native int division (unchanged, #142)operator/bool-operands-native.phptandbool-operands-varint.phpttests/compiler(full)Found with a differential fuzzer.