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

@psalm-return non-empty-list in combination with @psalm-assert non-empty-list does not work since version 3.8.2 #2636

Closed
smoench opened this issue Jan 16, 2020 · 5 comments

Comments

@smoench
Copy link

smoench commented Jan 16, 2020

https://github.com/webmozart/assert using psalm. With latest psalm version I got following error report:

ERROR: InvalidReturnType - tests/static-analysis/assert-isNonEmptyList.php:10:18 - The declared return type 'non-empty-list<mixed>' for Webmozart\Assert\Tests\StaticAnalysis\consume is incorrect, got 'list<mixed>'
 * @psalm-return non-empty-list

ERROR: InvalidReturnStatement - tests/static-analysis/assert-isNonEmptyList.php:16:12 - The type 'list<mixed>' does not match the declared return type 'non-empty-list<mixed>' for Webmozart\Assert\Tests\StaticAnalysis\consume
    return $value;

Following code is being used (https://github.com/webmozart/assert/blob/master/tests/static-analysis/assert-isNonEmptyList.php)

/**
 * @psalm-param mixed $value
 *
 * @psalm-return non-empty-list<mixed>
 */
function consume($value): array
{
    Assert::isNonEmptyList($value);
    return $value;
}

to test (https://github.com/webmozart/assert/blob/master/src/Assert.php#L1736)

   /**
     * @psalm-assert non-empty-list $array
     *
     * @param mixed  $array
     * @param string $message
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($array, $message = '')
    {
        static::isList($array, $message);
        static::notEmpty($array, $message);
    }

While I was wondering why it fails I could track down something has changed with psalm version 3.8.2. I could prove psalm is not reporting any error with version <3.8.2.

@psalm-github-bot
Copy link

Hey @smoench, can you reproduce the issue on https://psalm.dev ?

@weirdan
Copy link
Collaborator

weirdan commented Jan 16, 2020

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2e692148a5
<?php
  class Assert {
     /**
     * @psalm-assert non-empty-list<mixed> $array
     *
     * @param mixed  $_array
     * @param string $_message
     * @return void
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($_array, $_message = '')
    {
    } 
}
/**
 * @psalm-param mixed $value
 *
 * @psalm-return non-empty-list<mixed>
 */
function consume($value): array
{
    Assert::isNonEmptyList($value);
    return $value;
}
Psalm output (using commit d434f7f):

INFO: MixedReturnStatement - 24:12 - Could not infer a return type

INFO: MixedInferredReturnType - 19:18 - Could not verify return type 'non-empty-list<mixed>' for consume

@smoench
Copy link
Author

smoench commented Jan 16, 2020

https://psalm.dev/r/c53305ee8b (this one produces the described error report; the other snippet has an incorrect doc block)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c53305ee8b
<?php
  class Assert {
    /**
     * @psalm-assert non-empty-list $array
     *
     * @param mixed  $array
     * @param string $message
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($array, $message = ''): void
    {
    } 
}
/**
 * @psalm-param mixed $value
 *
 * @psalm-return non-empty-list<mixed>
 */
function consume($value): array
{
    Assert::isNonEmptyList($value);
    return $value;
}
Psalm output (using commit d434f7f):

ERROR: InvalidReturnStatement - 23:12 - The type 'list<mixed>' does not match the declared return type 'non-empty-list<mixed>' for consume

ERROR: InvalidReturnType - 18:18 - The declared return type 'non-empty-list<mixed>' for consume is incorrect, got 'list<mixed>'

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

No branches or pull requests

2 participants