@@ -5316,77 +5316,77 @@ class ConstantArrayType
53165316 * that has the key k has every key below k too - positional
53175317 * operations on the list trust both, so the keys are put in
53185318 * ascending order and the ones below a required key are made
5319- * required. In a sealed shape a key past a gap in the 0..n sequence
5320- * (or any non-integer key) can never appear in a list, so keep only
5321- * the contiguous 0..m prefix. Unsealed extras may fill the gaps, so
5322- * keep every key there, the integer ones first. */
5319+ * required. A list never has a negative or a non-integer key, so
5320+ * those are dropped. In a sealed shape a key past a gap in the 0..n
5321+ * sequence can never appear in a list either, so only the contiguous
5322+ * 0..m prefix is kept; unsealed extras may fill the gaps, so every
5323+ * other key is kept there. */
53235324 uint32_t keyCount = zend_hash_num_elements (Z_ARRVAL_P (k));
53245325 zv::ScratchTable positionByIndex (keyCount);
5325- zv::Arr otherPositions = zv::Arr::create (0 );
5326+ zv::Arr droppedPositions = zv::Arr::create (0 );
53265327 for (zv::ArrayEntry entry : zv::ArrRef (k)) {
53275328 zval *keyType = entry.value ().deref ().raw ();
53285329 zval position;
53295330 ZVAL_LONG (&position, (zend_long) entry.indexKey ());
5330- if (Z_TYPE_P (keyType) != IS_OBJECT || !instanceof_function (Z_OBJCE_P (keyType), pt_ce_constant_integer_type)) {
5331- otherPositions.push (zv::Ref (&position));
5331+ zend_long value = 0 ;
5332+ if (Z_TYPE_P (keyType) == IS_OBJECT && instanceof_function (Z_OBJCE_P (keyType), pt_ce_constant_integer_type)) {
5333+ if (UNEXPECTED (!pt_constant_integer_get_value (Z_OBJ_P (keyType), value))) return zv::Val ();
5334+ } else {
5335+ value = -1 ;
5336+ }
5337+ if (value < 0 ) {
5338+ droppedPositions.push (zv::Ref (&position));
53325339 continue ;
53335340 }
5334- zend_long value;
5335- if (UNEXPECTED (!pt_constant_integer_get_value (Z_OBJ_P (keyType), value))) return zv::Val ();
53365341 zend_hash_index_update (positionByIndex.table (), (zend_ulong) value, &position);
53375342 }
53385343
5344+ /* ksort($positionByIndex) - the kept keys are all non-negative */
5345+ zend_hash_sort_ex (positionByIndex.table (), zend_sort, compareSignedIndexBuckets, 0 );
53395346 zend_long unsealedness = thisIsUnsealed ();
53405347 if (UNEXPECTED (unsealedness < 0 )) return zv::Val ();
53415348 zv::Arr keptPositions = zv::Arr::create (keyCount);
53425349 if (unsealedness == PT_TRI_NO ) {
5343- for (zend_long index = 0 ;; index++) {
5344- zval *position = zend_hash_index_find (positionByIndex.table (), (zend_ulong) index);
5345- if (position == NULL ) break ;
5346- keptPositions.push (zv::Ref (position));
5350+ zend_long index = 0 ;
5351+ for (zv::ArrayEntry entry : zv::TableRef (positionByIndex.table ())) {
5352+ /* the contiguous 0..m prefix is kept, every key after the
5353+ * first gap dropped */
5354+ if ((zend_long) entry.indexKey () == index) {
5355+ keptPositions.push (entry.value ());
5356+ index++;
5357+ continue ;
5358+ }
5359+ index = -1 ;
5360+ droppedPositions.push (entry.value ());
53475361 }
53485362 } else {
5349- /* ksort($positionByIndex); array_merge(array_values(...), $otherPositions) */
5350- zend_hash_sort_ex (positionByIndex.table (), zend_sort, compareSignedIndexBuckets, 0 );
53515363 for (zv::ArrayEntry entry : zv::TableRef (positionByIndex.table ())) {
53525364 keptPositions.push (entry.value ());
53535365 }
5354- for (zv::ArrayEntry entry : otherPositions.arrRef ()) {
5355- keptPositions.push (entry.value ());
5356- }
5366+ }
5367+
5368+ for (zv::ArrayEntry entry : droppedPositions.arrRef ()) {
5369+ bool optional;
5370+ if (UNEXPECTED (!thisIsOptionalKey (entry.value ().asLong (), optional))) return zv::Val ();
5371+ if (!optional) return neverType ();
53575372 }
53585373
53595374 zend_long requiredLength = 0 ;
53605375 for (zv::ArrayEntry entry : keptPositions.arrRef ()) {
5361- zend_long position = entry.value ().asLong ();
5362- zval *keyType = arrayIndexObject (k, position, " keyTypes" );
5363- if (UNEXPECTED (keyType == NULL )) return zv::Val ();
5364- if (!instanceof_function (Z_OBJCE_P (keyType), pt_ce_constant_integer_type)) continue ;
53655376 bool optional;
5366- if (UNEXPECTED (!thisIsOptionalKey (position , optional))) return zv::Val ();
5377+ if (UNEXPECTED (!thisIsOptionalKey (entry. value (). asLong () , optional))) return zv::Val ();
53675378 if (optional) continue ;
53685379
5369- zend_long value;
5370- if (UNEXPECTED (!pt_constant_integer_get_value (Z_OBJ_P (keyType), value))) return zv::Val ();
5371- /* max($requiredLength, $value + 1); at PHP_INT_MAX the twin's
5372- * float compares like the saturated int below */
5373- if (value >= requiredLength) requiredLength = value == ZEND_LONG_MAX ? ZEND_LONG_MAX : value + 1 ;
5380+ requiredLength = (zend_long) entry.indexKey () + 1 ;
53745381 }
53755382
5383+ /* $optionalPositions: $indexByPosition[$position] is the kept
5384+ * position itself, $keptPositions being a list */
53765385 zv::Arr newOptionalKeys = zv::Arr::create (0 );
53775386 for (zv::ArrayEntry entry : keptPositions.arrRef ()) {
5378- zend_long position = entry.value ().asLong ();
53795387 bool optional;
5380- if (UNEXPECTED (!thisIsOptionalKey (position, optional))) return zv::Val ();
5381- if (!optional) continue ;
5382-
5383- zval *keyType = arrayIndexObject (k, position, " keyTypes" );
5384- if (UNEXPECTED (keyType == NULL )) return zv::Val ();
5385- if (instanceof_function (Z_OBJCE_P (keyType), pt_ce_constant_integer_type)) {
5386- zend_long value;
5387- if (UNEXPECTED (!pt_constant_integer_get_value (Z_OBJ_P (keyType), value))) return zv::Val ();
5388- if (value >= 0 && value < requiredLength) continue ;
5389- }
5388+ if (UNEXPECTED (!thisIsOptionalKey (entry.value ().asLong (), optional))) return zv::Val ();
5389+ if (!optional || (zend_long) entry.indexKey () < requiredLength) continue ;
53905390
53915391 zval newPosition;
53925392 ZVAL_LONG (&newPosition, (zend_long) entry.indexKey ());
0 commit comments