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

Weird !falsy assertion paradox when using template conditionals #8412

Open
danog opened this issue Aug 16, 2022 · 12 comments
Open

Weird !falsy assertion paradox when using template conditionals #8412

danog opened this issue Aug 16, 2022 · 12 comments

Comments

@danog
Copy link
Collaborator

danog commented Aug 16, 2022

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 danog changed the title Weird assertion paradox when using template conditionals Weird !falsy assertion paradox when using template conditionals Aug 16, 2022
@psalm-github-bot
Copy link

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!

@AndrolGenhald
Copy link
Collaborator

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

Those are the same link.

@psalm-github-bot
Copy link

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!

@danog
Copy link
Collaborator Author

danog commented Aug 16, 2022

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

@psalm-github-bot
Copy link

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!

@AndrolGenhald
Copy link
Collaborator

That one also has no issues 😛

@danog
Copy link
Collaborator Author

danog commented Aug 16, 2022

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

@AndrolGenhald
Copy link
Collaborator

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

@danog
Copy link
Collaborator Author

danog commented Aug 17, 2022

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 danog closed this as completed Aug 17, 2022
@AndrolGenhald
Copy link
Collaborator

Is this a false-positive maybe?

@psalm-github-bot
Copy link

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

@danog
Copy link
Collaborator Author

danog commented Aug 17, 2022

Whoops, yes it is :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants