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

Union assertions not detecting paradoxes #8322

Closed
danog opened this issue Jul 26, 2022 · 7 comments
Closed

Union assertions not detecting paradoxes #8322

danog opened this issue Jul 26, 2022 · 7 comments
Labels

Comments

@danog
Copy link
Collaborator

danog commented Jul 26, 2022

Possibly introduced by #8077: https://psalm.dev/r/49f98e503d

@psalm-github-bot
Copy link

psalm-github-bot bot commented Jul 26, 2022

I found these snippets:

https://psalm.dev/r/49f98e503d
<?php
                    namespace Bar;

                    /**
                     * Asserts that two variables are not the same.
                     *
                     * @template T
                     * @param T      $expected
                     * @param mixed  $actual
                     * @psalm-assert T $actual
                     */
                    function assertNotSame($expected, $actual) : void {}

                    $expected = rand(0, 1) ? 4 : 5;
                    $actual = 6;
                    assertNotSame($expected, $actual);
/** @psalm-trace $actual */;
Psalm output (using commit 4b2935f):

INFO: UnusedParam - 12:44 - Param $expected is never referenced in this method

INFO: UnusedParam - 12:55 - Param $actual is never referenced in this method

INFO: Trace - 17:28 - $actual: 6

@AndrolGenhald
Copy link
Collaborator

AndrolGenhald commented Jul 26, 2022

Shouldn't it assert !T?

Actually, I'm not sure what it should assert... Is this even possible to do correctly? If you have both $expected and $actual as strings then it shouldn't be asserting !T, but if $expected is string and $actual is float you don't want it asserting T either. Did this ever work? Do we actually check for paradoxes with assertion functions (I know we check redundancy, but I'm not sure I've ever seen paradoxes there)?

@danog
Copy link
Collaborator Author

danog commented Jul 26, 2022

I actually want to assertEqual: https://psalm.dev/r/2438f000d1
I expect it to complain that we expect a 4|5, but have only a 6: we're precisely missing paradox logic here.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2438f000d1
<?php
                    namespace Bar;

                    /**
                     * Asserts that two variables are not the same.
                     *
                     * @template T
                     * @param T      $expected
                     * @param mixed  $actual
                     * @psalm-assert T $actual
                     */
                    function assertSame($expected, $actual) : void {}

                    $expected = rand(0, 1) ? 4 : 5;
                    $actual = 6;
                    assertSame($expected, $actual);
/** @psalm-trace $actual */;
Psalm output (using commit 4b2935f):

INFO: UnusedParam - 12:41 - Param $expected is never referenced in this method

INFO: UnusedParam - 12:52 - Param $actual is never referenced in this method

INFO: Trace - 17:28 - $actual: 6

@AndrolGenhald
Copy link
Collaborator

assert works fine but behaves slightly differently, since it will generally be ignored in production environments. I wasn't aware we actually supported that, but it works for non-unions, so it should probably work for unions too.

@psalm-github-bot
Copy link

I found these snippets:

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

$expected = rand(0, 1) ? 4 : 5;
$actual = 6;
assert($expected === $actual);
/** @psalm-trace $actual */;
Psalm output (using commit 4b2935f):

ERROR: TypeDoesNotContainType - 5:8 - 6 cannot be identical to 4|5

ERROR: TypeDoesNotContainType - 5:1 - Type 4|5 for $expected is never =int(6)

INFO: Trace - 6:28 - $actual: 6
https://psalm.dev/r/97bbacb893
<?php
                    namespace Bar;

                    /**
                     * Asserts that two variables are not the same.
                     *
                     * @template T
                     * @param T      $expected
                     * @param mixed  $actual
                     * @psalm-assert T $actual
                     */
                    function assertSame($expected, $actual) : void {}

                    $expected = 5;
                    $actual = 6;
                    assertSame($expected, $actual);
/** @psalm-trace $actual */;
Psalm output (using commit 4b2935f):

ERROR: TypeDoesNotContainType - 16:21 - Type 6 for $actual is never 5

INFO: Trace - 17:28 - $actual: never

danog added a commit to nicelocal/psalm that referenced this issue Jul 27, 2022
@orklah
Copy link
Collaborator

orklah commented Jul 29, 2022

fixed in master :)

@orklah orklah closed this as completed Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants