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.
This commit is contained in:
Ilija Tovilo 2024-08-05 10:42:04 +02:00
parent 5622def429
commit be6dee3c5d
No known key found for this signature in database
GPG Key ID: 5050C66BFCD1015A
5 changed files with 127 additions and 0 deletions

1
NEWS
View File

@ -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

View File

@ -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

View File

@ -0,0 +1,58 @@
--TEST--
Namespace end resets seen function symbols
--FILE--
<?php
namespace {
function f() {
echo __FUNCTION__, "\n";
}
f();
}
namespace Ns {
function f() {
echo __FUNCTION__, "\n";
}
f();
}
namespace {
use function Ns\f;
f();
}
namespace Ns {
use function f;
f();
}
namespace {
f();
}
namespace Ns {
f();
}
namespace {
use function f;
f();
}
namespace Ns {
use function Ns\f;
f();
}
?>
--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

View File

@ -0,0 +1,62 @@
--TEST--
Namespace end resets seen class symbols
--FILE--
<?php
namespace {
class C {}
var_dump(new C);
}
namespace Ns {
class C {}
var_dump(new C);
}
namespace {
use Ns\C;
var_dump(new C);
}
namespace Ns {
use C;
var_dump(new C);
}
namespace {
var_dump(new C);
}
namespace Ns {
var_dump(new C);
}
namespace {
use C;
var_dump(new C);
}
namespace Ns {
use Ns\C;
var_dump(new C);
}
?>
--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) {
}

View File

@ -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));
}
/* }}} */