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

Generic parameter does not accept a union of generics #6073

Closed
azjezz opened this issue Jul 12, 2021 · 7 comments
Closed

Generic parameter does not accept a union of generics #6073

azjezz opened this issue Jul 12, 2021 · 7 comments

Comments

@azjezz
Copy link
Contributor

azjezz commented Jul 12, 2021

The new system for capturing generic constrains introduced in acc7ee2 breaks PSL build.

see: https://github.com/azjezz/psl/runs/3041183878?check_suite_focus=true

I couldn't make a smaller repro 🤔

@psalm-github-bot
Copy link

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

@weirdan
Copy link
Collaborator

weirdan commented Jul 25, 2021

Simplified to https://psalm.dev/r/86a60c6f13

@weirdan weirdan added the bug label Jul 25, 2021
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/86a60c6f13
<?php

/**
 * @template T
 * @param Container<T> $_p
 */
function acceptsContainer($_p): void {
}

/** @param Container<int>|Container<string> $var */
function ff($var): void {
	acceptsContainer($var);
}

/**
 * @template T
 */
interface Container {}

/**
 * @template T
 * @param T $_p
 * @return Container<T>
 * @psalm-suppress InvalidReturnType
 */
function makeContainer($_p): Container {}
Psalm output (using commit 82dfbbc):

ERROR: InvalidArgument - 12:2 - Incompatible types found for T

@weirdan weirdan changed the title Invalid InvalidArgument error Generic parameter does not accept a union of generics Jul 25, 2021
@weirdan
Copy link
Collaborator

weirdan commented Jul 25, 2021

@muglug any idea what could be wrong here? It looks like Psalm breaks union apart into constituent types and believes each of them is an upper (?) bound for T:acceptsContainer and that doesn't sound right.

@muglug
Copy link
Collaborator

muglug commented Jul 25, 2021

Yeah, have a look at this: https://psalm.dev/r/bbef3b2bdb

If there's one type for T then Psalm allows Container<int>|Container<string> to become Container<int|string> which is a violation of generic stuff.

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template T
 * @param Container<T> $p
 * @return Container<T>
 */
function acceptsContainer($p): Container {
    return $p;
}

/**
 * @param Container<int>|Container<string> $var
 * @return Container<int|string>
 */
function ff($var): Container {
	return acceptsContainer($var);
}

/**
 * @template T
 */
interface Container {}

/**
 * @template T
 * @param T $_p
 * @return Container<T>
 * @psalm-suppress InvalidReturnType
 */
function makeContainer($_p): Container {}
Psalm output (using commit 82dfbbc):

ERROR: InvalidArgument - 17:9 - Incompatible types found for T

@muglug
Copy link
Collaborator

muglug commented Jul 25, 2021

PHPStan emits a slightly different but comparable error: https://phpstan.org/r/b1097877-6b2c-4f5b-b897-30a0b069bad4

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

3 participants