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

Using the splat operator on a variable given to a function with a variadic parameter have incorrect side effects #2982

Closed
LeSuisse opened this issue Mar 17, 2020 · 4 comments
Labels

Comments

@LeSuisse
Copy link
Contributor

I would not have expected to get an error on https://psalm.dev/r/2196ad3688

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2196ad3688
<?php

function takeVariadicInts(int ...$inputs): void
{
    print_r($inputs);
}

/**
 * @param non-empty-list<int> $inputs
 */
function takeNonEmptyList(array $inputs): void
{
    print_r($inputs);
}

$a = [3, 5, 7];
takeVariadicInts(...$a);
takeNonEmptyList($a);
Psalm output (using commit 951b943):

ERROR: ArgumentTypeCoercion - 18:18 - Argument 1 of takeNonEmptyList expects non-empty-list<int>, parent type array<int, int(3)> provided

@muglug muglug added the bug label Mar 17, 2020
@muglug muglug closed this as completed in da170ef Mar 17, 2020
@LeSuisse
Copy link
Contributor Author

Thanks for the quick fix @muglug! I'm not sure it completely fixes the issue, see https://psalm.dev/r/da8bc6d1d5

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @return non-empty-list<int>
 */
function provide(): array
{
    return [3, 5, 7];
}

function takeVariadicInts(int ...$inputs): void
{
    print_r($inputs);
}

/**
 * @param non-empty-list<int> $inputs
 */
function takeNonEmptyList(array $inputs): void
{
    print_r($inputs);
}

$a = provide();
takeVariadicInts(...$a);
takeNonEmptyList($a);
Psalm output (using commit da170ef):

ERROR: ArgumentTypeCoercion - 26:18 - Argument 1 of takeNonEmptyList expects non-empty-list<int>, parent type array<int, int> provided

@muglug
Copy link
Collaborator

muglug commented Mar 17, 2020

made better in 9cd8170

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

2 participants