Re: com php-src: Disable restrictions regarding arrays in constants at run-time. For the discussion around it, see the thread on the mailing list:
http://www.mail-archive.com/internals@lists.php.net/msg68245.html: NEWS Zend/tests/constant_expressions_arrays.phpt Zend/tests/errmsg_040.phpt
Zend/tests/ns_059.phpt Zend/zend_language_parser.y Zend/zend_vm_def.h Zend/zend_vm_execute.h ext/standard/array.c
| From: | Nikita Popov | Date: | Tue, 12 Aug 2014 08:49:47 +0000 |
| Subject: | Re: com php-src: Disable restrictions regarding arrays in constants at run-time. For the discussion around it, see the thread on the mailing list: http://www.mail-archive.com/internals@lists.php.net/msg68245.html: NEWS Zend/tests/constant_expressions_arrays.phpt Zend/tests/errmsg_040.phpt Zend/tests/ns_059.phpt Zend/zend_language_parser.y Zend/zend_vm_def.h Zend/zend_vm_execute.h ext/standard/array.c |
||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-79810@lists.php.net to get a copy of this message | ||
On Wed, Jul 30, 2014 at 5:57 PM, Bob Weinand <bwoebi@php.net> wrote:
> Commit: d5ddd2dbb263cd626a5e0f203c00d6f39168507b
> Author: Bob Weinand <bobwei9@hotmail.com> Wed, 30 Jul 2014
> 17:57:07 +0200
> Parents: 354ee12b73dbe20d0c4fa7883eb34714f89109df
> Branches: PHP-5.6
>
> Link:
>
> http://git.php.net/?p=php-src.git;a=commitdiff;h=d5ddd2dbb263cd626a5e0f203c00d6f39168507b
>
> Log:
> Disable restrictions regarding arrays in constants at run-time.
> For the discussion around it, see the thread on the mailing list:
>
> http://www.mail-archive.com/internals@lists.php.net/msg68245.html
>
> Changed paths:
> M NEWS
> M Zend/tests/constant_expressions_arrays.phpt
> M Zend/tests/errmsg_040.phpt
> M Zend/tests/ns_059.phpt
> M Zend/zend_language_parser.y
> M Zend/zend_vm_def.h
> M Zend/zend_vm_execute.h
> M ext/standard/array.c
>
>
> Diff:
> diff --git a/NEWS b/NEWS
> index ba58827..7516f0c 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -4,6 +4,8 @@ PHP
> NEWS
>
> - Core:
> . Fixed bug #67693 (incorrect push to the empty array). (Tjerk)
> + . Removed inconsistency regarding behaviour of array in constants at
> + run-time. (Bob)
>
> - SPL:
> . Revert fix for bug #67064 (BC issues). (Bob)
> diff --git a/Zend/tests/constant_expressions_arrays.phpt
> b/Zend/tests/constant_expressions_arrays.phpt
> index 061fcc6..2ab0345 100644
> --- a/Zend/tests/constant_expressions_arrays.phpt
> +++ b/Zend/tests/constant_expressions_arrays.phpt
> @@ -22,7 +22,7 @@ class foo {
>
> var_dump(foo::bar);
>
> -var_dump(a); // Eventually allow that later with array dereferencing of
> constants
> +var_dump(a, a[0], a[2], a[2][1], a[3]);
>
> ?>
> --EXPECTF--
> @@ -32,4 +32,35 @@ int(1)
> int(4)
> int(1)
>
> -Fatal error: Arrays are not allowed in constants at run-time in %s on
> line %d
> +Notice: Undefined offset: 3 in %s on line %d
> +array(3) {
> + [0]=>
> + int(1)
> + [1]=>
> + int(2)
> + [2]=>
> + array(2) {
> + [0]=>
> + int(3)
> + [1]=>
> + array(1) {
> + [0]=>
> + int(4)
> + }
> + }
> +}
> +int(1)
> +array(2) {
> + [0]=>
> + int(3)
> + [1]=>
> + array(1) {
> + [0]=>
> + int(4)
> + }
> +}
> +array(1) {
> + [0]=>
> + int(4)
> +}
> +NULL
> diff --git a/Zend/tests/errmsg_040.phpt b/Zend/tests/errmsg_040.phpt
> index c3a007f..cda8d4c 100644
> --- a/Zend/tests/errmsg_040.phpt
> +++ b/Zend/tests/errmsg_040.phpt
> @@ -12,4 +12,12 @@ var_dump(test::TEST);
> echo "Done\n";
> ?>
> --EXPECTF--
> -Fatal error: Arrays are not allowed in constants at run-time in %s on
> line %d
> +array(3) {
> + [0]=>
> + int(1)
> + [1]=>
> + int(2)
> + [2]=>
> + int(3)
> +}
> +Done
> diff --git a/Zend/tests/ns_059.phpt b/Zend/tests/ns_059.phpt
> index 48da40b..701e448 100644
> --- a/Zend/tests/ns_059.phpt
> +++ b/Zend/tests/ns_059.phpt
> @@ -7,5 +7,5 @@ const C = array();
> var_dump(C);
> ?>
> --EXPECTF--
> -Fatal error: Arrays are not allowed in constants at run-time in
> %sns_059.php on line 4
> -
> +array(0) {
> +}
> diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
> index 56e702e..7502723 100644
> --- a/Zend/zend_language_parser.y
> +++ b/Zend/zend_language_parser.y
> @@ -865,13 +865,16 @@ yield_expr:
> ;
>
> combined_scalar_offset:
> - combined_scalar '[' dim_offset ']' {
> zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3
> TSRMLS_CC); }
> - | combined_scalar_offset '[' dim_offset ']' {
> fetch_array_dim(&$$,
> &$1, &$3 TSRMLS_CC); }
> - | T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' { $1.EA = 0;
> zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3
> TSRMLS_CC); }
> + combined_scalar '[' dim_offset ']' {
> zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3
> TSRMLS_CC); }
> + | combined_scalar_offset '[' dim_offset ']' {
> fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
> + | T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' { $1.EA = 0;
> zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3
> TSRMLS_CC); }
> + | general_constant '[' dim_offset ']' {
> zend_do_begin_variable_parse(TSRMLS_C); fetch_array_dim(&$$, &$1, &$3
> TSRMLS_CC); }
> +;
>
> combined_scalar:
> - T_ARRAY '(' array_pair_list ')' { $$ = $3; }
> - | '[' array_pair_list ']' { $$ = $2; }
> + T_ARRAY '(' array_pair_list ')' { $$ = $3; }
> + | '[' array_pair_list ']' { $$ = $2; }
> +;
>
> function:
> T_FUNCTION { $$.u.op.opline_num = CG(zend_lineno); }
> @@ -1038,20 +1041,22 @@ static_operation:
> | '(' static_scalar_value ')' { $$ = $2; }
> ;
>
> -
> -scalar:
> - T_STRING_VARNAME { $$ = $1; }
> - | class_name_scalar { $$ = $1; }
> - | class_constant { $$ = $1; }
> +general_constant:
> + class_constant { $$ = $1; }
> | namespace_name { zend_do_fetch_constant(&$$, NULL, &$1,
> ZEND_RT, 1 TSRMLS_CC); }
> | T_NAMESPACE T_NS_SEPARATOR namespace_name { $$.op_type =
> IS_CONST; ZVAL_EMPTY_STRING(&$$.u.constant);
> zend_do_build_namespace_name(&$$, &$$, &$3 TSRMLS_CC); $3 = $$;
> zend_do_fetch_constant(&$$, NULL, &$3, ZEND_RT, 0 TSRMLS_CC); }
> | T_NS_SEPARATOR namespace_name { char *tmp =
> estrndup(Z_STRVAL($2.u.constant), Z_STRLEN($2.u.constant)+1);
> memcpy(&(tmp[1]), Z_STRVAL($2.u.constant), Z_STRLEN($2.u.constant)+1);
> tmp[0] = '\\'; efree(Z_STRVAL($2.u.constant)); Z_STRVAL($2.u.constant) =
> tmp; ++Z_STRLEN($2.u.constant); zend_do_fetch_constant(&$$, NULL, &$2,
> ZEND_RT, 0 TSRMLS_CC); }
> - | common_scalar { $$ = $1; }
> - | '"' encaps_list '"' { $$ = $2; }
> - | T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
> - | T_CLASS_C { if
> (Z_TYPE($1.u.constant) == IS_CONSTANT) {zend_do_fetch_constant(&$$, NULL,
> &$1, ZEND_RT, 1 TSRMLS_CC);} else {$$ = $1;} }
> ;
>
> +scalar:
> + T_STRING_VARNAME { $$ = $1; }
> + | general_constant { $$ = $1; }
> + | class_name_scalar { $$ = $1; }
> + | common_scalar { $$ = $1; }
> + | '"' encaps_list '"' { $$ = $2; }
> + | T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; }
> + | T_CLASS_C { if (Z_TYPE($1.u.constant) == IS_CONSTANT)
> {zend_do_fetch_constant(&$$, NULL, &$1, ZEND_RT, 1 TSRMLS_CC);} else {$$ =
> $1;} }
> +;
If I read the patch correctly, these changes allow Foo[0] usage in normal
expressions - however the [] implementation for static scalars still allows
a bunch of things that are not allowed otherwise, like 1[0],
__NAMESPACE__[3] or <<<'A'\nB\nA\n[0].
Either the general PHP syntax should support these as well, or they should
be removed for constant scalar expressions, or - which I would prefer as
we're close to a 5.6 release - the [] support for constant scalar
expressions should be removed altogether and implemented again when
properly thought through.
Thanks,
Nikita