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 not inferring generator template parameters when using ternary #8564

Closed
danog opened this issue Oct 11, 2022 · 2 comments
Closed

Psalm not inferring generator template parameters when using ternary #8564

danog opened this issue Oct 11, 2022 · 2 comments
Labels

Comments

@danog
Copy link
Collaborator

danog commented Oct 11, 2022

https://psalm.dev/r/35d889bd48

The error for the function with the ternary does not contain the actual template parameters for \Generator.

@psalm-github-bot
Copy link

psalm-github-bot bot commented Oct 11, 2022

I found these snippets:

https://psalm.dev/r/35d889bd48
<?php

/** @psalm-yield int */
class a {}

/**
 * @return Generator<mixed, mixed, mixed, int>
 */
function a(): Generator {
	return random_int(0, 1) ? 123 : yield new a;
}

/**
 * @return Generator<mixed, mixed, mixed, int>
 */
function b(): Generator {
	return yield new a;
}
Psalm output (using commit 028ac7f):

INFO: MixedReturnTypeCoercion - 7:12 - The declared return type 'Generator<mixed, mixed, mixed, int>' for a is more specific than the inferred return type 'Generator'

INFO: MixedReturnTypeCoercion - 14:12 - The declared return type 'Generator<mixed, mixed, mixed, int>' for b is more specific than the inferred return type 'Generator<int, a, mixed, mixed>'

@danog danog changed the title Psalm not inferring generator template parameters Psalm not inferring generator template parameters when using ternary Oct 11, 2022
@orklah
Copy link
Collaborator

orklah commented Oct 11, 2022

Pretty sure it should be handled here:

protected static function getYieldTypeFromExpression(

But this piece of code breaks my mind each time. It's a recursive method that collects every return type and yield type through each statement.

So it should have a Ternary handling somewhere (possibly around the place where we handle Ifs)

@orklah orklah added the bug label Oct 11, 2022
danog added a commit to nicelocal/psalm that referenced this issue Oct 13, 2022
@danog danog closed this as completed Oct 13, 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

2 participants