Skip to content

Commit 7dbc598

Browse files
committed
Infer an error for an intersection raised to an invalid exponent
A member's ErrorType is a MixedType, so intersecting it with the other members' powers hid it: numeric-string ** 'abc' was float|int although it always throws a TypeError. An erroring member now decides for the intersection. Amp-Thread-ID: https://ampcode.com/threads/T-01a1a1e7-5e72-7235-a4b2-ba1b689f7e1b
1 parent 224dcab commit 7dbc598

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/Type/IntersectionType.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,18 @@ public function tryRemove(Type $typeToRemove): ?Type
17761776

17771777
public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
17781778
{
1779-
return $this->intersectTypes(static fn (Type $type): Type => $type->arithmeticPower($exponent, $phpVersion));
1779+
// a member that errors decides for the value; intersecting an ErrorType,
1780+
// a MixedType, with the other members' results would hide it
1781+
$results = [];
1782+
foreach ($this->types as $type) {
1783+
$result = $type->arithmeticPower($exponent, $phpVersion);
1784+
if ($result instanceof ErrorType) {
1785+
return $result;
1786+
}
1787+
$results[] = $result;
1788+
}
1789+
1790+
return self::intersectOperands($results);
17801791
}
17811792

17821793
public function getFiniteTypes(): array
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PowerAccessoryString;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param numeric-string $numericString
9+
* @param non-empty-string $nonEmptyString
10+
* @param decimal-int-string $decimalIntString
11+
*/
12+
function doFoo(string $numericString, string $nonEmptyString, string $decimalIntString): void
13+
{
14+
// a non-numeric string exponent is a TypeError whatever the base is
15+
assertType('*ERROR*', $numericString ** '');
16+
assertType('*ERROR*', $numericString ** 'abc');
17+
assertType('*ERROR*', $nonEmptyString ** 'abc');
18+
assertType('*ERROR*', $decimalIntString ** 'abc');
19+
20+
// a numeric exponent stays valid
21+
assertType('(float|int)', $numericString ** '2');
22+
assertType('(float|int)', $decimalIntString ** 2);
23+
}

‎turbo-ext/src/IntersectionType.cpp‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,10 +2139,25 @@ class IntersectionType
21392139
});
21402140
}
21412141

2142+
/* every member's power intersected, unless a member's is an ErrorType,
2143+
* which decides for the value (intersecting it, a MixedType, would hide
2144+
* it) */
21422145
zv::Val arithmeticPower(zval *exponent, zval *phpVersion) const
21432146
{
2147+
zval *types = this->types();
2148+
if (UNEXPECTED(types == NULL)) return zv::Val();
2149+
zv::Val typesCopy = zv::Val::copyOf(zv::Ref(types));
21442150
zv::Args args{exponent, phpVersion};
2145-
return intersectCall(PT_LC("arithmeticpower"), 2, args);
2151+
zv::Arr results = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(typesCopy.raw())));
2152+
for (zv::ArrayEntry entry : zv::ArrRef(typesCopy.raw())) {
2153+
zv::Val result = callType(entry.value().deref().asObject(), PT_LC("arithmeticpower"), 2, args);
2154+
if (UNEXPECTED(result.isUndef())) return zv::Val();
2155+
if (Z_TYPE_P(result.raw()) == IS_OBJECT && instanceof_function(Z_OBJCE_P(result.raw()), pt_ce_error_type)) {
2156+
return result;
2157+
}
2158+
results.push(std::move(result));
2159+
}
2160+
return intersectOperands(results.arrRef());
21462161
}
21472162

21482163
/* the finite types every member has, keyed by enum case or type-only

‎turbo-ext/tests/type-family.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7799,7 +7799,7 @@ public function getResolvedPhpDoc(): ?\PHPStan\PhpDoc\ResolvedPhpDocBlock { retu
77997799
'mod' => '%',
78007800
];
78017801
$integerOperands = ['int0' => $int(0), 'int7' => $int(7), 'int-3' => $int(-3), 'intMax' => $int(PHP_INT_MAX), 'intMin' => $int(PHP_INT_MIN), 'range0-10' => $range(0, 10), 'rangeMin--1' => $range(null, -1), 'range5-max' => $range(5, null), 'range-4-6' => $range(-4, 6), 'rangeMin-max' => $range(-1000, null), 'unionRangeConst' => $operands['unionRangeConst'], 'benevolentRanges' => new \PHPStan\Type\BenevolentUnionType([$range(0, 3), $int(-8)])];
7802-
$numericStringOperands = ['overflowingNumericString' => new \PHPStan\Type\Constant\ConstantStringType('9223372036854775808'), 'negativeOverflowingNumericString' => new \PHPStan\Type\Constant\ConstantStringType('-9223372036854775809'), 'leadingSpace' => new \PHPStan\Type\Constant\ConstantStringType(' 1'), 'plusSign' => new \PHPStan\Type\Constant\ConstantStringType('+5'), 'trailingNewline' => new \PHPStan\Type\Constant\ConstantStringType("5\n"), 'exponent' => new \PHPStan\Type\Constant\ConstantStringType('1.5e3'), 'trailingSpace' => new \PHPStan\Type\Constant\ConstantStringType('1 '), 'decimalIntString' => new \PHPStan\Type\IntersectionType([new \PHPStan\Type\StringType(), new \PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType()])];
7802+
$numericStringOperands = ['overflowingNumericString' => new \PHPStan\Type\Constant\ConstantStringType('9223372036854775808'), 'negativeOverflowingNumericString' => new \PHPStan\Type\Constant\ConstantStringType('-9223372036854775809'), 'leadingSpace' => new \PHPStan\Type\Constant\ConstantStringType(' 1'), 'plusSign' => new \PHPStan\Type\Constant\ConstantStringType('+5'), 'trailingNewline' => new \PHPStan\Type\Constant\ConstantStringType("5\n"), 'exponent' => new \PHPStan\Type\Constant\ConstantStringType('1.5e3'), 'trailingSpace' => new \PHPStan\Type\Constant\ConstantStringType('1 '), 'nonNumericString' => new \PHPStan\Type\Constant\ConstantStringType('abc'), 'decimalIntString' => new \PHPStan\Type\IntersectionType([new \PHPStan\Type\StringType(), new \PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType()])];
78037803
foreach ($integerOperands + $numericStringOperands as $name => $operand) {
78047804
foreach ($operand->getConstantScalarTypes() as $i => $constant) {
78057805
$r["toIntegerOperand $name $i"] = $catching(static fn () => $staticPrivate('toIntegerOperand', $constant));

0 commit comments

Comments
 (0)