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

iterator_to_array never returns mixed key, it throws TypeError instead #5396

Closed
vudaltsov opened this issue Mar 15, 2021 · 1 comment · Fixed by #5400
Closed

iterator_to_array never returns mixed key, it throws TypeError instead #5396

vudaltsov opened this issue Mar 15, 2021 · 1 comment · Fixed by #5400

Comments

@vudaltsov
Copy link
Contributor

https://psalm.dev/r/ce986a03b8

I think the issue raised by Psalm is not correct. While iterator_to_array does throw a TypeError in case of a non-scalar key (https://3v4l.org/BBOJF), the returned type of iterator_to_array(T, true) is never array<mixed, T>, it is array<array-key, T>.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ce986a03b8
<?php

/**
 * @template TKey
 * @template TValue
 * @param Traversable<TKey, TValue> $traversable
 * @return array<TValue>
 */
function toArray(Traversable $traversable): array
{
    return iterator_to_array($traversable);
}
Psalm output (using commit dd4d970):

INFO: MixedReturnTypeCoercion - 11:12 - The type 'array<TKey:fn-toarray as mixed, TValue:fn-toarray as mixed>' is more general than the declared return type 'array<array-key, TValue:fn-toarray as mixed>' for toArray

INFO: MixedReturnTypeCoercion - 7:12 - The declared return type 'array<array-key, TValue:fn-toarray as mixed>' for toArray is more specific than the inferred return type 'array<TKey:fn-toarray as mixed, TValue:fn-toarray as mixed>'

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

Successfully merging a pull request may close this issue.

1 participant