Skip to content

Commit

Permalink
Merge pull request #7076 from klimick/fix-generic-assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 7, 2021
2 parents 77e0f09 + dee3fc4 commit 2530b33
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,9 @@ public static function applyAssertionsToContext(

if ($replacement_atomic_type instanceof Type\Atomic\TArray
|| $replacement_atomic_type instanceof Type\Atomic\TKeyedArray
|| $replacement_atomic_type instanceof Type\Atomic\TList
) {
$ored_type_assertions[] = $prefix . 'array';
$ored_type_assertions[] = $prefix . $replacement_atomic_type->getId();
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNamedObject) {
$ored_type_assertions[] = $prefix . $replacement_atomic_type->value;
} elseif ($replacement_atomic_type instanceof Type\Atomic\Scalar) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function (array $rules) use ($inferred_lower_bounds, $codebase): array {
if ($first_type instanceof TTemplateParam) {
$rule_token[0] = $first_type->param_name;
} else {
$rule_token[0] = $first_type->getKey();
$rule_token[0] = $first_type->getId();
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions tests/Template/FunctionTemplateAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,98 @@ function validateUsername(string $username): void {
}
}'
],
'ifTrueListAssertionFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert-if-true T $toCheck
*/
function is($toCheck): bool
{
throw new RuntimeException("???");
}
}
/**
* @param list<int> $_list
*/
function acceptsIntList(array $_list): void {}
/** @var Type<list<int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
if ($numbersT->is($mixed)) {
acceptsIntList($mixed);
}'
],
'assertListFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert T $toCheck
*/
function assert($toCheck): void
{
}
}
/**
* @param list<int> $_list
*/
function acceptsIntList(array $_list): void {}
/** @var Type<list<int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
$numbersT->assert($mixed);
acceptsIntList($mixed);'
],
'assertArrayFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert T $toCheck
*/
function assert($toCheck): void
{
}
}
/**
* @param array<string, int> $_list
*/
function acceptsArray(array $_list): void {}
/** @var Type<array<string, int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
$numbersT->assert($mixed);
acceptsArray($mixed);'
],
];
}

Expand Down

0 comments on commit 2530b33

Please sign in to comment.