-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User defined assertions works incorrectly for generic iterable types #5294
Comments
I found these snippets: https://psalm.dev/r/12ed2787e9<?php
/**
* @template A
*/
final class Type {}
/**
* @template T
*
* @param Type<T> $t
* @psalm-assert-if-true T $toCheck
*/
function isTypeOf(Type $t, mixed $toCheck): bool
{
throw new RuntimeException('???');
}
/** @var Type<int> $numberT */
$numberT = null;
/** @var mixed $mixed */
$mixed = null;
if (isTypeOf($numberT, $mixed)) {
/** @psalm-trace $number */
$number = $mixed; // is int. awesome.
}
https://psalm.dev/r/a4ab076a44<?php
/**
* @template A
*/
final class Type {}
/**
* @template T
*
* @param Type<T> $t
* @psalm-assert T $toCheck
*/
function assertTypeOf(Type $t, mixed $toCheck): void
{
throw new RuntimeException('???');
}
/** @var Type<int> $numberT */
$numberT = null;
/** @var mixed $mixed */
$mixed = null;
assertTypeOf($numberT, $mixed);
/** @psalm-trace $number */
$number = $mixed; // is int. awesome.
https://psalm.dev/r/58dbc19d8a<?php
/**
* @template A
*/
final class Type {}
/**
* @template T
*
* @param Type<T> $t
* @psalm-assert-if-true T $toCheck
*/
function isTypeOf(Type $t, mixed $toCheck): bool
{
throw new RuntimeException('???');
}
/** @var Type<list<int>> $numbersT */
$numbersT = null;
/** @var mixed $mixed */
$mixed = null;
if (isTypeOf($numbersT, $mixed)) {
/** @psalm-trace $numbers */
$numbers = $mixed; // is array<array-key, mixed>. but should be list<int>.
}
https://psalm.dev/r/4c25ed702c<?php
/**
* @template A
*/
final class Type {}
/**
* @template T
*
* @param Type<T> $t
* @psalm-assert T $toCheck
*/
function assertTypeOf(Type $t, mixed $toCheck): void
{
throw new RuntimeException('???');
}
/** @var Type<list<int>> $numbersT */
$numbersT = null;
/** @var mixed $mixed */
$mixed = null;
assertTypeOf($numbersT, $mixed);
/** @psalm-trace $numbers */
$numbers = $mixed; // is mixed. Not even array<array-key, int>.
|
Hmmmmm. This example https://psalm.dev/r/205e1d64ea contains generic assertion too. But it is working. It seems issue only with |
I found these snippets: https://psalm.dev/r/205e1d64ea<?php
/**
* @template A
*/
final class Type {}
/**
* @template T
*
* @param Type<T> $t
* @psalm-assert-if-true T $toCheck
*/
function isTypeOf(Type $t, mixed $toCheck): bool
{
throw new RuntimeException('???');
}
/**
* @template T
*/
final class Foo {}
/** @var Type<Foo<int>> $fooT */
$fooT = null;
/** @var mixed $mixed */
$mixed = null;
if (isTypeOf($fooT, $mixed)) {
/** @psalm-trace $number */
$number = $mixed; // is int. awesome.
}
|
Then it's possibly related to #5310 |
Seems a problem at this place: psalm/src/Psalm/Storage/Assertion.php Line 63 in 110c9ef
Can we change it to |
@klimick please try it in a PR, we'll see how CI reacts :) |
Lets take a look at this https://psalm.dev/r/12ed2787e9
or this https://psalm.dev/r/a4ab076a44
example.
Works fine. As expected.
But if go deeper and try to assert generic type like
list<int>
then psalm lose types:psalm-assert-if-true: https://psalm.dev/r/58dbc19d8a
psalm-assert: https://psalm.dev/r/4c25ed702c
The text was updated successfully, but these errors were encountered: