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 crash on @return ( #6159

Closed
someniatko opened this issue Jul 23, 2021 · 4 comments · Fixed by #6172
Closed

Psalm crash on @return ( #6159

someniatko opened this issue Jul 23, 2021 · 4 comments · Fixed by #6172

Comments

@someniatko
Copy link
Contributor

https://psalm.dev/r/b566eeeb37

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @return (
 */
function foo () {}
Psalm encountered an internal error:

/vendor/vimeo/psalm/src/Psalm/Internal/Type/TypeParser.php: Argument 1 passed to Psalm\Internal\Type\TypeParser::getTypeFromTree() must be an instance of Psalm\Internal\Type\ParseTree, null given, called in /vendor/vimeo/psalm/src/Psalm/Internal/Type/TypeParser.php on line 187

@Jack97
Copy link
Contributor

Jack97 commented Jul 24, 2021

I've been looking into this issue. The cause is when an EncapsulationTree has an empty array of children during the TypeParser::getTypeFromTree phase.

Psalm will crash with any variation of the following input for @return: (, (), ((())) etc. This can be easily fixed by verifying that the parse tree has children before recursively evaluating the type of its first child.

On a slightly unrelated note, I've also found that (((((string and ((((string)))) are valid aliases for string according to the parser. This doesn't feel quite right to me. The former can be fixed by verifying that the EncapsulationTree has been terminated before continuing. However, I'm not sure what to do about the latter 🤔

@weirdan
Copy link
Collaborator

weirdan commented Jul 24, 2021

I don't think anything needs to be done about the latter, as long as parens are balanced. Parentheses are valid in a type, they are used to override union vs intersection precedence (e.g. (A|B) & C) and to enclose conditional types. So a single type wrapped into a parenthesis is valid as well.

@Jack97
Copy link
Contributor

Jack97 commented Jul 24, 2021

Ah okay, thank you for the information - I thought I might have been missing something. I'll raise a PR for this as soon as I can 😄

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

Successfully merging a pull request may close this issue.

3 participants