Skip to content

RFC: Deprecate lossy conversions to and from bool - #25889

Open
theodorejb wants to merge 3 commits into
php:masterfrom
theodorejb:deprecate-lossy-bool-conversions
Open

theodorejb wants to merge 3 commits into
php:masterfrom
theodorejb:deprecate-lossy-bool-conversions

Conversation

@theodorejb

Copy link
Copy Markdown
Contributor

@Girgias

Girgias commented Nov 29, 2026

Copy link
Copy Markdown
Member

I know @iluuu1994 told me the main reason he voted against my related RFC was because of the int -> bool so his input might be more valuable than mine if special casing 0/1 is sufficient. But overall in favour of the proposal (which shouldn't be a surprise) :)

@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch 2 times, most recently from bf065ce to 381e820 Compare November 29, 2026 04:46

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to review zend_may_throw_ex() function for the ZEND_ASSIGN_OBJ opcode and any opcode that assigns to typed properties as they follow the same semantics as function param/return vals.

@alexdowad

Copy link
Copy Markdown
Contributor

No objection from me (on mbstring)

In coercive typing mode, emit E_DEPRECATED when a parameter, return value, or typed property coerces a value to bool which has no canonical bool representation (an int other than 0 or 1, any float, or a string other than "0" or "1"), or coerces a bool to int, float, or string. This also applies when a union type without bool, such as int|float or int|string, selects another type for a bool.

NAN coerced to bool emits the deprecation in addition to the existing "unexpected NAN value was coerced to bool" warning.

zend_parse_arg_str_or_long_slow() no longer falls back to string when the deprecation for the int conversion is promoted to an exception.

The compiler and optimizer previously evaluated a constant $strict argument to in_array() with zend_is_true(), bypassing the parameter type check. This is now only done for true and false, so other values go through the regular call. As a side effect, in_array($needle, $haystack, 1) now throws a TypeError under strict_types, as with any other bool parameter.

RFC: https://wiki.php.net/rfc/deprecate-lossy-bool-conversions
@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from 885608a to 04b355a Compare November 29, 2026 13:49
@LamentXU123

LamentXU123 commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

I agree with the deprecation RFC. And code-wise I think fine for phar and intl. Please go ahead 👍

The lossy bool conversion deprecation surfaced a number of mistakes in tests, which are fixed here instead of recording the deprecation in the expected output:

- A misplaced parenthesis passed the result of `substr(PHP_OS, 0, 3) == 'WIN'` to strtoupper() in the poll test helper and two stream tests. This only worked because true and false become "1" and "".
- Trampoline __call() methods used as predicates were declared as returning string instead of bool.
- Iterator::valid() implementations returned a filename string or an int counter.
- Reflection tests called strlen() on a false doc comment before checking for false.
- fread(), fgets(), inet_pton(), iconv_mime_decode(), file_get_contents(), tempnam(), curl_exec(), mktime(), imageloadfont(), mb_ereg_replace(), and locale_get_display_name() failures returning false were passed on to other functions.
- The FPM log reader returned false from fgets() at the end of the stream from a ?string method, which now returns null instead.
- Three OpenSSL session resumption tests waited for a server message which the test helper had already printed, so the helper's ?string wait() returned false from fgets() at the end of the stream.
- Two memory checks looped until feof() and passed the false returned by fgets() at the end of /proc/meminfo to preg_match().
- A mysqli debug test passed the false returned for a trace file which is expected not to be written to explode().
- PDO_ODBC tests passed false from getenv() as the user and password when they were not set, instead of null.
- Arbitrary strings or ints were passed to the bool parameters of ini_get_all(), strstr(), substr_compare(), IntlRuleBasedBreakIterator::__construct(), and tidy_repair_file(), and assigned to the bool SNMP::$oid_increasing_check property.
- false was passed instead of an empty string to bind_textdomain_codeset() and locale_lookup().
- ob_start(), openssl_decrypt(), and debug_backtrace() were called with a bool for parameters which were bool before PHP 5.4 and 5.3.6, but are now int flags.
- array_filter() was called with true instead of ARRAY_FILTER_USE_BOTH.
- SessionHandlerInterface::gc() implementations returned true instead of the number of deleted sessions.
- A get hook for an int property returned the bool result of isset().
- false was passed to an int variadic parameter.
- The bool returned by touch() was passed to date() as the timestamp.
- false was passed to PDOStatement::execute(), which binds every value as a string, so a user-defined SQLite function received an empty string for its bool parameter. It is now bound with PDO::PARAM_BOOL.
- run-tests.php printed the current directory as the loaded php.ini when no php.ini was loaded, as false was passed to realpath().
Tests which only exist to pass each type to one parameter of a function (such as the "different data types" variation tests and "invalid params" tests) are removed, as this type juggling is global behavior already covered by the Zend type declaration tests. Other tests which included booleans in their list of int, float, or string inputs, or other scalars in their list of bool inputs, no longer pass them, so they won't need updating again when the deprecation becomes a TypeError. The fscanf() tests which write booleans to a file as test data now convert them to strings explicitly.

The deprecations are recorded only in tests of the coercion behavior itself, such as the scalar and union type declaration tests, the ZPP weak mode tests, exit() values, and the NAN coercion tests, in the IntlTimeZone::getCanonicalID() test which assigns its bool by-reference result to a typed string property, in the PDO_DBLIB test of quoting booleans, and in the regression test for bug #79919, which needs the bool returned by error_log() to reproduce the bug. The phpGH-22112 test now ignores the deprecation in its error handler, so it continues to cover an exception thrown by the NAN warning.
@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from 04b355a to 4625bd1 Compare November 29, 2026 14:31
@theodorejb

Copy link
Copy Markdown
Contributor Author

@Girgias Thanks! I checked zend_may_throw_ex(), and no changes are needed. For typed properties, the only assignments it treats as unable to throw are ones where the value already has the property's exact type, so no conversion happens and the deprecation can't be emitted. All other assignments to typed properties are treated as possibly throwing, so the deprecation is handled the same way as the existing float to int deprecation.

I also added ext/opcache/tests/jit/assign_obj_008.phpt to cover lossy conversions when assigning to typed properties under the JIT.

@TimWolla TimWolla added the RFC label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants