Skip to content

Commit

Permalink
Fix key comparison when unpacking Traversables.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrolGenhald committed Jul 26, 2022
1 parent 2b389df commit ee68184
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Expand Up @@ -588,7 +588,7 @@ private static function handleUnpackedArray(

$array_creation_info->can_create_objectlike = false;

if (!UnionTypeComparator::canBeContainedBy(
if (!UnionTypeComparator::isContainedBy(
$codebase,
$iterable_type->type_params[0],
Type::getArrayKey(),
Expand Down
38 changes: 33 additions & 5 deletions tests/ArrayAssignmentTest.php
Expand Up @@ -1674,17 +1674,30 @@ function foobar(Foo $foo): array
'ignored_issues' => [],
'php_version' => '8.1',
],
'unpackIncorrectlyExtendedTraversable' => [
'unpackIncorrectlyExtendedInterface' => [
'code' => '<?php
/** @extends Traversable<int> */
/**
* @template TKey
* @template TValue of scalar
* @extends Traversable<TKey, TValue>
*/
interface Foo extends Traversable {}
/**
* @return array<int, mixed>
* @psalm-suppress MissingTemplateParam
* @template TKey
* @extends Foo<TKey>
*/
function foobar(Foo $foo): array
interface Bar extends Foo {}
/**
* @param Bar<int> $bar
* @return list<scalar>
*/
function foobar(Bar $bar): array
{
return [...$foo];
$unpacked = [...$bar];
return $unpacked;
}
',
],
Expand Down Expand Up @@ -2407,6 +2420,21 @@ class Foo {}
',
'error_message' => 'InvalidOperand',
],
'unpackTraversableWithKeyOmitted' => [
'code' => '<?php
/** @extends Traversable<int> */
interface Foo extends Traversable {}
/**
* @return array<int, mixed>
*/
function foobar(Foo $foo): array
{
return [...$foo];
}
',
'error_message' => 'InvalidOperand',
],
];
}
}

0 comments on commit ee68184

Please sign in to comment.