psalm icon indicating copy to clipboard operation
psalm copied to clipboard

@psalm-type does not support parentheses

Open sctice-ifixit opened this issue 3 years ago • 3 comments

https://psalm.dev/r/a55241a2df

The error message implies a syntax error, but it seems to be the case that parentheses are just unsupported in @psalm-type. Perhaps that's by design (last I checked, the docs have very little to say about parentheses). If it is intentional or not worth supporting, perhaps the error message could be a little more straightforward? "Unterminated parentheses" seems incorrect; perhaps "Parentheses are not supported in @psalm-type"?

The parentheses in the example are obviously superfluous. It's less obvious (though probably still superfluous?) if you want to define a large union over multiple lines.

sctice-ifixit avatar Oct 29 '21 15:10 sctice-ifixit

I found these snippets:

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

/** @psalm-type arr = (list<string>) */
class Demo {
    /** @return arr */
    public function f() {
        return ["a"];
    }
}
Psalm output (using commit 8c33b21):

ERROR: InvalidDocblock - 4:1 - Unterminated parentheses

ERROR: UndefinedDocblockClass - 5:17 - Docblock-defined class, interface or enum named arr does not exist

ERROR: InvalidReturnStatement - 7:16 - The inferred type 'array{"a"}' does not match the declared return type 'arr' for Demo::f

ERROR: InvalidReturnType - 5:17 - The declared return type 'arr' for Demo::f is incorrect, got 'array{"a"}'

psalm-github-bot[bot] avatar Oct 29 '21 15:10 psalm-github-bot[bot]

Hi Shawn! I ran into the same thing, but I think it's even more odd.

@psalm-type, anything after the > greater-than symbol is just straight-up ignored by the parser:

https://psalm.dev/r/67abe4ae45

danielbeardsley avatar Jul 21 '22 22:07 danielbeardsley

I found these snippets:

https://psalm.dev/r/67abe4ae45
<?php

/**
 * @psalm-type maybe0 = (array<string>)
 */
/**
 * @psalm-type maybe1 = null|array<string>
 * @psalm-type maybe2 = array<string>|null
 */

/** @var maybe0 */
$x = 1;
    
/** @psalm-trace $x */
isset($x);


/** @var maybe1 */
$x = 1;
    
/** @psalm-trace $x */
isset($x);


/** @var maybe2 */
$x = 1;
    
/** @psalm-trace $x */
isset($x);
Psalm output (using commit f6fb715):

ERROR: InvalidDocblock - 12:1 - Unterminated parentheses

ERROR: UndefinedDocblockClass - 12:1 - Docblock-defined class, interface or enum named maybe0 does not exist

INFO: Trace - 15:1 - $x: maybe0

INFO: Trace - 22:1 - $x: array<array-key, string>|null

INFO: Trace - 29:1 - $x: array<array-key, string>

psalm-github-bot[bot] avatar Jul 21 '22 22:07 psalm-github-bot[bot]