Skip to content

Commit bddf27c

Browse files
ondrejmirtesclaude
andcommitted
Keep an explicit key of an unsealed shape as an optional never key when subtracting its offset
ConstantArrayType::tryRemove() subtracted HasOffsetType and HasOffsetValueType by unsetting the offset. On an unsealed shape whose extras can hold that offset, dropping the explicit key lets the extras supply it again with their value type, so the "subtracted" shape is wider than the original: array{a?: string, ...} minus hasOffset('a') was array{...}, and array{a?: string, b: int, ...<string, int>} re-admitted 'a' as int. The falsey isset() narrowing from 77527df and the falsey array_key_exists() narrowing both go through this subtraction, and merging the widened branch back collapsed the subject to array. The key now stays as an optional never key instead - absent, but still fencing the offset off from the extras - and branch merges restore the original shape. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xj1fBpXNVT34mRk1GQBxJF
1 parent 486821c commit bddf27c

6 files changed

Lines changed: 279 additions & 95 deletions

File tree

‎src/Type/Constant/ConstantArrayType.php‎

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use PHPStan\Type\IsSuperTypeOfResult;
5555
use PHPStan\Type\MixedType;
5656
use PHPStan\Type\NeverType;
57+
use PHPStan\Type\NonAcceptingNeverType;
5758
use PHPStan\Type\NullType;
5859
use PHPStan\Type\ObjectWithoutClassType;
5960
use PHPStan\Type\RecursionGuard;
@@ -2793,15 +2794,7 @@ public function tryRemove(Type $typeToRemove): ?Type
27932794
}
27942795

27952796
if ($valueIsSuperType->yes()) {
2796-
$unsetResult = $this->unsetOffset($offsetType, true);
2797-
// When the source was definitely a list but the post-unset shape
2798-
// definitely isn't (e.g. unsetting a non-optional leading key
2799-
// creates a hole), no value of $this could have lacked the
2800-
// removed key — the subtraction yields the empty set.
2801-
if ($this->isList->yes() && $unsetResult->isList()->no()) {
2802-
return new NeverType();
2803-
}
2804-
return $unsetResult;
2797+
return $this->removeOffset($offsetType);
28052798
}
28062799

28072800
$newValueType = TypeCombinator::remove($currentValueType, $valueTypeToRemove);
@@ -2822,20 +2815,59 @@ public function tryRemove(Type $typeToRemove): ?Type
28222815
}
28232816

28242817
if ($typeToRemove instanceof HasOffsetType) {
2825-
$unsetResult = $this->unsetOffset($typeToRemove->getOffsetType(), true);
2826-
// When the source was definitely a list but the post-unset shape
2827-
// definitely isn't (e.g. unsetting a non-optional leading key
2828-
// creates a hole), no value of $this could have lacked the
2829-
// removed key — the subtraction yields the empty set.
2830-
if ($this->isList->yes() && $unsetResult->isList()->no()) {
2831-
return new NeverType();
2832-
}
2833-
return $unsetResult;
2818+
return $this->removeOffset($typeToRemove->getOffsetType());
28342819
}
28352820

28362821
return null;
28372822
}
28382823

2824+
/**
2825+
* The values of this shape lacking the offset. Unlike unsetOffset(), an
2826+
* explicit key the unsealed extras can also hold becomes an optional never
2827+
* key - dropping it would let the extras supply it again, widening the
2828+
* shape instead of subtracting from it.
2829+
*/
2830+
private function removeOffset(Type $offsetType): Type
2831+
{
2832+
$unsetResult = $this->unsetOffset($offsetType, true);
2833+
// When the source was definitely a list but the post-unset shape
2834+
// definitely isn't (e.g. unsetting a non-optional leading key
2835+
// creates a hole), no value of $this could have lacked the
2836+
// removed key — the subtraction yields the empty set.
2837+
if ($this->isList->yes() && $unsetResult->isList()->no()) {
2838+
return new NeverType();
2839+
}
2840+
2841+
if ($this->unsealed === null || !$this->isUnsealed()->yes()) {
2842+
return $unsetResult;
2843+
}
2844+
2845+
$offsetType = $offsetType->toArrayKey();
2846+
$offsetValues = $offsetType->getConstantScalarValues();
2847+
if (count($offsetValues) !== 1 || $this->unsealed[0]->isSuperTypeOf($offsetType)->no()) {
2848+
return $unsetResult;
2849+
}
2850+
2851+
$offsetValue = $offsetValues[0];
2852+
if (!is_int($offsetValue) && !is_string($offsetValue)) {
2853+
return $unsetResult;
2854+
}
2855+
2856+
$i = $this->getKeyIndexMap()[$offsetValue] ?? null;
2857+
if ($i === null) {
2858+
return $unsetResult;
2859+
}
2860+
2861+
$valueTypes = $this->valueTypes;
2862+
$valueTypes[$i] = new NonAcceptingNeverType();
2863+
$optionalKeys = $this->optionalKeys;
2864+
if (!in_array($i, $optionalKeys, true)) {
2865+
$optionalKeys[] = $i;
2866+
}
2867+
2868+
return $this->recreate($this->keyTypes, $valueTypes, $this->nextAutoIndexes, $optionalKeys, $unsetResult->isList(), $this->unsealed);
2869+
}
2870+
28392871
public function traverse(callable $cb): Type
28402872
{
28412873
$valueTypes = [];
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15428;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param array{a?: string, ...} $c
9+
*/
10+
function repro(array &$c): void
11+
{
12+
$x = $c['a'] ?? ''; // this causes $c to lose it's type
13+
assertType('array{a?: string, ...}', $c);
14+
$c['a'] = '1';
15+
}
16+
17+
/**
18+
* @param array{a?: string, ...} $c
19+
*/
20+
function issetBranches(array $c): void
21+
{
22+
if (isset($c['a'])) {
23+
assertType('array{a: string, ...}', $c);
24+
} else {
25+
assertType('array{a?: never, ...}', $c);
26+
}
27+
assertType('array{a?: string, ...}', $c);
28+
}
29+
30+
/**
31+
* @param array{a?: string, ...} $c
32+
*/
33+
function arrayKeyExistsBranches(array $c): void
34+
{
35+
if (array_key_exists('a', $c)) {
36+
assertType('array{a: string, ...}', $c);
37+
} else {
38+
assertType('array{a?: never, ...}', $c);
39+
}
40+
assertType('array{a?: string, ...}', $c);
41+
}
42+
43+
/**
44+
* @param array{a?: string, b: int, ...<string, int>} $c
45+
*/
46+
function typedExtras(array $c): void
47+
{
48+
if (!isset($c['a'])) {
49+
assertType('array{a?: never, b: int, ...<string, int>}', $c);
50+
}
51+
assertType('array{a?: string, b: int, ...<string, int>}', $c);
52+
}
53+
54+
/**
55+
* @param array{b: int, ...<string, int>} $c
56+
*/
57+
function implicitKey(array $c): void
58+
{
59+
if (!isset($c['a'])) {
60+
assertType('array{b: int, ...<string, int>}', $c);
61+
}
62+
assertType('array{b: int, ...<string, int>}', $c);
63+
}
64+
65+
/**
66+
* @param array{a?: string, ...<int, int>} $c
67+
*/
68+
function extrasCannotHoldKey(array $c): void
69+
{
70+
if (!isset($c['a'])) {
71+
assertType('array{...<int, int>}', $c);
72+
}
73+
assertType('array{a?: string, ...<int, int>}', $c);
74+
}

‎tests/PHPStan/Rules/Variables/ParameterOutAssignedTypeRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ public function testBug15066(): void
140140
]);
141141
}
142142

143+
public function testBug15428(): void
144+
{
145+
$this->analyse([__DIR__ . '/data/bug-15428.php'], []);
146+
}
147+
143148
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15428Variables;
4+
5+
/**
6+
* @param array{a?: string, ...} $c
7+
*/
8+
function repro(array &$c): void
9+
{
10+
$x = $c['a'] ?? ''; // this causes $c to lose it's type
11+
$c['a'] = '1';
12+
}

‎turbo-ext/src/ConstantArrayType.cpp‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,7 +4532,7 @@ class ConstantArrayType
45324532

45334533
if (valueIsSuperType == PT_TRI_NO) return zv::Val::null();
45344534

4535-
if (valueIsSuperType == PT_TRI_YES) return unsetPreservingListCertainty(offsetType.raw(), l);
4535+
if (valueIsSuperType == PT_TRI_YES) return removeOffset(offsetType.raw(), l);
45364536

45374537
zv::Val newValueType = combinator2(PT_LC("remove"), currentValueType, valueTypeToRemove.raw());
45384538
if (UNEXPECTED(newValueType.isUndef())) return zv::Val();
@@ -4550,7 +4550,7 @@ class ConstantArrayType
45504550
zv::Val offsetType = pt_has_offset_type_get_offset_type(Z_OBJ_P(typeToRemove));
45514551
zval *l = isListSlot();
45524552
if (UNEXPECTED(offsetType.isUndef() || l == NULL)) return zv::Val();
4553-
return unsetPreservingListCertainty(offsetType.raw(), l);
4553+
return removeOffset(offsetType.raw(), l);
45544554
}
45554555

45564556
return zv::Val::null();
@@ -6621,9 +6621,12 @@ class ConstantArrayType
66216621

66226622
/* $this->unsetOffset($offsetType, true), never when that turns a
66236623
* definite list into a definite non-list; UNDEF = pending exception */
6624-
zv::Val unsetPreservingListCertainty(zval *offsetType, zval *isListSlotValue) const
6624+
/* removeOffset(): the values of $this lacking the offset — an explicit
6625+
* key the unsealed extras can also hold stays as an optional never key
6626+
* instead of being dropped; UNDEF = pending exception */
6627+
zv::Val removeOffset(zval *offsetTypeArg, zval *isListSlotValue) const
66256628
{
6626-
zv::Val unsetResult = thisUnsetOffset(offsetType, true);
6629+
zv::Val unsetResult = thisUnsetOffset(offsetTypeArg, true);
66276630
if (UNEXPECTED(unsetResult.isUndef())) return zv::Val();
66286631
/* When the source was definitely a list but the post-unset shape
66296632
* definitely isn't (e.g. unsetting a non-optional leading key
@@ -6636,7 +6639,61 @@ class ConstantArrayType
66366639
if (UNEXPECTED(resultIsList < 0)) return zv::Val();
66376640
if (resultIsList == PT_TRI_NO) return neverType();
66386641
}
6639-
return unsetResult;
6642+
6643+
/* $this->unsealed === null || !$this->isUnsealed()->yes() */
6644+
zval *u = unsealedSlot();
6645+
if (UNEXPECTED(u == NULL)) return zv::Val();
6646+
if (Z_TYPE_P(u) == IS_NULL) return unsetResult;
6647+
zend_long unsealedness = thisIsUnsealed();
6648+
if (UNEXPECTED(unsealedness < 0)) return zv::Val();
6649+
if (unsealedness != PT_TRI_YES) return unsetResult;
6650+
6651+
zv::Val offsetType = callType(Z_OBJ_P(offsetTypeArg), PT_LC("toarraykey"), 0, NULL);
6652+
if (UNEXPECTED(offsetType.isUndef())) return zv::Val();
6653+
zv::Val offsetValues = pt_type_call_array(Z_OBJ_P(offsetType.raw()), PT_LC("getconstantscalarvalues"), 0, NULL);
6654+
if (UNEXPECTED(offsetValues.isUndef())) return zv::Val();
6655+
if (arrayCount(offsetValues.raw()) != 1) return unsetResult;
6656+
zval *unsealedKeyType, *unsealedValueType;
6657+
if (UNEXPECTED(!unsealedPair(u, unsealedKeyType, unsealedValueType))) return zv::Val();
6658+
zend_long extrasHoldOffset = isSuperTypeOfValue(unsealedKeyType, offsetType.raw());
6659+
if (UNEXPECTED(extrasHoldOffset < 0)) return zv::Val();
6660+
if (extrasHoldOffset == PT_TRI_NO) return unsetResult;
6661+
6662+
/* $offsetValues[0], an int or a string */
6663+
zval *offsetValue = zend_hash_index_find(Z_ARRVAL_P(offsetValues.raw()), 0);
6664+
if (offsetValue == NULL) return unsetResult;
6665+
ZVAL_DEREF(offsetValue);
6666+
if (Z_TYPE_P(offsetValue) != IS_LONG && Z_TYPE_P(offsetValue) != IS_STRING) return unsetResult;
6667+
6668+
/* $this->getKeyIndexMap()[$offsetValue] ?? null */
6669+
zv::Val map = getKeyIndexMap();
6670+
if (UNEXPECTED(map.isUndef())) return zv::Val();
6671+
zval *found = mapFind(Z_ARRVAL_P(map.raw()), offsetValue);
6672+
if (found == NULL || Z_TYPE_P(found) != IS_LONG) return unsetResult;
6673+
zend_long i = Z_LVAL_P(found);
6674+
6675+
zval *k = keyTypes();
6676+
zval *v = k != NULL ? valueTypes() : NULL;
6677+
zval *n = v != NULL ? nextAutoIndexes() : NULL;
6678+
zval *o = n != NULL ? optionalKeys() : NULL;
6679+
if (UNEXPECTED(o == NULL)) return zv::Val();
6680+
6681+
zval never;
6682+
if (UNEXPECTED(!pt_non_accepting_never_type_new(&never))) return zv::Val();
6683+
zv::Val neverValue = zv::Val::adopt(never);
6684+
zv::Arr valueTypesCopy = zv::Arr::adoptVal(zv::Val::copyOf(zv::Ref(v)));
6685+
valueTypesCopy.separate();
6686+
zv::ArrRef(valueTypesCopy.raw()).setIndex((zend_ulong) i, zv::Ref(neverValue.raw()));
6687+
6688+
zv::Arr optionalKeysCopy = zv::Arr::adoptVal(zv::Val::copyOf(zv::Ref(o)));
6689+
if (!inArrayStrictLong(o, i)) {
6690+
optionalKeysCopy.separate();
6691+
optionalKeysCopy.push(zv::Val::integer(i));
6692+
}
6693+
6694+
zend_long resultIsList = pt_type_op_trinary(Z_OBJ_P(unsetResult.raw()), PT_OP_IS_LIST, 0, NULL);
6695+
if (UNEXPECTED(resultIsList < 0)) return zv::Val();
6696+
return thisRecreate(k, valueTypesCopy.raw(), n, optionalKeysCopy.raw(), pt_trinary_singleton(resultIsList), u);
66406697
}
66416698

66426699
/* TypeCombinator::intersect($this, new NonEmptyArrayType()) */

0 commit comments

Comments
 (0)