psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Weird !falsy assertion paradox when using template conditionals

Open danog opened this issue 2 years ago • 12 comments

https://psalm.dev/r/46b3483706, but the non-templated version works fine: https://psalm.dev/r/46b3483706 Changing the $onlyType assertion to a non-null assertion also fixes it: https://psalm.dev/r/55b41e85e3

danog avatar Aug 16 '22 09:08 danog

I found these snippets:

https://psalm.dev/r/46b3483706
<?php
	/**
     * @param array<array{type: string}> $v
	 * @param "prof"|"organization"|null $onlyType
     * @return array<array{type: string}> 
	 */
	function getOwnerObjects(array $v, ?string $onlyType = null): array {
        $ok = [];
		foreach ($v as $owner) {
			if ($onlyType && $onlyType !== $owner['type']) {
				continue;
			}
            $ok []= $owner;
		}
        return $ok;
	}
Psalm output (using commit 42462b8):

No issues!
https://psalm.dev/r/55b41e85e3
<?php
	/**
     * @param array<array{type: string}> $v
	 * @param "prof"|"organization"|null $onlyType
     * @return ($onlyType is null ? array<array{type: string}> : array<array{type: $onlyType}>)
	 */
	function getOwnerObjects(array $v, ?string $onlyType = null): array {
        $ok = [];
		foreach ($v as $owner) {
			if ($onlyType !== null && $onlyType !== $owner['type']) {
				continue;
			}
            $ok []= $owner;
		}
        return $ok;
	}
Psalm output (using commit 42462b8):

No issues!

psalm-github-bot[bot] avatar Aug 16 '22 09:08 psalm-github-bot[bot]

https://psalm.dev/r/46b3483706, but the non-templated version works fine: https://psalm.dev/r/46b3483706

Those are the same link.

AndrolGenhald avatar Aug 16 '22 17:08 AndrolGenhald

I found these snippets:

https://psalm.dev/r/46b3483706
<?php
	/**
     * @param array<array{type: string}> $v
	 * @param "prof"|"organization"|null $onlyType
     * @return array<array{type: string}> 
	 */
	function getOwnerObjects(array $v, ?string $onlyType = null): array {
        $ok = [];
		foreach ($v as $owner) {
			if ($onlyType && $onlyType !== $owner['type']) {
				continue;
			}
            $ok []= $owner;
		}
        return $ok;
	}
Psalm output (using commit 42462b8):

No issues!

psalm-github-bot[bot] avatar Aug 16 '22 17:08 psalm-github-bot[bot]

Whoops, I meant https://psalm.dev/r/82163ab6b7 :)

danog avatar Aug 16 '22 19:08 danog

I found these snippets:

https://psalm.dev/r/82163ab6b7
<?php
	/**
     * @param array<array{type: string}> $v
	 * @param "prof"|"organization"|null $onlyType
     * @return array<array{type: string}> 
	 */
	function getOwnerObjects(array $v, ?string $onlyType = null): array {
        $ok = [];
		foreach ($v as $owner) {
			if ($onlyType !== null && $onlyType !== $owner['type']) {
				continue;
			}
            $ok []= $owner;
		}
        return $ok;
	}
Psalm output (using commit 42462b8):

No issues!

psalm-github-bot[bot] avatar Aug 16 '22 19:08 psalm-github-bot[bot]

That one also has no issues :stuck_out_tongue:

AndrolGenhald avatar Aug 16 '22 22:08 AndrolGenhald

That's the point, weird how a !falsy is buggy but a !null assertion isn't

danog avatar Aug 16 '22 22:08 danog

I'm still a bit confused, none of the links you posted have issues. What's different?

AndrolGenhald avatar Aug 17 '22 14:08 AndrolGenhald

Huh, my bad, I'm apparently blind, must've been an issue on my outdated fork which I didn't notice had disappeared in the psalm.dev REPL :)

danog avatar Aug 17 '22 14:08 danog

Is this a false-positive maybe?

AndrolGenhald avatar Aug 17 '22 15:08 AndrolGenhald

I found these snippets:

https://psalm.dev/r/c23cbf01bc
<?php
	/**
     * @param array<array{type: string}> $v
	 * @param "prof"|"organization"|null $onlyType
     * @return ($onlyType is null ? array<array{type: string}> : array<array{type: $onlyType}>)
	 */
	function getOwnerObjects(array $v, ?string $onlyType = null): array {
        $ok = [];
		foreach ($v as $owner) {
			if ($onlyType && $onlyType !== $owner['type']) {
				continue;
			}
            $ok []= $owner;
		}
        return $ok;
	}
Psalm output (using commit 42462b8):

ERROR: RedundantCondition - 10:8 - Type TGeneratedFromParam1:fn-getownerobjects as 'organization'|'prof' for $onlyType is never falsy

psalm-github-bot[bot] avatar Aug 17 '22 15:08 psalm-github-bot[bot]

Whoops, yes it is :)

danog avatar Aug 17 '22 15:08 danog