Psalm: possible issue with shaped array not being interchangeable with non-empty-array

Created on 13 Jan 2020  路  9Comments  路  Source: vimeo/psalm

If a type is restricted to non-empty-array, a shaped array should be allowed: https://psalm.dev/r/c8fe243f5f

Appending to the list child of a shaped array with a value-restricted param appears to alter the parent type from the initial shaped array to a non-empty-array: https://psalm.dev/r/f24c852ae9

bug

All 9 comments

I found these snippets:


https://psalm.dev/r/c8fe243f5f

<?php
/** @param non-empty-array $_bar */
function foo(array $_bar) : void { }

/** @var array{0:list<string>, 1:list<int>} */
$bar = [[], []];

foo($bar);
Psalm output (using commit 7a65eb1):

ERROR: ArgumentTypeCoercion - 8:5 - Argument 1 of foo expects non-empty-array<mixed, mixed>, parent type array{0: list<string>, 1: list<int>} provided


https://psalm.dev/r/f24c852ae9

<?php

class Bar {
  /** @var array{0: list<string>, 1:list<string>} */
  private $baz = [[], []];

  /**
  * @param 0|1 $index
  */
  public function append(string $str, int $index) : void {    
    $this->baz[$index][] = $str;
  }
}
Psalm output (using commit 7a65eb1):

ERROR: PropertyTypeCoercion - 11:4 - $this->baz expects 'array{0: list<string>, 1: list<string>}',  parent type 'non-empty-array<int, list<string>>' provided

@muglug https://psalm.dev/r/f24c852ae9 is still bugged

I found these snippets:


https://psalm.dev/r/f24c852ae9

<?php

class Bar {
  /** @var array{0: list<string>, 1:list<string>} */
  private $baz = [[], []];

  /**
  * @param 0|1 $index
  */
  public function append(string $str, int $index) : void {    
    $this->baz[$index][] = $str;
  }
}
Psalm output (using commit 887a635):

ERROR: PropertyTypeCoercion - 11:4 - $this->baz expects 'array{0: list<string>, 1: list<string>}',  parent type 'non-empty-array<int, list<string>>' provided

That's a different bug, more succinctly here: https://psalm.dev/r/31d5cccec3

I found these snippets:


https://psalm.dev/r/31d5cccec3

<?php

class Bar {
  /** @var array{0: string, 1:string} */
  private $baz = ["a", "b"];

  /**
  * @param 0|1 $index
  */
  public function append(string $str, int $index) : void {    
    $this->baz[$index] = $str;
  }
}
Psalm output (using commit 887a635):

ERROR: PropertyTypeCoercion - 11:4 - $this->baz expects 'array{0: string, 1: string}',  parent type 'non-empty-array<int(0)|int(1), string>' provided

that different bug was fixed int c77f2cd

waits for psalm.dev to update

@muglug https://psalm.dev/r/31d5cccec3 still appears to be bugged in c77f2cd ?

I found these snippets:


https://psalm.dev/r/31d5cccec3

<?php

class Bar {
  /** @var array{0: string, 1:string} */
  private $baz = ["a", "b"];

  /**
  * @param 0|1 $index
  */
  public function append(string $str, int $index) : void {    
    $this->baz[$index] = $str;
  }
}
Psalm output (using commit c77f2cd):

ERROR: PropertyTypeCoercion - 11:4 - $this->baz expects 'array{0: string, 1: string}',  parent type 'non-empty-array<int(0)|int(1), string>' provided

Was this page helpful?
0 / 5 - 0 ratings

Related issues

greg0ire picture greg0ire  路  3Comments

Pierstoval picture Pierstoval  路  3Comments

albe picture albe  路  3Comments

orklah picture orklah  路  3Comments

vudaltsov picture vudaltsov  路  3Comments