Zend: Deprecate non-canonical cast names (#19372)

RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_non-standard_cast_names
This commit is contained in:
Gina Peter Banyard 2025-08-08 21:22:49 +01:00 committed by GitHub
parent 43a91089ca
commit 3bf21a0d43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 80 additions and 8 deletions

View file

@ -1,5 +1,5 @@
--TEST--
casting different variables to double
casting different variables to float
--INI--
precision=14
--FILE--
@ -32,7 +32,7 @@ $vars = array(
);
foreach ($vars as $var) {
$tmp = (double)$var;
$tmp = (float)$var;
var_dump($tmp);
}

View file

@ -0,0 +1,11 @@
--TEST--
Non canonical (binary) cast
--FILE--
<?php
var_dump((binary) 42);
?>
--EXPECTF--
Deprecated: Non-canonical cast (binary) is deprecated, use the (string) cast instead in %s on line %d
int(42)

View file

@ -0,0 +1,11 @@
--TEST--
Non canonical (boolean) cast
--FILE--
<?php
var_dump((boolean) 42);
?>
--EXPECTF--
Deprecated: Non-canonical cast (boolean) is deprecated, use the (bool) cast instead in %s on line %d
int(42)

View file

@ -0,0 +1,11 @@
--TEST--
Non canonical (double) cast
--FILE--
<?php
var_dump((double) 42);
?>
--EXPECTF--
Deprecated: Non-canonical cast (double) is deprecated, use the (float) cast instead in %s on line %d
int(42)

View file

@ -0,0 +1,11 @@
--TEST--
Non canonical (integer) cast
--FILE--
<?php
var_dump((integer) "42");
?>
--EXPECTF--
Deprecated: Non-canonical cast (integer) is deprecated, use the (int) cast instead in %s on line %d
int(42)

View file

@ -2310,7 +2310,7 @@ simple_list:
case IS_NULL: PREFIX_OP("(unset)", 240, 241);
case _IS_BOOL: PREFIX_OP("(bool)", 240, 241);
case IS_LONG: PREFIX_OP("(int)", 240, 241);
case IS_DOUBLE: PREFIX_OP("(double)", 240, 241);
case IS_DOUBLE: PREFIX_OP("(float)", 240, 241);
case IS_STRING: PREFIX_OP("(string)", 240, 241);
case IS_ARRAY: PREFIX_OP("(array)", 240, 241);
case IS_OBJECT: PREFIX_OP("(object)", 240, 241);

View file

@ -212,7 +212,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%token T_INC "'++'"
%token T_DEC "'--'"
%token T_INT_CAST "'(int)'"
%token T_DOUBLE_CAST "'(double)'"
%token T_DOUBLE_CAST "'(float)'"
%token T_STRING_CAST "'(string)'"
%token T_ARRAY_CAST "'(array)'"
%token T_OBJECT_CAST "'(object)'"

View file

@ -1629,14 +1629,28 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN_WITH_IDENT(T_VAR);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_INT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"|"float"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("integer"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (integer) is deprecated, use the (int) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("float"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_DOUBLE_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (double) is deprecated, use the (float) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"real"{TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_throw_exception(zend_ce_parse_error, "The (real) cast has been removed, use (float) instead", 0);
@ -1645,10 +1659,17 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN(T_DOUBLE_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_STRING_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("binary"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (binary) is deprecated, use the (string) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
RETURN_TOKEN(T_ARRAY_CAST);
}
@ -1657,10 +1678,17 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_
RETURN_TOKEN(T_OBJECT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_BOOL_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("boolean"){TABS_AND_SPACES}")" {
if (PARSER_MODE()) {
zend_error(E_DEPRECATED, "Non-canonical cast (boolean) is deprecated, use the (bool) cast instead");
}
RETURN_TOKEN(T_INT_CAST);
}
<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
RETURN_TOKEN(T_UNSET_CAST);
}