Skip to content

Commit ac5fe1b

Browse files
committed
TypeCombinator - fix segfault
1 parent ac6a102 commit ac5fe1b

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/Type/Generic/TemplateTypeTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Type\Generic;
44

55
use PHPStan\TrinaryLogic;
6-
use PHPStan\Type\CompoundType;
76
use PHPStan\Type\IntersectionType;
87
use PHPStan\Type\MixedType;
98
use PHPStan\Type\Type;
@@ -123,7 +122,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
123122

124123
public function isSuperTypeOf(Type $type): TrinaryLogic
125124
{
126-
if ($type instanceof CompoundType) {
125+
if ($type instanceof self) {
127126
return $type->isSubTypeOf($this);
128127
}
129128

tests/PHPStan/Analyser/data/instanceof.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,23 @@ public function testExprInstanceof($subject, string $classString, $union, $inter
133133
assertType('bool', $subject instanceof $objectT);
134134
} else {
135135
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
136-
assertType('false', $subject instanceof $objectT);
136+
assertType('bool', $subject instanceof $objectT); // can be false
137137
}
138138

139139
if ($subject instanceof $objectTString) {
140140
assertType('ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
141141
assertType('bool', $subject instanceof $objectTString);
142142
} else {
143143
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
144-
assertType('false', $subject instanceof $objectTString);
144+
assertType('bool', $subject instanceof $objectTString); // can be false
145145
}
146146

147147
if ($subject instanceof $mixedTString) {
148148
assertType('MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)&object', $subject);
149149
assertType('bool', $subject instanceof $mixedTString);
150150
} else {
151151
assertType('mixed~MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
152-
assertType('false', $subject instanceof $mixedTString);
152+
assertType('bool', $subject instanceof $mixedTString); // can be false
153153
}
154154

155155
if ($subject instanceof $string) {

tests/PHPStan/Rules/Methods/data/returnTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ public function doBar(\DateTimeInterface $date): \DateTimeImmutable
11841184
}
11851185

11861186
/**
1187-
* @template CollectionKey
1187+
* @template CollectionKey of array-key
11881188
* @template CollectionValue
11891189
* @implements \Iterator<CollectionKey, CollectionValue>
11901190
*/

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,39 @@ public function dataUnion(): array
18221822
UnionType::class,
18231823
'(T of bool|float|int|string (function doFoo(), parameter))|null',
18241824
],
1825+
[
1826+
[
1827+
new UnionType([
1828+
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
1829+
IntegerRangeType::fromInterval(null, -1),
1830+
IntegerRangeType::fromInterval(1, null),
1831+
]),
1832+
TemplateTypeFactory::create(
1833+
TemplateTypeScope::createWithClass('Foo'),
1834+
'TCode',
1835+
new UnionType([new ArrayType(new IntegerType(), new IntegerType()), new IntegerType()]),
1836+
TemplateTypeVariance::createInvariant()
1837+
),
1838+
],
1839+
UnionType::class,
1840+
'array|int<min, -1>|int<1, max>|(TCode of array<int, int>|int (class Foo, parameter))',
1841+
],
1842+
[
1843+
[
1844+
new UnionType([
1845+
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
1846+
new CallableType(),
1847+
]),
1848+
TemplateTypeFactory::create(
1849+
TemplateTypeScope::createWithClass('Foo'),
1850+
'TCode',
1851+
new UnionType([new ArrayType(new IntegerType(), new IntegerType()), new IntegerType()]),
1852+
TemplateTypeVariance::createInvariant()
1853+
),
1854+
],
1855+
UnionType::class,
1856+
'array|(callable(): mixed)|(TCode of array<int, int>|int (class Foo, parameter))',
1857+
],
18251858
];
18261859
}
18271860

0 commit comments

Comments
 (0)