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

Casting down iterable in else branch sometimes doesn't preserve type params #2005

Closed
bugreportuser opened this issue Aug 12, 2019 · 3 comments
Labels

Comments

@bugreportuser
Copy link
Contributor

#1997 fixed most issues. However, Psalm still has trouble inferring the types for this function.

Removing T1 fixes the issue.

Example (https://psalm.dev/r/aa78059e8a):

<?php
/**
 * @template T1 as array-key
 * @template T2
 *
 * @param iterable<T1,T2> $x
 *
 * @return array<T1,T2>
 */
function iterableToArray (iterable $x): array {
    if (is_array($x)) {
        return $x;
    }
    else {
        return iterator_to_array($x);
    }
}

/** @var iterable<int,int> */
$x = null;
iterableToArray($x);

Example output:

ERROR: InvalidReturnStatement - 15:16 - The type 'array<array-key, mixed>' does not match the declared return type 'array<T1 as array-key, T2 as mixed>' for iterableToArray

ERROR: InvalidReturnType - 8:12 - The declared return type 'array<T1, T2>' for iterableToArray is incorrect, got 'array<T1|array-key, T2|mixed>'
@muglug muglug added the bug label Aug 12, 2019
@muglug muglug changed the title Casting down iterable in else branch sometimes doesn't work Casting down iterable in else branch sometimes doesn't preserve type params Aug 12, 2019
@muglug muglug closed this as completed in 6245176 Aug 12, 2019
@bugreportuser
Copy link
Contributor Author

Thanks!

2e3s pushed a commit to 2e3s/psalm that referenced this issue Sep 29, 2019
@paxal
Copy link
Contributor

paxal commented Nov 8, 2019

Hi ! I'm still having this kind of issue despite being on v3.6.4 : result is marked as array, and calling sleep(iterableToArray($x)) raises a MixedArgument issue :
https://psalm.dev/r/cc8c7ed010

@muglug
Copy link
Collaborator

muglug commented Nov 8, 2019

@paxal mind ticketing that separately?

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