Conversation
parseBitwiseNot() converted every operand to int. PHP's ~ inverts the bytes
of a string and returns a string, and raises "Cannot perform bitwise not on
<type>" for bool and array:
function f(string $s): mixed { return ~$s; }
bin2hex(f("abc")); // PHP: 9e9d9c compiled: "-1" (2d31)
~false; // PHP: TypeError compiled: -1
and `$x = ~"ab"` in a function returning string did not compile (ambiguous
conversion from php::Int to php::Str).
A statically string, bool or array operand now goes through the Variant
operator, i.e. Zend's bitwise_not_function(), and ~$string is inferred as a
string. int, float and operands of unknown type keep the native operator, so
idioms such as `$flags &= ~self::FLAG` (class constants are not typed
statically) still compile to a plain C++ ~.
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 #147
parseBitwiseNot()converted every operand to int.The change
Variantoperator, i.e. Zend'sbitwise_not_function(): a string has its bytes inverted, bool/array raiseTypeError.~$stringis inferred as a string.~. A first version also routed unknown types throughVariant, which brokeNativePropertyTest::testNativeIntPropertyAssignOpConvertsBitwiseNotClassConst: class constants are not typed statically, so$flags &= ~ReflectionClassConstant::IS_PUBLIC(and the common~self::FLAGidiom) would have lost the native operator. That is kept as it was.Not covered:
~$mixedholding a string at runtime still goes through the int path, as on master.Verified on a binary
PHP 8.4.26 ZTS with embed, GCC 11, Linux x64. 11 cases (
~false,~true,~"abc",~"",~"1e3",$x = ~"ab",~~"xyz",~5,~PHP_INT_MIN,~1.5,~$mixedint):operator/bitwise-not-operands.phpttests/compiler(full)Found with a differential fuzzer.