Skip to content

Commit 711c847

Browse files
committed
Process array arguments holding only closures after their siblings
Bare closures were already sorted after the other arguments, so a generic callable(T) parameter is resolved from the siblings before the closure body is walked. An array literal of closures was processed in source order, so in each([fn ($x) => ...], 1) the closure saw T unresolved and $x was mixed. Such an array has no side effects when it is created, so it now sorts last too. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
1 parent b613e9c commit 711c847

4 files changed

Lines changed: 193 additions & 81 deletions

File tree

‎src/Analyser/ArgumentsHandler.php‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ public function processArgs(
197197
$bOriginalArg = $args[$b]->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
198198
$aValue = $aOriginalArg !== null ? $aOriginalArg->value : $args[$a]->value;
199199
$bValue = $bOriginalArg !== null ? $bOriginalArg->value : $args[$b]->value;
200-
$aIsClosure = $aValue instanceof Expr\Closure || $aValue instanceof Expr\ArrowFunction;
201-
$bIsClosure = $bValue instanceof Expr\Closure || $bValue instanceof Expr\ArrowFunction;
200+
$aIsClosure = self::isClosureOrClosureArray($aValue);
201+
$bIsClosure = self::isClosureOrClosureArray($bValue);
202202
if ($aIsClosure !== $bIsClosure) {
203203
// closures sort after non-closures so every sibling feeding an
204204
// intrinsic override / generic callable(T) is in scope first
@@ -1017,6 +1017,33 @@ private function addGatheredArgType(array &$types, bool &$unpack, bool &$hasName
10171017
* in-function-call stack and the nested closure types itself from there).
10181018
* Every other argument only reads variant-stable facts off its parameter.
10191019
*/
1020+
/**
1021+
* A closure, or an array literal holding only closures (with literal keys),
1022+
* has no side effects when created, so it can be processed after its siblings.
1023+
*/
1024+
private static function isClosureOrClosureArray(Expr $expr): bool
1025+
{
1026+
if ($expr instanceof Expr\Closure || $expr instanceof Expr\ArrowFunction) {
1027+
return true;
1028+
}
1029+
if (!$expr instanceof Expr\Array_ || $expr->items === []) {
1030+
return false;
1031+
}
1032+
foreach ($expr->items as $item) {
1033+
if ($item->byRef || $item->unpack) {
1034+
return false;
1035+
}
1036+
if ($item->key !== null && !$item->key instanceof Node\Scalar\String_ && !$item->key instanceof Node\Scalar\Int_) {
1037+
return false;
1038+
}
1039+
if (!self::isClosureOrClosureArray($item->value)) {
1040+
return false;
1041+
}
1042+
}
1043+
1044+
return true;
1045+
}
1046+
10201047
private function argConsumesResolvedParameterType(Expr $value): bool
10211048
{
10221049
if ($value instanceof Expr\Closure || $value instanceof Expr\ArrowFunction) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ClosureArrayArgumentBeforeTemplateSibling;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @template T
9+
* @param list<callable(T): mixed> $cbs
10+
* @param T $value
11+
*/
12+
function each(array $cbs, $value): void {}
13+
14+
/**
15+
* @template T
16+
* @param T $value
17+
* @param list<callable(T): mixed> $cbs
18+
*/
19+
function eachSwapped($value, array $cbs): void {}
20+
21+
/**
22+
* @template T
23+
* @param list<array<string, callable(T): mixed>> $cbs
24+
* @param T $value
25+
*/
26+
function eachNested(array $cbs, $value): void {}
27+
28+
function test(): void
29+
{
30+
each([fn ($x) => assertType('1', $x)], 1);
31+
each([function ($x) {
32+
assertType('1', $x);
33+
}, fn ($y) => assertType('1', $y)], 1);
34+
eachSwapped(1, [fn ($x) => assertType('1', $x)]);
35+
eachNested([['a' => fn ($x) => assertType('1', $x)], ['b' => fn ($y) => assertType('1', $y)]], 1);
36+
}

‎turbo-ext/src/ArgumentsHandler.cpp‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pt_property_site pt_ah_method_call_name_site;
231231
pt_property_site pt_ah_array_items_site;
232232
pt_property_site pt_ah_array_item_key_site;
233233
pt_property_site pt_ah_array_item_unpack_site;
234+
pt_property_site pt_ah_array_item_by_ref_site;
234235
pt_property_site pt_ah_array_item_value_site;
235236
pt_property_site pt_ah_string_value_site;
236237
pt_method_site pt_ah_identifier_to_string_site;
@@ -309,6 +310,51 @@ inline bool closureKind(zval *value, bool &isClosure, bool &isArrowFunction)
309310
return true;
310311
}
311312

313+
/* Mirrors isClosureOrClosureArray(): a closure, or an array literal holding
314+
* only closures (with literal keys); false = pending exception */
315+
bool isClosureOrClosureArray(zval *value, bool &out)
316+
{
317+
out = false;
318+
bool isClosure, isArrowFunction;
319+
if (UNEXPECTED(!closureKind(value, isClosure, isArrowFunction))) return false;
320+
if (isClosure || isArrowFunction) {
321+
out = true;
322+
return true;
323+
}
324+
bool isArray = false;
325+
if (UNEXPECTED(!isA(value, PT_CLASS_ARRAY_EXPR, isArray))) return false;
326+
if (!isArray) return true;
327+
zv::Val itemsHold;
328+
zval *items = readProperty(pt_ah_array_items_site, value, PT_LC("items"), itemsHold);
329+
if (UNEXPECTED(items == NULL)) return false;
330+
if (Z_TYPE_P(items) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(items)) == 0) return true;
331+
for (zv::ArrayEntry entry : zv::TableRef(Z_ARRVAL_P(items))) {
332+
zval *item = entry.value().deref().raw();
333+
zv::Val byRefHold, unpackHold, keyHold, itemValueHold;
334+
zval *byRef = readProperty(pt_ah_array_item_by_ref_site, item, PT_LC("byRef"), byRefHold);
335+
if (UNEXPECTED(byRef == NULL)) return false;
336+
if (zend_is_true(byRef)) return true;
337+
zval *unpack = readProperty(pt_ah_array_item_unpack_site, item, PT_LC("unpack"), unpackHold);
338+
if (UNEXPECTED(unpack == NULL)) return false;
339+
if (zend_is_true(unpack)) return true;
340+
zval *key = readProperty(pt_ah_array_item_key_site, item, PT_LC("key"), keyHold);
341+
if (UNEXPECTED(key == NULL)) return false;
342+
if (Z_TYPE_P(key) != IS_NULL) {
343+
bool isString = false, isInt = false;
344+
if (UNEXPECTED(!isA(key, PT_CLASS_SCALAR_STRING, isString))) return false;
345+
if (!isString && UNEXPECTED(!isA(key, PT_CLASS_SCALAR_INT, isInt))) return false;
346+
if (!isString && !isInt) return true;
347+
}
348+
zval *itemValue = readProperty(pt_ah_array_item_value_site, item, PT_LC("value"), itemValueHold);
349+
if (UNEXPECTED(itemValue == NULL)) return false;
350+
bool itemIsClosure = false;
351+
if (UNEXPECTED(!isClosureOrClosureArray(itemValue, itemIsClosure))) return false;
352+
if (!itemIsClosure) return true;
353+
}
354+
out = true;
355+
return true;
356+
}
357+
312358
/* $callLike->getArgs() (pt_call_like_args(): the `args` slot,
313359
* or the method's result) as an owned value; UNDEF = pending exception */
314360
zv::Val callArgs(zval *callLike)
@@ -1562,7 +1608,7 @@ class ArgumentsHandler
15621608
Walk *walk = NULL;
15631609
};
15641610

1565-
/* the usort() comparator: closures after non-closures, then the
1611+
/* the usort() comparator: closures (and closure-only arrays) after the rest, then the
15661612
* normalized-only arguments after the original ones, by index / start
15671613
* token position (a strict order together with the stable fallback on
15681614
* the original position, so any stable sort agrees with usort()) */
@@ -1612,9 +1658,7 @@ class ArgumentsHandler
16121658
zv::Val valueHold;
16131659
zval *value = argValue(record.sortHasOriginal ? original : record.arg, valueHold);
16141660
if (UNEXPECTED(value == NULL)) return false;
1615-
bool isClosure, isArrowFunction;
1616-
if (UNEXPECTED(!closureKind(value, isClosure, isArrowFunction))) return false;
1617-
record.sortIsClosure = isClosure || isArrowFunction;
1661+
if (UNEXPECTED(!isClosureOrClosureArray(value, record.sortIsClosure))) return false;
16181662
record.sortStartTokenPos = -1;
16191663
if (record.sortHasOriginal) {
16201664
if (UNEXPECTED(Z_TYPE_P(original) != IS_OBJECT)) {

0 commit comments

Comments
 (0)