Skip to content

Commit 8e0ab07

Browse files
committed
Start array shape builders without a known key order
ConstantArrayTypeBuilder::createEmpty() no longer claims that the keys come in the order they are set. Builders of literal arrays, lists and arrays copied from a source that knows its order call makeKeyOrderKnown(), and createFromConstantArray() keeps the order knowledge of its source. With bleeding edge, values appended in no known order make a list only maybe. Loop widening keeps the key order of two shapes that list the same keys in the same order, and forgets it otherwise. Amp-Thread-ID: https://ampcode.com/threads/T-01a230f4-34d7-76c1-abde-c90173e47a85
1 parent 5bff136 commit 8e0ab07

42 files changed

Lines changed: 1041 additions & 560 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/Analyser/ExprHandler/Helper/FuncCallScopeEffectsHelper.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,14 @@ private function getArrayFunctionAppendingType(FunctionReflection $functionRefle
433433
$newArrayTypes = [];
434434
$prepend = $functionReflection->getName() === 'array_unshift';
435435
foreach ($constantArrays as $constantArray) {
436-
$arrayTypeBuilder = $prepend ? ConstantArrayTypeBuilder::createEmpty() : ConstantArrayTypeBuilder::createFromConstantArray($constantArray);
436+
if ($prepend) {
437+
$arrayTypeBuilder = ConstantArrayTypeBuilder::createEmpty();
438+
if ($constantArray->isKeyOrderKnown()) {
439+
$arrayTypeBuilder->makeKeyOrderKnown();
440+
}
441+
} else {
442+
$arrayTypeBuilder = ConstantArrayTypeBuilder::createFromConstantArray($constantArray);
443+
}
437444

438445
$setOffsetValueTypes(
439446
$scope,
@@ -490,6 +497,9 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
490497
// unpack value type" and are now subsumed by the
491498
// unsealed slot).
492499
$builder = ConstantArrayTypeBuilder::createEmpty();
500+
if ($constantArrays[0]->isKeyOrderKnown()) {
501+
$builder->makeKeyOrderKnown();
502+
}
493503
$intValues = [];
494504
foreach ($constantArrays[0]->getKeyTypes() as $i => $keyType) {
495505
$valueType = $constantArrays[0]->getValueTypes()[$i];

‎src/Analyser/MutatingScope.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6620,7 +6620,26 @@ private function generalizeConstantFloats(array $a, array $b, float $entryMin, f
66206620
private function generalizeShapesKeyByKey(Type $a, Type $b, int $depth, Type $allA, Type $allB): Type
66216621
{
66226622
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
6623-
foreach (TypeUtils::flattenTypes(TypeCombinator::union($a->getIterableKeyType(), $b->getIterableKeyType())) as $keyType) {
6623+
$keyTypes = TypeUtils::flattenTypes(TypeCombinator::union($a->getIterableKeyType(), $b->getIterableKeyType()));
6624+
6625+
// the shape keeps the key order of the two shapes it widens when they list the same keys in it; keys
6626+
// only one of them lists can be added in any order by the passes of a loop
6627+
$aShapes = $a->getConstantArrays();
6628+
$bShapes = $b->getConstantArrays();
6629+
if (count($aShapes) === 1 && count($bShapes) === 1) {
6630+
$mergedKeyTypes = $aShapes[0]->getKeyTypesInMergedOrderWith($bShapes[0]);
6631+
if (
6632+
$mergedKeyTypes !== null
6633+
&& count($mergedKeyTypes) === count($keyTypes)
6634+
&& count($mergedKeyTypes) === count($aShapes[0]->getKeyTypes())
6635+
&& count($mergedKeyTypes) === count($bShapes[0]->getKeyTypes())
6636+
) {
6637+
$keyTypes = $mergedKeyTypes;
6638+
$resultArrayBuilder->makeKeyOrderKnown();
6639+
}
6640+
}
6641+
6642+
foreach ($keyTypes as $keyType) {
66246643
$aHasOffset = $a->hasOffsetValueType($keyType);
66256644
$bHasOffset = $b->hasOffsetValueType($keyType);
66266645
$optional = !$aHasOffset->and($bHasOffset)->negate()->no();

‎src/PhpDoc/ConstExprNodeResolver.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function resolve(ConstExprNode $node, NameScope $nameScope): Type
121121
private function resolveArrayNode(ConstExprArrayNode $node, NameScope $nameScope): Type
122122
{
123123
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
124+
$arrayBuilder->makeKeyOrderKnown();
124125
foreach ($node->items as $item) {
125126
if ($item->key === null) {
126127
$key = null;

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback, ?PhpV
553553
}
554554

555555
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
556+
$arrayBuilder->makeKeyOrderKnown();
556557
$isList = null;
557558
$hasOffsetValueTypes = [];
558559
foreach ($expr->items as $arrayItem) {

‎src/Reflection/Php/PhpClassReflectionExtension.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ private function createMethod(
640640
&& strtolower($methodReflection->getName()) === 'cases'
641641
) {
642642
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
643+
$arrayBuilder->makeKeyOrderKnown();
643644
foreach (array_keys($classReflection->getEnumCases()) as $name) {
644645
$arrayBuilder->setOffsetValueType(null, new EnumCaseObjectType($classReflection->getName(), $name));
645646
}

‎src/Type/ArrayAlgebra.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ private static function tryIntersectShapeWithArray(ConstantArrayType $constArray
285285
}
286286

287287
$newArray = ConstantArrayTypeBuilder::createEmpty();
288+
$newArray->makeKeyOrderKnown();
288289
// Preserve unsealed extras from the source shape so the
289290
// rebuild doesn't silently turn `array{k: int, ...} & X`
290291
// into a sealed `array{k: int}` — intersect with the other
@@ -367,6 +368,7 @@ private static function intersectDefiniteConstantArrays(ConstantArrayType $a, Co
367368
}
368369

369370
$newArray = ConstantArrayTypeBuilder::createEmpty();
371+
$newArray->makeKeyOrderKnown();
370372

371373
if ($bothUnsealed) {
372374
[$unsealedKey, $unsealedValue] = ArrayKeyTemplateHelper::intersectEntryTypes($aUnsealed, $bUnsealed);

‎src/Type/ArrayType.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ public function intersectKeyArray(Type $otherArraysType): Type
824824
private function intersectConstantArrayShape(ConstantArrayType $constantArray): Type
825825
{
826826
$builder = ConstantArrayTypeBuilder::createEmpty();
827+
if ($constantArray->isKeyOrderKnown()) {
828+
$builder->makeKeyOrderKnown();
829+
}
827830

828831
$valueType = $this->getIterableValueType();
829832
$keyType = $this->getIterableKeyType();

‎src/Type/Constant/ConstantArrayType.php‎

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ public function forgetKeyOrder(): self
275275
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList, $this->unsealedDeclaration ?? $this->unsealed, false);
276276
}
277277

278+
private function knowKeyOrder(): self
279+
{
280+
if ($this->keyOrderKnown) {
281+
return $this;
282+
}
283+
284+
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList, $this->unsealedDeclaration ?? $this->unsealed, true);
285+
}
286+
278287
/**
279288
* Whether both shapes know their key order and list the keys they share in the same order.
280289
* Two such shapes that list them in different orders never hold the same array.
@@ -581,8 +590,8 @@ public function getAllArrays(): array
581590
}
582591

583592
$builder = ConstantArrayTypeBuilder::createEmpty();
584-
if (!$this->keyOrderKnown) {
585-
$builder->forgetKeyOrder();
593+
if ($this->keyOrderKnown) {
594+
$builder->makeKeyOrderKnown();
586595
}
587596
$builder->disableArrayDegradation();
588597
foreach ($keys as $i) {
@@ -1982,8 +1991,8 @@ public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
19821991
$length = $finiteType->getValue();
19831992

19841993
$builder = ConstantArrayTypeBuilder::createEmpty();
1985-
if (!$this->keyOrderKnown) {
1986-
$builder->forgetKeyOrder();
1994+
if ($this->keyOrderKnown) {
1995+
$builder->makeKeyOrderKnown();
19871996
}
19881997

19891998
$keyTypesCount = count($this->keyTypes);
@@ -2004,8 +2013,8 @@ public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
20042013
public function fillKeysArray(Type $valueType): Type
20052014
{
20062015
$builder = ConstantArrayTypeBuilder::createEmpty();
2007-
if (!$this->keyOrderKnown) {
2008-
$builder->forgetKeyOrder();
2016+
if ($this->keyOrderKnown) {
2017+
$builder->makeKeyOrderKnown();
20092018
}
20102019

20112020
foreach ($this->valueTypes as $i => $keyType) {
@@ -2040,8 +2049,8 @@ public function fillKeysArray(Type $valueType): Type
20402049
public function flipArray(): Type
20412050
{
20422051
$builder = ConstantArrayTypeBuilder::createEmpty();
2043-
if (!$this->keyOrderKnown) {
2044-
$builder->forgetKeyOrder();
2052+
if ($this->keyOrderKnown) {
2053+
$builder->makeKeyOrderKnown();
20452054
}
20462055

20472056
foreach ($this->keyTypes as $i => $keyType) {
@@ -2077,8 +2086,8 @@ public function flipArray(): Type
20772086
public function intersectKeyArray(Type $otherArraysType): Type
20782087
{
20792088
$builder = ConstantArrayTypeBuilder::createEmpty();
2080-
if (!$this->keyOrderKnown) {
2081-
$builder->forgetKeyOrder();
2089+
if ($this->keyOrderKnown) {
2090+
$builder->makeKeyOrderKnown();
20822091
}
20832092

20842093
foreach ($this->keyTypes as $i => $keyType) {
@@ -2123,8 +2132,8 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type
21232132
}
21242133

21252134
$builder = ConstantArrayTypeBuilder::createEmpty();
2126-
if (!$this->keyOrderKnown) {
2127-
$builder->forgetKeyOrder();
2135+
if ($this->keyOrderKnown) {
2136+
$builder->makeKeyOrderKnown();
21282137
}
21292138

21302139
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
@@ -2304,8 +2313,8 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
23042313
}
23052314

23062315
$builder = ConstantArrayTypeBuilder::createEmpty();
2307-
if (!$this->keyOrderKnown) {
2308-
$builder->forgetKeyOrder();
2316+
if ($this->keyOrderKnown) {
2317+
$builder->makeKeyOrderKnown();
23092318
}
23102319

23112320
$nonOptionalElementsCount = 0;
@@ -2406,8 +2415,8 @@ public function spliceArray(Type $offsetType, Type $lengthType, Type $replacemen
24062415
$optionalKeysBeforeReplacement = 0;
24072416

24082417
$builder = ConstantArrayTypeBuilder::createEmpty();
2409-
if (!$this->keyOrderKnown) {
2410-
$builder->forgetKeyOrder();
2418+
if ($this->keyOrderKnown) {
2419+
$builder->makeKeyOrderKnown();
24112420
}
24122421
for ($i = 0;; $i++) {
24132422
$isOptional = $this->isOptionalKey($i);
@@ -2541,8 +2550,8 @@ public function truncateListToSize(Type $sizeType): Type
25412550
}
25422551

25432552
$builder = ConstantArrayTypeBuilder::createEmpty();
2544-
if (!$this->keyOrderKnown) {
2545-
$builder->forgetKeyOrder();
2553+
if ($this->keyOrderKnown) {
2554+
$builder->makeKeyOrderKnown();
25462555
}
25472556
foreach ($builderData as [$offsetType, $valueType, $optional]) {
25482557
$builder->setOffsetValueType($offsetType, $valueType, $optional);
@@ -2812,8 +2821,8 @@ private function removeLastElements(int $length): self
28122821
private function removeFirstElements(int $length, bool $reindex = true): Type
28132822
{
28142823
$builder = ConstantArrayTypeBuilder::createEmpty();
2815-
if (!$this->keyOrderKnown) {
2816-
$builder->forgetKeyOrder();
2824+
if ($this->keyOrderKnown) {
2825+
$builder->makeKeyOrderKnown();
28172826
}
28182827

28192828
$optionalKeysIgnored = 0;
@@ -3037,7 +3046,10 @@ public function inRenumberingOrder(): self
30373046
$optionalKeys[] = count($keyTypes) - 1;
30383047
}
30393048

3040-
return $this->recreate($keyTypes, $valueTypes, $this->nextAutoIndexes, $optionalKeys, $this->isList, $this->unsealed);
3049+
$renumbered = $this->recreate($keyTypes, $valueTypes, $this->nextAutoIndexes, $optionalKeys, $this->isList, $this->unsealed);
3050+
3051+
// with only integer keys, each position holds any of the values, so the positions are all the order there is
3052+
return count($integerValueTypes) === count($this->keyTypes) ? $renumbered->knowKeyOrder() : $renumbered;
30413053
}
30423054

30433055
private function listsIntegerKey(): bool
@@ -3823,41 +3835,62 @@ public function mergeWith(self $otherArray): self
38233835
*/
38243836
public function inMergedKeyOrderOf(self $a, self $b): self
38253837
{
3826-
if (!$a->sharesKeyOrderWith($b)) {
3838+
$mergedKeyTypes = $a->getKeyTypesInMergedOrderWith($b);
3839+
if ($mergedKeyTypes === null) {
38273840
return $this->forgetKeyOrder();
38283841
}
38293842

3830-
$aKeyIndexMap = $a->getKeyIndexMap();
3831-
$bKeyIndexMap = $b->getKeyIndexMap();
38323843
$positions = [];
3844+
foreach ($mergedKeyTypes as $position => $keyType) {
3845+
$positions[$keyType->getValue()] = $position;
3846+
}
3847+
3848+
$indices = [];
3849+
foreach ($this->keyTypes as $k => $keyType) {
3850+
$indices[$positions[$keyType->getValue()]] = $k;
3851+
}
3852+
ksort($indices);
3853+
3854+
return $this->reorderKeys(array_values($indices));
3855+
}
3856+
3857+
/**
3858+
* The keys of both shapes in an order both of them keep: a key only one of them lists stays between the keys
3859+
* it is listed between there. Null when the shapes do not list the keys they share in one known order.
3860+
*
3861+
* @return list<ConstantIntegerType|ConstantStringType>|null
3862+
*/
3863+
public function getKeyTypesInMergedOrderWith(self $other): ?array
3864+
{
3865+
if (!$this->sharesKeyOrderWith($other)) {
3866+
return null;
3867+
}
3868+
3869+
$keyIndexMap = $this->getKeyIndexMap();
3870+
$otherKeyIndexMap = $other->getKeyIndexMap();
3871+
$keyTypes = [];
38333872
$i = 0;
38343873
$j = 0;
3835-
$aCount = count($a->keyTypes);
3836-
$bCount = count($b->keyTypes);
3837-
while ($i < $aCount || $j < $bCount) {
3838-
if ($i < $aCount && !array_key_exists($a->keyTypes[$i]->getValue(), $bKeyIndexMap)) {
3839-
$positions[$a->keyTypes[$i]->getValue()] = count($positions);
3874+
$count = count($this->keyTypes);
3875+
$otherCount = count($other->keyTypes);
3876+
while ($i < $count || $j < $otherCount) {
3877+
if ($i < $count && !array_key_exists($this->keyTypes[$i]->getValue(), $otherKeyIndexMap)) {
3878+
$keyTypes[] = $this->keyTypes[$i];
38403879
$i++;
38413880
continue;
38423881
}
3843-
if ($j < $bCount && !array_key_exists($b->keyTypes[$j]->getValue(), $aKeyIndexMap)) {
3844-
$positions[$b->keyTypes[$j]->getValue()] = count($positions);
3882+
if ($j < $otherCount && !array_key_exists($other->keyTypes[$j]->getValue(), $keyIndexMap)) {
3883+
$keyTypes[] = $other->keyTypes[$j];
38453884
$j++;
38463885
continue;
38473886
}
38483887
// the next key of both is the same shared key
3849-
$positions[$a->keyTypes[$i]->getValue()] = count($positions);
3888+
$keyTypes[] = $this->keyTypes[$i];
38503889
$i++;
38513890
$j++;
38523891
}
38533892

3854-
$indices = [];
3855-
foreach ($this->keyTypes as $k => $keyType) {
3856-
$indices[$positions[$keyType->getValue()]] = $k;
3857-
}
3858-
ksort($indices);
3859-
3860-
return $this->reorderKeys(array_values($indices));
3893+
return $keyTypes;
38613894
}
38623895

38633896
private function legacyMergeWith(self $otherArray): self
@@ -4089,8 +4122,8 @@ public function makeList(): Type
40894122

40904123
if (count($keptPositions) < count($this->keyTypes)) {
40914124
$builder = ConstantArrayTypeBuilder::createEmpty();
4092-
if (!$this->keyOrderKnown) {
4093-
$builder->forgetKeyOrder();
4125+
if ($this->keyOrderKnown) {
4126+
$builder->makeKeyOrderKnown();
40944127
}
40954128
foreach ($keptPositions as $position) {
40964129
$builder->setOffsetValueType(
@@ -4173,8 +4206,8 @@ public function makeAllArrayKeysOptional(): Type
41734206
public function changeKeyCaseArray(?int $case): Type
41744207
{
41754208
$builder = ConstantArrayTypeBuilder::createEmpty();
4176-
if (!$this->keyOrderKnown) {
4177-
$builder->forgetKeyOrder();
4209+
if ($this->keyOrderKnown) {
4210+
$builder->makeKeyOrderKnown();
41784211
}
41794212
foreach ($this->keyTypes as $i => $keyType) {
41804213
if ($keyType instanceof ConstantStringType) {
@@ -4200,8 +4233,8 @@ public function filterArrayRemovingFalsey(): Type
42004233
{
42014234
$falseyTypes = StaticTypeFactory::falsey();
42024235
$builder = ConstantArrayTypeBuilder::createEmpty();
4203-
if (!$this->keyOrderKnown) {
4204-
$builder->forgetKeyOrder();
4236+
if ($this->keyOrderKnown) {
4237+
$builder->makeKeyOrderKnown();
42054238
}
42064239
foreach ($this->keyTypes as $i => $keyType) {
42074240
$value = $this->valueTypes[$i];
@@ -4424,8 +4457,8 @@ public function getFiniteTypes(): array
44244457
}
44254458

44264459
$emptyPartial = ConstantArrayTypeBuilder::createEmpty();
4427-
if (!$this->keyOrderKnown) {
4428-
$emptyPartial->forgetKeyOrder();
4460+
if ($this->keyOrderKnown) {
4461+
$emptyPartial->makeKeyOrderKnown();
44294462
}
44304463

44314464
/** @var list<ConstantArrayTypeBuilder> $partials */

0 commit comments

Comments
 (0)