From 60afeb55374cf94334a40f799537ce86a1d2daeb Mon Sep 17 00:00:00 2001 From: Saki Takamachi <34942839+SakiTakamachi@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:41:49 +0900 Subject: [PATCH] RFC: Change GMP bool cast behavior (#15151) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implementation of "RFC: Change GMP bool cast behavior" https://wiki.php.net/rfc/fix_up_bcmath_number_class Co-authored-by: Tim Düsterhus --- NEWS | 2 ++ UPGRADING | 3 +++ ext/gmp/gmp.c | 4 ++++ ext/gmp/tests/cast.phpt | 18 ++++++++++++++++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 68577d27460..cfde633ce8d 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.4.0beta1 +- GMP: + . RFC: Change GMP bool cast behavior. (Saki Takamachi) 01 Aug 2024, PHP 8.4.0alpha3 diff --git a/UPGRADING b/UPGRADING index 1f4604cbfd6..a14711ed4ff 100644 --- a/UPGRADING +++ b/UPGRADING @@ -51,6 +51,9 @@ PHP 8.4 UPGRADE NOTES - GMP: . The GMP class is now final and cannot be extended anymore. RFC: https://wiki.php.net/rfc/gmp-final + . Casting a GMP object to bool changed so that 0 becomes false and everything else + becomes true. + RFC: https://wiki.php.net/rfc/fix_up_bcmath_number_class - Intl: . resourcebundle_get(), ResourceBundle::get(), and accessing offsets on a diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index 63838ad2a6e..09cbff82942 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -298,6 +298,10 @@ static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int typ ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum)); } return SUCCESS; + case _IS_BOOL: + gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num; + ZVAL_BOOL(writeobj, mpz_sgn(gmpnum) != 0); + return SUCCESS; default: return FAILURE; } diff --git a/ext/gmp/tests/cast.phpt b/ext/gmp/tests/cast.phpt index 661a0159b94..864d8b2ad08 100644 --- a/ext/gmp/tests/cast.phpt +++ b/ext/gmp/tests/cast.phpt @@ -12,11 +12,25 @@ var_dump((int) $n); var_dump((float) $n); var_dump((bool) $n); +echo "\n"; + +$zero = gmp_init(0); +echo $zero, "\n"; +var_dump((string) $zero); +var_dump((int) $zero); +var_dump((float) $zero); +var_dump((bool) $zero); + ?> ---EXPECTF-- +--EXPECT-- 42 string(2) "42" int(42) float(42) +bool(true) -Recoverable fatal error: Object of class GMP could not be converted to bool in %s on line %d +0 +string(1) "0" +int(0) +float(0) +bool(false)