Skip to content

Commit

Permalink
Merge pull request #9022 from Ocramius/fix/#8983-allow-large-union-ty…
Browse files Browse the repository at this point in the history
…pes-in-array-type-inference

Better type inference and type checking for large union types used in array keys/values
  • Loading branch information
orklah committed Dec 29, 2022
2 parents 790c309 + e6600fe commit 5bfb87c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ public static function analyze(
$item_key_type = TypeCombiner::combine(
$array_creation_info->item_key_atomic_types,
$codebase,
false,
true,
30,
);
} else {
$item_key_type = null;
Expand All @@ -109,9 +106,6 @@ public static function analyze(
$item_value_type = TypeCombiner::combine(
$array_creation_info->item_value_atomic_types,
$codebase,
false,
true,
30,
);
} else {
$item_value_type = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,21 +548,13 @@ private static function inferArrayType(
if ($array_creation_info->item_key_atomic_types) {
$item_key_type = TypeCombiner::combine(
$array_creation_info->item_key_atomic_types,
null,
false,
true,
30,
);
}

$item_value_type = null;
if ($array_creation_info->item_value_atomic_types) {
$item_value_type = TypeCombiner::combine(
$array_creation_info->item_value_atomic_types,
null,
false,
true,
30,
);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ArrayFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,19 @@ function findit(Closure $x): void
}
',
],
'functionRequiringArrayWithLargeUnionTypeKeyAllowsInputArrayUsingSameUnionForItsKeys' => [
'code' => '<?php
/** @psalm-type TLargeUnion = 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 */
/** @return TLargeUnion */
function makeKey(): int { throw new Exception("irrelevant"); }
/** @param array<TLargeUnion, mixed> $_input */
function consumeArray(array $_input): void {}
consumeArray([makeKey() => null]);
',
],
];
}

Expand Down

0 comments on commit 5bfb87c

Please sign in to comment.