1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Zend: Make Closure a proper subtype of callable (#15492)

This commit is contained in:
Gina Peter Banyard
2025-01-24 17:32:23 +00:00
committed by GitHub
parent c2fddac8ca
commit 32fb2607a3
6 changed files with 30 additions and 0 deletions

View File

@@ -65,6 +65,7 @@ PHP 8.5 UPGRADE NOTES
========================================
- Core:
. Closure is now a proper subtype of callable
. Added support for Closures in constant expressions.
RFC: https://wiki.php.net/rfc/closures_in_const_expr

View File

@@ -0,0 +1,15 @@
--TEST--
Closure should be covariant with callable
--FILE--
<?php
class A {
public function foo(Closure $c): callable {}
}
class B extends A {
public function foo(callable $c): Closure {}
}
?>
OK
--EXPECT--
OK

View File

@@ -23,6 +23,7 @@
#include "zend_execute.h"
#include "zend_inheritance.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "zend_smart_str.h"
#include "zend_operators.h"
#include "zend_exceptions.h"
@@ -490,6 +491,19 @@ static inheritance_status zend_is_class_subtype_of_type(
}
}
/* If the parent has 'callable' as a return type, then Closure satisfies the co-variant check */
if (ZEND_TYPE_FULL_MASK(proto_type) & MAY_BE_CALLABLE) {
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
if (!fe_ce) {
have_unresolved = 1;
} else if (fe_ce == zend_ce_closure) {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
} else {
return INHERITANCE_ERROR;
}
}
zend_type *single_type;
/* Traverse the list of parent types and check if the current child (FE)