Skip to content
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

Function with conditional types gives errors since 4.5.0 #5198

Closed
vudaltsov opened this issue Feb 11, 2021 · 2 comments
Closed

Function with conditional types gives errors since 4.5.0 #5198

vudaltsov opened this issue Feb 11, 2021 · 2 comments
Labels

Comments

@vudaltsov
Copy link
Contributor

https://psalm.dev/r/9449b708b5

This function didn't throw any errors before 4.5.0

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9449b708b5
<?php

/**
 * @psalm-pure
 * @template TValue
 * @template TIterable of ?iterable<TValue>
 * @param TIterable $iterable
 * @return (TIterable is null ? null : list<TValue>)
 */
function toList(?iterable $iterable): ?array
{
    if (null === $iterable) {
        return null;
    }

    if (is_array($iterable)) {
        return array_values($iterable);
    }

    return iterator_to_array($iterable, false);
}
Psalm output (using commit 257a1ca):

ERROR: DocblockTypeContradiction - 16:9 - Docblock-defined type TIterable:fn-tolist as iterable<mixed, TValue:fn-tolist as mixed> for $iterable is never array

INFO: MixedArgument - 17:29 - Argument 1 of array_values cannot be mixed, expecting array<array-key, mixed>

INFO: MixedReturnTypeCoercion - 17:16 - The type 'array<array-key, mixed>' is more general than the declared return type '(list<TValue:fn-tolist as mixed>)|null' for toList

INFO: MixedReturnTypeCoercion - 20:12 - The type 'array<array-key, mixed>' is more general than the declared return type '(list<TValue:fn-tolist as mixed>)|null' for toList

INFO: MixedReturnTypeCoercion - 8:12 - The declared return type '(list<TValue:fn-tolist as mixed>)|null' for toList is more specific than the inferred return type 'array<array-key, mixed>|null'

@vudaltsov vudaltsov changed the title Function with conditional types stopped working Function with conditional types gives errors since 4.5.0 Feb 11, 2021
@weirdan weirdan added the bug label Feb 11, 2021
@muglug muglug closed this as completed in fa33737 Feb 12, 2021
@muglug
Copy link
Collaborator

muglug commented Feb 12, 2021

A bugfix in 4.5 revealed the fixed issue. The other issues with mixed are also longstanding, ticketed here: #5208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants