1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix iterable coercion for Traversable-extending types

Fixes #3159
This commit is contained in:
Brown 2020-04-19 09:15:09 -04:00
parent 5c511ad95d
commit 3e07684855
2 changed files with 32 additions and 0 deletions

View File

@ -684,6 +684,23 @@ class TypeAnalyzer
continue 2;
}
if ($intersection_container_type_lower === 'iterable') {
if ($intersection_input_type_lower === 'traversable'
|| ($codebase->classlikes->classExists($intersection_input_type_lower)
&& $codebase->classlikes->classImplements(
$intersection_input_type_lower,
'Traversable'
))
|| ($codebase->classlikes->interfaceExists($intersection_input_type_lower)
&& $codebase->classlikes->interfaceExtends(
$intersection_input_type_lower,
'Traversable'
))
) {
continue 2;
}
}
if ($intersection_input_type_lower === 'traversable'
&& $intersection_container_type_lower === 'iterable'
) {

View File

@ -3171,6 +3171,21 @@ class ClassTemplateTest extends TestCase
}',
'error_message' => 'InvalidArgument'
],
'preventIteratorAggregateToIterableWithDifferentTypes' => [
'<?php
class Foo {}
class Bar {}
/** @param iterable<int, Foo> $foos */
function consume(iterable $foos): void {}
/** @param IteratorAggregate<int, Bar> $t */
function foo(IteratorAggregate $t) : void {
consume($t);
}',
'error_message' => 'InvalidArgument',
],
];
}
}