Skip to content

Commit d72128a

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: Fix partial application treating methods named assert() as the global assert() (#25771)
2 parents b890258 + 415e68c commit d72128a

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Partial function application on methods named assert() must invoke the method, while real global assert(?) keeps working
3+
--INI--
4+
zend.assertions=1
5+
assert.exception=1
6+
--FILE--
7+
<?php
8+
9+
class Validator {
10+
public function assert(bool $cond, string $label = 'default'): string {
11+
return "INSTANCE:$label:" . ($cond ? 'true' : 'false');
12+
}
13+
14+
public static function assertStatic(bool $cond): string {
15+
return "CTRL:" . ($cond ? 'true' : 'false');
16+
}
17+
}
18+
19+
class StaticValidator {
20+
public static function assert(bool $cond, string $label = 'static'): string {
21+
return "STATIC:$label:" . ($cond ? 'true' : 'false');
22+
}
23+
}
24+
25+
$v = new Validator();
26+
$ctrl = $v->assertStatic(?);
27+
var_dump($ctrl(false));
28+
29+
$p1 = $v->assert(?, 'inst');
30+
var_dump($p1(false));
31+
32+
$p2 = StaticValidator::assert(?, 'cls');
33+
var_dump($p2(false));
34+
35+
$p3 = $v->assert(...);
36+
var_dump($p3(false, 'fcc'));
37+
38+
// Positive control: real global \assert(?) must still invoke global assert()
39+
$realAssert = \assert(?, 'real-assert-msg');
40+
var_dump($realAssert(true));
41+
try {
42+
$realAssert(false);
43+
echo "UNEXPECTED_PASS\n";
44+
} catch (AssertionError $e) {
45+
echo "REAL_ASSERT_ERR:" . $e->getMessage() . "\n";
46+
}
47+
48+
?>
49+
--EXPECT--
50+
string(10) "CTRL:false"
51+
string(19) "INSTANCE:inst:false"
52+
string(16) "STATIC:cls:false"
53+
string(18) "INSTANCE:fcc:false"
54+
bool(true)
55+
REAL_ASSERT_ERR:real-assert-msg

‎Zend/zend_partial.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,10 @@ static zend_ast *zp_compile_forwarding_call(
522522
bool forward_superfluous_args,
523523
zend_ast *stmts_ast, uint32_t const_args)
524524
{
525-
bool is_assert = zend_string_equals(function->common.function_name,
526-
ZSTR_KNOWN(ZEND_STR_ASSERT));
525+
bool is_assert = function->type == ZEND_INTERNAL_FUNCTION
526+
&& !function->common.scope
527+
&& zend_string_equals(function->common.function_name,
528+
ZSTR_KNOWN(ZEND_STR_ASSERT));
527529

528530
zend_ast *args_ast = zend_ast_create_list(0, ZEND_AST_ARG_LIST);
529531
zend_ast *call_ast = NULL;

0 commit comments

Comments
 (0)