Skip to content

Commit cf008aa

Browse files
committed
Fix unbound closure
1 parent ccf90d5 commit cf008aa

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

Zend/tests/return_types/025_2.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Return type of self is allowed in closure but $this return value must be checked as closure might not be bound to a class
3+
--FILE--
4+
<?php
5+
6+
$c = function(): self { return $this; };
7+
try {
8+
var_dump($c());
9+
} catch (Throwable $e) {
10+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
11+
}
12+
?>
13+
--EXPECT--
14+
Error: Using $this when not in object context

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,11 @@ static bool zend_is_this_instance_of_name(const zend_string *type_name)
26722672

26732673
static bool zend_is_this_valid_for_return_type(zend_type type)
26742674
{
2675+
/* Closures can be bound to a class scope, however it might not and this must type error */
2676+
if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
2677+
return false;
2678+
}
2679+
26752680
if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_OBJECT|MAY_BE_STATIC)) {
26762681
return true;
26772682
}

0 commit comments

Comments
 (0)