Skip to content

Zend: Make Closure covariant to callable #15492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Loading