Skip to content
Permalink
Browse files

Coerce generic types from empty

  • Loading branch information...
muglug committed May 21, 2019
1 parent ec2ee04 commit 034ea725e8f59658328a1483e4a2c45e61a85af8
@@ -2640,10 +2640,35 @@ private static function coerceValueAfterGatekeeperArgument(
Context $context,
bool $unpack
) : void {
if ($param_type->hasMixed() || ($param_type->from_docblock && !$input_type->isMixed())) {
if ($param_type->hasMixed()) {
return;
}
if ($param_type->from_docblock && !$input_type->isMixed()) {
$input_type_changed = false;
foreach ($param_type->getTypes() as $param_atomic_type) {
if ($param_atomic_type instanceof Type\Atomic\TGenericObject) {
foreach ($input_type->getTypes() as $input_atomic_type) {
if ($input_atomic_type instanceof Type\Atomic\TGenericObject
&& $input_atomic_type->value === $param_atomic_type->value
) {
foreach ($input_atomic_type->type_params as $i => $type_param) {
if ($type_param->isEmpty() && isset($param_atomic_type->type_params[$i])) {
$input_type_changed = true;
$input_atomic_type->type_params[$i] = clone $param_atomic_type->type_params[$i];
}
}
}
}
}
}
if (!$input_type_changed) {
return;
}
}
$var_id = ExpressionAnalyzer::getVarId(
$input_expr,
$statements_analyzer->getFQCLN(),
@@ -2251,7 +2251,8 @@ class Item {}
/** @psalm-param Collection<Item> $c */
function takesCollectionOfItems(Collection $c): void {}
takesCollectionOfItems(new Collection());',
takesCollectionOfItems(new Collection());
takesCollectionOfItems(new Collection([]));',
],
];
}
@@ -2824,6 +2825,35 @@ public function set($value): void {}
}',
'error_message' => 'InvalidTemplateParam',
],
'templateEmptyParamCoercionChangeVariable' => [
'<?php
namespace NS;
use Countable;
/** @template T */
class Collection
{
/** @psalm-var iterable<T> */
private $data;
/** @psalm-param iterable<T> $data */
public function __construct(iterable $data = []) {
$this->data = $data;
}
}
/** @psalm-param Collection<string> $c */
function takesStringCollection(Collection $c): void {}
/** @psalm-param Collection<int> $c */
function takesIntCollection(Collection $c): void {}
$collection = new Collection();
takesStringCollection($collection);
takesIntCollection($collection);',
'error_message' => 'InvalidScalarArgument',
],
];
}
}

0 comments on commit 034ea72

Please sign in to comment.
You can’t perform that action at this time.