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++ ~.
|
Thank you for investigating this issue and for providing the reproduction cases and regression test! We have refactored operator dispatch, including unary
This resolves the string inversion and typed return issues reported here, and also covers the dynamic string case. Class constant masks and typed property compound assignments have been verified as well. Under TypePHP's static rules, We added 8 PHPT files covering the issues from #136 and this PR. All 74 related compilation/runtime regression tests passed, and the full PHPUnit suite completed with no failures (2,411 tests). The new mechanism now covers the issue, so this patch is no longer needed. Thank you for your contribution—it helped us identify the missing coverage and improve the implementation. |
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.