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

Splat in constant array expression produces wrong array shape #6263

Closed
weirdan opened this issue Aug 8, 2021 · 5 comments · Fixed by #6290
Closed

Splat in constant array expression produces wrong array shape #6263

weirdan opened this issue Aug 8, 2021 · 5 comments · Fixed by #6290
Assignees
Labels

Comments

@weirdan
Copy link
Collaborator

weirdan commented Aug 8, 2021

https://psalm.dev/r/fb451afec9

Expected: flat list (https://3v4l.org/0IdDo)

@psalm-github-bot
Copy link

I found these snippets:

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

final class Token
{
    public const ONE = [
        16 => 16,
        21 => 21,
    ];

    public const TWO = [
        17 => 17,
        22 => 22,
    ];

    public const THREE = [
        18 => 18,
        23 => 23,
    ];

    public const RESULT = [...self::ONE, ...self::TWO, ...self::THREE];
}
$_t = Token::RESULT;
/** @psalm-trace $_t */;
Psalm output (using commit 3fb9e1c):

INFO: Trace - 23:24 - $_t: array{array{16: 16, 21: 21}, array{17: 17, 22: 22}, array{18: 18, 23: 23}}

@orklah
Copy link
Collaborator

orklah commented Aug 9, 2021

I took a quick glance at it. The issue is in here:

$const_type = SimpleTypeInferer::infer(

We loop over the class constants and we use SimpleTypeInferer::infer to calculate the type. Unfortunately, this method doesn't handle unpacking at all. We should probably take a look at how it's handled in ArrayAnalyzer::handleUnpackedArray and reuse code somehow

@weirdan weirdan changed the title Spat in constant array expression produces wrong array shape Splat in constant array expression produces wrong array shape Aug 10, 2021
@weirdan weirdan self-assigned this Aug 10, 2021
@weirdan
Copy link
Collaborator Author

weirdan commented Aug 11, 2021

@ondrejmirtes PHPStan also suffers from this bug: https://phpstan.org/r/9d00884a-5311-45c1-ad35-f2d7cdcbb852

@orklah
Copy link
Collaborator

orklah commented Aug 11, 2021

@weirdan FYI you can dump types in PHPStan, similar to @psalm-trace: https://phpstan.org/r/4b4198b8-b52a-4c72-b185-42b3b5c8dc94

weirdan added a commit to weirdan/psalm that referenced this issue Aug 11, 2021
This fixes int offset inference and adds handling for array unpacks in
constant arrays. Mostly it's copy/pasted from `ArrayAnalyzer` with
further adjustments.

Fixes vimeo#6263
Fixes vimeo#6289
@ondrejmirtes
Copy link

Please open a bug report in PHPStan's repo, thanks :)

weirdan added a commit to weirdan/psalm that referenced this issue Aug 12, 2021
This fixes int offset inference and adds handling for array unpacks in
constant arrays. Mostly it's copy/pasted from `ArrayAnalyzer` with
further adjustments.

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

Successfully merging a pull request may close this issue.

3 participants