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

literal strings as non-empty-string #5017

Closed
thomasvargiu opened this issue Jan 14, 2021 · 4 comments
Closed

literal strings as non-empty-string #5017

thomasvargiu opened this issue Jan 14, 2021 · 4 comments
Labels

Comments

@thomasvargiu
Copy link
Contributor

Should literal strings be a non-empty-string?

The problem I occurred is the following:

/**
 * @return list<non-empty-string>
 */
function a(): array
{
    // Imaging I have an external declared type like this:
    /** @var array{foo?: list<non-empty-string>} $array */
    $array = [];

    return $array['foo'] ?? ['bar'];
}

https://psalm.dev/r/0cb0b9aa53

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/0cb0b9aa53
<?php

/**
 * @return list<non-empty-string>
 */
function a(): array
{
    // Imaging I have a declared type like this:
    /** @var array{foo?: list<non-empty-string>} $array */
    $array = ['foo' => ['bar']];

    return $array['foo'] ?? ['bar'];
}
Psalm output (using commit ef4afd7):

INFO: LessSpecificReturnStatement - 12:12 - The type 'list<string>' is more general than the declared return type 'list<non-empty-string>' for a

INFO: MoreSpecificReturnType - 4:12 - The declared return type 'list<non-empty-string>' for a is more specific than the inferred return type 'list<string>'

@weirdan
Copy link
Collaborator

weirdan commented Jan 14, 2021

Should literal strings be a non-empty-string?

Not necessarily. '' is an empty literal string.

It's reconciliation between literal and non-empty strings that should result in non-empty string here, but that doesn't currently happen: https://psalm.dev/r/6d680d86b6

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6d680d86b6
<?php

/** @return non-empty-string */
function a()
{
    /** @var non-empty-string */
    $s = 'foo';
    if (rand(0, 1)) {
        $r = $s;
    } else {
        /** @psalm-trace $r */
        $r = 'bar';
    }
    // expected non-empty-string, but Psalm infers it as string
    /** @psalm-trace $r */; 
    return $r;
}
Psalm output (using commit ef4afd7):

INFO: Trace - 12:9 - $r: string(bar)

INFO: Trace - 15:27 - $r: string

INFO: LessSpecificReturnStatement - 16:12 - The type 'string' is more general than the declared return type 'non-empty-string' for a

INFO: MoreSpecificReturnType - 3:13 - The declared return type 'non-empty-string' for a is more specific than the inferred return type 'string'

@weirdan weirdan added the bug label Jan 14, 2021
@orklah
Copy link
Collaborator

orklah commented Jan 14, 2021

nvm

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