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

Invalid provided type consolidation causes InvalidArgument error #8231

Open
kkmuffme opened this issue Jul 8, 2022 · 7 comments
Open

Invalid provided type consolidation causes InvalidArgument error #8231

kkmuffme opened this issue Jul 8, 2022 · 7 comments

Comments

@kkmuffme
Copy link
Contributor

kkmuffme commented Jul 8, 2022

https://psalm.dev/r/ef73d1b6b4

I think there is a bug with the type consolidation, as the provided type in error is invalid
array{non-empty-list<string>, non-empty-list<string>}<non-empty-list<string>>

When I put the correct type, then no error https://psalm.dev/r/3549b6fa5a
However I think it needs to have the "provided" type fixed to a valid type, so the error message makes sense

@psalm-github-bot
Copy link

psalm-github-bot bot commented Jul 8, 2022

I found these snippets:

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

$csv = array( 
    'foo,bar',
    'hello,world',
);
process( array_map( 'str_getcsv', $csv ) );


/**
 * @param array<int, string> $data
 * @return void
 */
function process( $data ) {
    print_r( $data );
}
Psalm output (using commit 6d45003):

ERROR: InvalidArgument - 7:10 - Argument 1 of process expects array<int, string>, array{non-empty-list<string>, non-empty-list<string>}<non-empty-list<string>> provided
https://psalm.dev/r/3549b6fa5a
<?php

$csv = array( 
    'foo,bar',
    'hello,world',
);
process( array_map( 'str_getcsv', $csv ) );


/**
 * @param array<int, string[]> $data
 * @return void
 */
function process( $data ) {
    print_r( $data );
}
Psalm output (using commit 6d45003):

No issues!

@AndrolGenhald
Copy link
Collaborator

array{non-empty-list<string>, non-empty-list<string>}<non-empty-list<string>> isn't a valid type annotation that a user can use, but it's how arrays with some known keys are represented internally. It's not great that we show a type that can't actually be used, but I'm waiting till I get around to doing index signature like arrays to fix this.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/165b0a9c5c
<?php

/** @var array<string, float> */
$arr = [];
$arr[1] = 2;
$arr[4] = 3;
/** @psalm-trace $arr */;
Psalm output (using commit 6d45003):

INFO: Trace - 7:25 - $arr: array{1: 2, 4: 3}<string, float>

INFO: UnusedVariable - 4:1 - $arr is never referenced or the value is not used

@orklah
Copy link
Collaborator

orklah commented Jul 8, 2022

Note that you can use this syntax like this:
https://psalm.dev/r/ff73b2dfb0

But I hate it. It's not an intersection at all. I'd rather we parse the original syntax

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ff73b2dfb0
<?php
/** @var array{1: 2, 4: 3}&array<string, float> $array */
$array = '';

/** @psalm-trace $array */;
Psalm output (using commit 6d45003):

INFO: Trace - 5:27 - $array: array{1: 2, 4: 3}<string, float>

INFO: UnusedVariable - 3:1 - $array is never referenced or the value is not used

@kkmuffme
Copy link
Contributor Author

kkmuffme commented Jul 9, 2022

So basically you are saying this issue could be fixed by just replacing }< with }&< in the error message? That would be a quick fix I could PR, which would solve this issue for the time being until those other things are implemented.

@orklah
Copy link
Collaborator

orklah commented Jul 9, 2022

I wouldn't accept such a PR. That syntax need to be replaced, not normalized

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

No branches or pull requests

3 participants