|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace NeverIntoUnionTemplate; |
| 4 | + |
| 5 | +use Traversable; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +/** @template-covariant T */ |
| 9 | +interface P |
| 10 | +{ |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * @template T |
| 16 | + * @param callable(): (T|null) $cb |
| 17 | + * @return P<T> |
| 18 | + */ |
| 19 | +function tOrNull(callable $cb): P |
| 20 | +{ |
| 21 | + throw new \Exception(); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * @template T |
| 26 | + * @param callable(): (T|int|float) $cb |
| 27 | + * @return P<T> |
| 28 | + */ |
| 29 | +function tOrIntFloat(callable $cb): P |
| 30 | +{ |
| 31 | + throw new \Exception(); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * @template T |
| 36 | + * @param callable(): (array<T>|T) $cb |
| 37 | + * @return P<T> |
| 38 | + */ |
| 39 | +function arrayOrT(callable $cb): P |
| 40 | +{ |
| 41 | + throw new \Exception(); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @template T |
| 46 | + * @param callable(): (iterable<T>|T) $cb |
| 47 | + * @return P<T> |
| 48 | + */ |
| 49 | +function iterableOrT(callable $cb): P |
| 50 | +{ |
| 51 | + throw new \Exception(); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @template T |
| 56 | + * @param callable(): (Traversable<T>|T) $cb |
| 57 | + * @return P<T> |
| 58 | + */ |
| 59 | +function traversableOrT(callable $cb): P |
| 60 | +{ |
| 61 | + throw new \Exception(); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @template T |
| 66 | + * @param callable(): (P<T>|T) $cb |
| 67 | + * @return P<T> |
| 68 | + */ |
| 69 | +function pOrT(callable $cb): P |
| 70 | +{ |
| 71 | + throw new \Exception(); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @template T |
| 76 | + * @param callable(): iterable<T> $cb |
| 77 | + * @return P<T> |
| 78 | + */ |
| 79 | +function iterable(callable $cb): P |
| 80 | +{ |
| 81 | + throw new \Exception(); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @template T |
| 86 | + * @param P<P<T>|T> $p |
| 87 | + * @return P<T> |
| 88 | + */ |
| 89 | +function nestedInGeneric(P $p): P |
| 90 | +{ |
| 91 | + throw new \Exception(); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @template T |
| 96 | + * @param array{P<T>|T} $a |
| 97 | + * @return P<T> |
| 98 | + */ |
| 99 | +function nestedInArrayShape(array $a): P |
| 100 | +{ |
| 101 | + throw new \Exception(); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @param P<never> $pNever |
| 106 | + * @param array{never} $aNever |
| 107 | + */ |
| 108 | +function test(P $pNever, array $aNever): void |
| 109 | +{ |
| 110 | + $throwing = static function (): void { |
| 111 | + throw new \Exception(); |
| 112 | + }; |
| 113 | + |
| 114 | + assertType('NeverIntoUnionTemplate\\P<never>', tOrNull($throwing)); |
| 115 | + assertType('NeverIntoUnionTemplate\\P<never>', tOrIntFloat($throwing)); |
| 116 | + assertType('NeverIntoUnionTemplate\\P<never>', arrayOrT($throwing)); |
| 117 | + assertType('NeverIntoUnionTemplate\\P<never>', iterableOrT($throwing)); |
| 118 | + assertType('NeverIntoUnionTemplate\\P<never>', traversableOrT($throwing)); |
| 119 | + assertType('NeverIntoUnionTemplate\\P<never>', pOrT($throwing)); |
| 120 | + |
| 121 | + // no union involved: consistent with array<T> and Traversable<T>, which |
| 122 | + // infer nothing from never either |
| 123 | + assertType('NeverIntoUnionTemplate\\P<mixed>', iterable($throwing)); |
| 124 | + |
| 125 | + assertType('NeverIntoUnionTemplate\\P<never>', nestedInGeneric($pNever)); |
| 126 | + assertType('NeverIntoUnionTemplate\\P<never>', nestedInArrayShape($aNever)); |
| 127 | + |
| 128 | + $returningNull = static fn () => null; |
| 129 | + $returningInt = static fn (): int => 1; |
| 130 | + |
| 131 | + assertType('NeverIntoUnionTemplate\\P<mixed>', tOrNull($returningNull)); |
| 132 | + assertType('NeverIntoUnionTemplate\\P<mixed>', tOrIntFloat($returningInt)); |
| 133 | +} |
0 commit comments