Skip to content
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
2 changes: 1 addition & 1 deletion src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
final class TurboExtensionEnabler
{

public const EXPECTED_EXTENSION_VERSION = '9cebb2a';
public const EXPECTED_EXTENSION_VERSION = '67f72d8';

private static bool $active = false;

Expand Down
12 changes: 9 additions & 3 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,14 @@ private static function optimizeConstantArrays(array $types): array
}

$results = [];
$emptyArrays = [];
$eachIsOversized = true;
foreach ($types as $type) {
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
$emptyArrays[] = $type;
continue;
}

$isOversized = false;
$result = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$isOversized): Type {
if (!$type instanceof ConstantArrayType) {
Expand Down Expand Up @@ -1290,7 +1296,7 @@ private static function optimizeConstantArrays(array $types): array
$results[] = $result;
}

if ($eachIsOversized) {
if ($eachIsOversized && $results !== []) {
$eachIsList = true;
$keyTypes = [];
$valueTypes = [];
Expand All @@ -1317,10 +1323,10 @@ private static function optimizeConstantArrays(array $types): array
$accessories[] = new NonEmptyArrayType();
$accessories[] = new OversizedArrayType();

return [self::intersect(new ArrayType($keyType, $valueType), ...$accessories)];
return [...$emptyArrays, self::intersect(new ArrayType($keyType, $valueType), ...$accessories)];
}

return $results;
return [...$emptyArrays, ...$results];
}

/**
Expand Down
215 changes: 215 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15293.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php declare(strict_types = 1);

namespace Bug15293;

use function PHPStan\Testing\assertType;

class Foo
{

public const MAP = [
'a' => [
'a0' => [0, 0, 0],
'a1' => [1, 1, 1],
'a2' => [2, 2, 2],
'a3' => [3, 3, 3],
'a4' => [4, 4, 4],
'a5' => [5, 5, 5],
'a6' => [6, 6, 6],
'a7' => [7, 7, 7],
'a8' => [8, 8, 8],
'a9' => [9, 9, 9],
'a10' => [10, 10, 10],
'a11' => [11, 11, 11],
'a12' => [12, 12, 12],
'a13' => [13, 13, 13],
'a14' => [14, 14, 14],
'a15' => [15, 15, 15],
'a16' => [16, 16, 16],
'a17' => [17, 17, 17],
'a18' => [18, 18, 18],
'a19' => [19, 19, 19],
'a20' => [20, 20, 20],
'a21' => [21, 21, 21],
'a22' => [22, 22, 22],
'a23' => [23, 23, 23],
'a24' => [24, 24, 24],
'a25' => [25, 25, 25],
'a26' => [26, 26, 26],
'a27' => [27, 27, 27],
'a28' => [28, 28, 28],
'a29' => [29, 29, 29],
],
'b' => [
'b0' => [30, 30, 30],
'b1' => [31, 31, 31],
'b2' => [32, 32, 32],
'b3' => [33, 33, 33],
'b4' => [34, 34, 34],
'b5' => [35, 35, 35],
'b6' => [36, 36, 36],
'b7' => [37, 37, 37],
'b8' => [38, 38, 38],
'b9' => [39, 39, 39],
'b10' => [40, 40, 40],
'b11' => [41, 41, 41],
'b12' => [42, 42, 42],
'b13' => [43, 43, 43],
'b14' => [44, 44, 44],
'b15' => [45, 45, 45],
'b16' => [46, 46, 46],
'b17' => [47, 47, 47],
'b18' => [48, 48, 48],
'b19' => [49, 49, 49],
'b20' => [50, 50, 50],
'b21' => [51, 51, 51],
'b22' => [52, 52, 52],
'b23' => [53, 53, 53],
'b24' => [54, 54, 54],
'b25' => [55, 55, 55],
'b26' => [56, 56, 56],
'b27' => [57, 57, 57],
'b28' => [58, 58, 58],
'b29' => [59, 59, 59],
],
'c' => [
'c0' => [60, 60, 60],
'c1' => [61, 61, 61],
'c2' => [62, 62, 62],
'c3' => [63, 63, 63],
'c4' => [64, 64, 64],
'c5' => [65, 65, 65],
'c6' => [66, 66, 66],
'c7' => [67, 67, 67],
'c8' => [68, 68, 68],
'c9' => [69, 69, 69],
'c10' => [70, 70, 70],
'c11' => [71, 71, 71],
'c12' => [72, 72, 72],
'c13' => [73, 73, 73],
'c14' => [74, 74, 74],
'c15' => [75, 75, 75],
'c16' => [76, 76, 76],
'c17' => [77, 77, 77],
'c18' => [78, 78, 78],
'c19' => [79, 79, 79],
'c20' => [80, 80, 80],
'c21' => [81, 81, 81],
'c22' => [82, 82, 82],
'c23' => [83, 83, 83],
'c24' => [84, 84, 84],
'c25' => [85, 85, 85],
'c26' => [86, 86, 86],
'c27' => [87, 87, 87],
'c28' => [88, 88, 88],
'c29' => [89, 89, 89],
],
'd' => [],
];

public const MAP_WITHOUT_EMPTY = [
'a' => [
'a0' => [0, 0, 0],
'a1' => [1, 1, 1],
'a2' => [2, 2, 2],
'a3' => [3, 3, 3],
'a4' => [4, 4, 4],
'a5' => [5, 5, 5],
'a6' => [6, 6, 6],
'a7' => [7, 7, 7],
'a8' => [8, 8, 8],
'a9' => [9, 9, 9],
'a10' => [10, 10, 10],
'a11' => [11, 11, 11],
'a12' => [12, 12, 12],
'a13' => [13, 13, 13],
'a14' => [14, 14, 14],
'a15' => [15, 15, 15],
'a16' => [16, 16, 16],
'a17' => [17, 17, 17],
'a18' => [18, 18, 18],
'a19' => [19, 19, 19],
'a20' => [20, 20, 20],
'a21' => [21, 21, 21],
'a22' => [22, 22, 22],
'a23' => [23, 23, 23],
'a24' => [24, 24, 24],
'a25' => [25, 25, 25],
'a26' => [26, 26, 26],
'a27' => [27, 27, 27],
'a28' => [28, 28, 28],
'a29' => [29, 29, 29],
],
'b' => [
'b0' => [30, 30, 30],
'b1' => [31, 31, 31],
'b2' => [32, 32, 32],
'b3' => [33, 33, 33],
'b4' => [34, 34, 34],
'b5' => [35, 35, 35],
'b6' => [36, 36, 36],
'b7' => [37, 37, 37],
'b8' => [38, 38, 38],
'b9' => [39, 39, 39],
'b10' => [40, 40, 40],
'b11' => [41, 41, 41],
'b12' => [42, 42, 42],
'b13' => [43, 43, 43],
'b14' => [44, 44, 44],
'b15' => [45, 45, 45],
'b16' => [46, 46, 46],
'b17' => [47, 47, 47],
'b18' => [48, 48, 48],
'b19' => [49, 49, 49],
'b20' => [50, 50, 50],
'b21' => [51, 51, 51],
'b22' => [52, 52, 52],
'b23' => [53, 53, 53],
'b24' => [54, 54, 54],
'b25' => [55, 55, 55],
'b26' => [56, 56, 56],
'b27' => [57, 57, 57],
'b28' => [58, 58, 58],
'b29' => [59, 59, 59],
],
'c' => [
'c0' => [60, 60, 60],
'c1' => [61, 61, 61],
'c2' => [62, 62, 62],
'c3' => [63, 63, 63],
'c4' => [64, 64, 64],
'c5' => [65, 65, 65],
'c6' => [66, 66, 66],
'c7' => [67, 67, 67],
'c8' => [68, 68, 68],
'c9' => [69, 69, 69],
'c10' => [70, 70, 70],
'c11' => [71, 71, 71],
'c12' => [72, 72, 72],
'c13' => [73, 73, 73],
'c14' => [74, 74, 74],
'c15' => [75, 75, 75],
'c16' => [76, 76, 76],
'c17' => [77, 77, 77],
'c18' => [78, 78, 78],
'c19' => [79, 79, 79],
'c20' => [80, 80, 80],
'c21' => [81, 81, 81],
'c22' => [82, 82, 82],
'c23' => [83, 83, 83],
'c24' => [84, 84, 84],
'c25' => [85, 85, 85],
'c26' => [86, 86, 86],
'c27' => [87, 87, 87],
'c28' => [88, 88, 88],
'c29' => [89, 89, 89],
],
];

public function doFoo(string $key): void
{
assertType('array{}|(non-empty-array<literal-string&lowercase-string&non-falsy-string, list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array>&oversized-array)', self::MAP[$key]);
assertType('non-empty-array<literal-string&lowercase-string&non-falsy-string, list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array>&oversized-array', self::MAP_WITHOUT_EMPTY[$key]);
}

}
24 changes: 19 additions & 5 deletions turbo-ext/src/TypeCombinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,8 +2184,20 @@ class TypeCombinator
}

zv::Arr results = zv::Arr::create(countOf(types.raw()));
zv::Arr emptyArrays = zv::Arr::create(0);
bool eachIsOversized = true;
for (zv::ArrayEntry entry : zv::ArrRef(types.raw())) {
zend_long isConstantArray = callTrinary(entry.value().raw(), PT_LC("isconstantarray"));
PT_FAIL_IF_NEG(isConstantArray);
if (isConstantArray == PT_TRI_YES) {
zend_long atLeastOnce = callTrinary(entry.value().raw(), PT_LC("isiterableatleastonce"));
PT_FAIL_IF_NEG(atLeastOnce);
if (atLeastOnce == PT_TRI_NO) {
emptyArrays.push(zv::Ref(entry.value().raw()));
continue;
}
}

zval isOversized;
ZVAL_FALSE(&isOversized);
zv::Val callback = pt_type_native_callback(generalizeOversizedCallback, &isOversized, NULL);
Expand All @@ -2200,7 +2212,7 @@ class TypeCombinator
results.push(std::move(result));
}

if (eachIsOversized) {
if (eachIsOversized && zend_hash_num_elements(results.table()) > 0) {
bool eachIsList = true;
TypeList keyTypes;
TypeList valueTypes;
Expand Down Expand Up @@ -2236,12 +2248,14 @@ class TypeCombinator

zv::Val oversized = oversizedArrayOf(keyType.raw(), valueType.raw(), eachIsList);
PT_FAIL_IF_UNDEF(oversized);
zv::Arr single = zv::Arr::create(1);
single.push(std::move(oversized));
return zv::Val(std::move(single));
emptyArrays.push(std::move(oversized));
return zv::Val(std::move(emptyArrays));
}

return zv::Val(std::move(results));
for (zv::ArrayEntry entry : zv::ArrRef(results.raw())) {
emptyArrays.push(zv::Ref(entry.value().raw()));
}
return zv::Val(std::move(emptyArrays));
}

/* the key signature of a ConstantArrayType the stage-1 grouping and the
Expand Down
Loading