From be6dee3c5d2c61d14e217808dcb16068cfd0d13b Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Mon, 5 Aug 2024 10:42:04 +0200 Subject: [PATCH] Reset seen symbols when ending namespace (GH-15244) Previously, seen symbols were never cleaned during the compilation of a single file. This makes it impossible to use a class or function from a different namespace if such a symbol is also declared within the same file. This is inconsistent with how it would work when split into different files. --- NEWS | 1 + UPGRADING | 4 ++ .../ns_end_resets_seen_symbols_1.phpt | 58 +++++++++++++++++ .../ns_end_resets_seen_symbols_2.phpt | 62 +++++++++++++++++++ Zend/zend_compile.c | 2 + 5 files changed, 127 insertions(+) create mode 100644 Zend/tests/use_function/ns_end_resets_seen_symbols_1.phpt create mode 100644 Zend/tests/use_function/ns_end_resets_seen_symbols_2.phpt diff --git a/NEWS b/NEWS index f5801cf7a38..38dfed12eab 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,7 @@ PHP NEWS . Passing E_USER_ERROR to trigger_error() is now deprecated. (Girgias) . Fixed bug GH-15292 (Dynamic AVX detection is broken for MSVC). (nielsdos) . Using "_" as a class name is now deprecated. (Girgias) + . Exiting a namespace now clears seen symbols. (ilutov) - Curl: . Added constants CURL_HTTP_VERSION_3 (libcurl 7.66) and CURL_HTTP_VERSION_3ONLY diff --git a/UPGRADING b/UPGRADING index 528aaff6165..2a3e3d18554 100644 --- a/UPGRADING +++ b/UPGRADING @@ -248,6 +248,10 @@ PHP 8.4 UPGRADE NOTES RFC: https://wiki.php.net/rfc/deprecated_attribute . Implemented property hooks. RFC: https://wiki.php.net/rfc/property-hooks + . Exiting a namespace now clears seen symbols. This allows using a symbol in a + namespace block, even if a previous namespace block declared a symbol with + the same name. + See Zend/tests/use_function/ns_end_resets_seen_symbols_1.phpt. - Curl: . curl_version() returns an additional feature_list value, which is an diff --git a/Zend/tests/use_function/ns_end_resets_seen_symbols_1.phpt b/Zend/tests/use_function/ns_end_resets_seen_symbols_1.phpt new file mode 100644 index 00000000000..71f9c74f0c2 --- /dev/null +++ b/Zend/tests/use_function/ns_end_resets_seen_symbols_1.phpt @@ -0,0 +1,58 @@ +--TEST-- +Namespace end resets seen function symbols +--FILE-- + +--EXPECTF-- +Warning: The use statement with non-compound name 'f' has no effect in %s on line 36 +f +Ns\f +Ns\f +f +f +Ns\f +f +Ns\f diff --git a/Zend/tests/use_function/ns_end_resets_seen_symbols_2.phpt b/Zend/tests/use_function/ns_end_resets_seen_symbols_2.phpt new file mode 100644 index 00000000000..83925c14664 --- /dev/null +++ b/Zend/tests/use_function/ns_end_resets_seen_symbols_2.phpt @@ -0,0 +1,62 @@ +--TEST-- +Namespace end resets seen class symbols +--FILE-- + +--EXPECTF-- +Warning: The use statement with non-compound name 'C' has no effect in %s on line 32 +object(C)#%d (0) { +} +object(Ns\C)#1 (0) { +} +object(Ns\C)#1 (0) { +} +object(C)#%d (0) { +} +object(C)#%d (0) { +} +object(Ns\C)#1 (0) { +} +object(C)#%d (0) { +} +object(Ns\C)#1 (0) { +} diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index a650d81d163..cfd97e59b7e 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -377,6 +377,8 @@ static void zend_reset_import_tables(void) /* {{{ */ efree(FC(imports_const)); FC(imports_const) = NULL; } + + zend_hash_clean(&FC(seen_symbols)); } /* }}} */