Skip to content

Commit

Permalink
TypeCombinator - fix segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 4, 2021
1 parent ac6a102 commit ac5fe1b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Type\Generic;

use PHPStan\TrinaryLogic;
use PHPStan\Type\CompoundType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -123,7 +122,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof CompoundType) {
if ($type instanceof self) {
return $type->isSubTypeOf($this);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/instanceof.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ public function testExprInstanceof($subject, string $classString, $union, $inter
assertType('bool', $subject instanceof $objectT);
} else {
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
assertType('false', $subject instanceof $objectT);
assertType('bool', $subject instanceof $objectT); // can be false
}

if ($subject instanceof $objectTString) {
assertType('ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
assertType('bool', $subject instanceof $objectTString);
} else {
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
assertType('false', $subject instanceof $objectTString);
assertType('bool', $subject instanceof $objectTString); // can be false
}

if ($subject instanceof $mixedTString) {
assertType('MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)&object', $subject);
assertType('bool', $subject instanceof $mixedTString);
} else {
assertType('mixed~MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject);
assertType('false', $subject instanceof $mixedTString);
assertType('bool', $subject instanceof $mixedTString); // can be false
}

if ($subject instanceof $string) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/data/returnTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ public function doBar(\DateTimeInterface $date): \DateTimeImmutable
}

/**
* @template CollectionKey
* @template CollectionKey of array-key
* @template CollectionValue
* @implements \Iterator<CollectionKey, CollectionValue>
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,39 @@ public function dataUnion(): array
UnionType::class,
'(T of bool|float|int|string (function doFoo(), parameter))|null',
],
[
[
new UnionType([
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
IntegerRangeType::fromInterval(null, -1),
IntegerRangeType::fromInterval(1, null),
]),
TemplateTypeFactory::create(
TemplateTypeScope::createWithClass('Foo'),
'TCode',
new UnionType([new ArrayType(new IntegerType(), new IntegerType()), new IntegerType()]),
TemplateTypeVariance::createInvariant()
),
],
UnionType::class,
'array|int<min, -1>|int<1, max>|(TCode of array<int, int>|int (class Foo, parameter))',
],
[
[
new UnionType([
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
new CallableType(),
]),
TemplateTypeFactory::create(
TemplateTypeScope::createWithClass('Foo'),
'TCode',
new UnionType([new ArrayType(new IntegerType(), new IntegerType()), new IntegerType()]),
TemplateTypeVariance::createInvariant()
),
],
UnionType::class,
'array|(callable(): mixed)|(TCode of array<int, int>|int (class Foo, parameter))',
],
];
}

Expand Down

0 comments on commit ac5fe1b

Please sign in to comment.