com php-src: Improve detection of strict_types declaration being first in the file: Zend/tests/typehints/sc alar_namespace_declaration_order.phpt
Zend/tests/type hints/scalar_strict_declaration_placement_001.p hpt Zend/tests/typehints/scalar_strict_declaration_plac ement_002.phpt
Zend/tests/typehints/scalar_strict _declaration_placement_003.phpt Zend/tests/type hints/scalar_strict_declaration_placement_004.p hpt
Zend/tests/typehints/scalar_strict_declar

From: Date: Wed, 18 Feb 2015 16:40:52 +0000
Subject: com php-src: Improve detection of strict_types declaration being first in the file: Zend/tests/typehints/sc alar_namespace_declaration_order.phpt
Zend/tests/type hints/scalar_strict_declaration_placement_001.p hpt Zend/tests/typehints/scalar_strict_declaration_plac ement_002.phpt
Zend/tests/typehints/scalar_strict _declaration_placement_003.phpt Zend/tests/type hints/scalar_strict_declaration_placement_004.p hpt
Zend/tests/typehints/scalar_strict_declar
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-85718@lists.php.net to get a copy of this message
Commit: ac2ece885f7571189c278232f25f3117445c5c03 Author: Anthony Ferrara <ircmaxell@gmail.com> Wed, 18 Feb 2015 11:40:52 -0500 Parents: e2053fa94cb39369bfed53d237ef093ccebd8288 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ac2ece885f7571189c278232f25f3117445c5c03 Log: Improve detection of strict_types declaration being first in the file Changed paths: D Zend/tests/typehints/scalar_namespace_declaration_order.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_001.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_002.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_003.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_004.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_005.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_006.phpt A Zend/tests/typehints/scalar_strict_declaration_placement_007.phpt M Zend/zend_compile.c Diff: diff --git a/Zend/tests/typehints/scalar_namespace_declaration_order.phpt b/Zend/tests/typehints/scalar_namespace_declaration_order.phpt deleted file mode 100644 index 15c1cf0..0000000 --- a/Zend/tests/typehints/scalar_namespace_declaration_order.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Scalar type with namespaces proper ordering ---FILE-- -<?php -declare(strict_types=1); -namespace Foo { - function add1(int $arg): int { - return $arg + 1; - } -} - -namespace { - var_dump(Foo\add1(123)); -} ---EXPECT-- -int(124) diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_001.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_001.phpt new file mode 100644 index 0000000..2a2b3e6 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_001.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test strict declaration being first operation only +--FILE-- +<?php + +function takes_int(int $x) { + global $errored; + if ($errored) { + echo "Failure!", PHP_EOL; + $errored = FALSE; + } else { + echo "Success!", PHP_EOL; + } +} + +?> +<?php + +declare(strict_types=1); +var_dump(takes_int(32)); + +?> +--EXPECTF-- +Fatal error: strict_types declaration must be the very first statement in the script in %s on line %d diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_002.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_002.phpt new file mode 100644 index 0000000..09ac111 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_002.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test strict declaration being first operation only 002 +--FILE-- +<?php + +namespace Foo; + +declare(strict_types=1); + +var_dump(strlen("abc")); + +?> +--EXPECTF-- +Fatal error: strict_types declaration must be the very first statement in the script in %s on line %d diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_003.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_003.phpt new file mode 100644 index 0000000..6ec5b59 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_003.phpt @@ -0,0 +1,11 @@ +--TEST-- +Test strict declaration being first operation only 003 +--FILE-- +hi<?php + +declare(strict_types=1); +var_dump(strlen(32)); + +?> +--EXPECTF-- +Fatal error: strict_types declaration must be the very first statement in the script in %s on line %d diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_004.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_004.phpt new file mode 100644 index 0000000..6000324 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_004.phpt @@ -0,0 +1,17 @@ +--TEST-- +Test strict declaration being first operation only 004 +--FILE-- +<?php +declare(strict_types=1); + +namespace Foo { + function add1(int $arg): int { + return $arg + 1; + } +} + +namespace { + var_dump(Foo\add1(123)); +} +--EXPECT-- +int(124) diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_005.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_005.phpt new file mode 100644 index 0000000..8611a20 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_005.phpt @@ -0,0 +1,14 @@ +--TEST-- +Test strict declaration being first operation only 005 +--FILE-- +<?php + +declare(strict_types=1); + +namespace Foo; + +var_dump(strlen("abc")); + +?> +--EXPECTF-- +int(3) \ No newline at end of file diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_006.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_006.phpt new file mode 100644 index 0000000..0c71cd9 --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_006.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test strict declaration being first operation only 006 +--FILE-- +<?php +declare(encoding=1); +declare(strict_types=1); + +namespace Foo; + +var_dump(strlen("abc")); + +?> +--EXPECTF-- +Warning: declare(encoding=...) ignored because Zend multibyte feature is turned off by settings in %s on line %d + +Fatal error: strict_types declaration must be the very first statement in the script in %s on line %d \ No newline at end of file diff --git a/Zend/tests/typehints/scalar_strict_declaration_placement_007.phpt b/Zend/tests/typehints/scalar_strict_declaration_placement_007.phpt new file mode 100644 index 0000000..1a398de --- /dev/null +++ b/Zend/tests/typehints/scalar_strict_declaration_placement_007.phpt @@ -0,0 +1,15 @@ +--TEST-- +Test strict declaration being first operation only 007 +--FILE-- +<?php +declare(strict_types=1); +declare(encoding=1); + +namespace Foo; + +var_dump(strlen("abc")); + +?> +--EXPECTF-- +Warning: declare(encoding=...) ignored because Zend multibyte feature is turned off by settings in %s on line %d +int(3) \ No newline at end of file diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index a77c19d..a13fab7 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -3890,16 +3890,10 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */ } else if (zend_string_equals_literal_ci(name, "strict_types")) { zval value_zv; - /* Strict Hint declaration was already handled during parsing. Here we - * only check that it is the first statement in the file. */ - uint32_t num = CG(active_op_array)->last; - while (num > 0 && - (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT || - CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) { - --num; - } + /* Check that it is the node in the AST from the file. */ + zend_ast_list *file_ast = zend_ast_get_list(CG(ast)); - if (num > 0) { + if (file_ast->child[0] != ast) { zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be " "the very first statement in the script"); }

« previous php.cvs (#85718) next »