Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Analyser/ExpressionTypeHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public function and(self $other): self
);
}

/** A holder of $type for the values this holder and $other hold, still narrowed from the value both were narrowed from. */
public function generalize(self $other, Type $type): self
{
$narrowedFrom = $this->narrowedFrom ?? $this->type;
$otherNarrowedFrom = $other->narrowedFrom ?? $other->type;
if (
($this->narrowedFrom !== null || $other->narrowedFrom !== null)
&& ($narrowedFrom === $otherNarrowedFrom || $narrowedFrom->equals($otherNarrowedFrom))
) {
return new self($this->expr, $type, $this->certainty, $narrowedFrom);
}

return new self($this->expr, $type, $this->certainty);
}

/** A holder of the same value refined into $type. */
public function narrow(Expr $expr, Type $type, TrinaryLogic $certainty): self
{
Expand Down
6 changes: 1 addition & 5 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6134,11 +6134,7 @@ private function generalizeVariableTypeHolders(
) {
$generalizedExpressions[$variableExprString] = $variableTypeHolder->getExpr();
}
$newVariableTypeHolders[$variableExprString] = new ExpressionTypeHolder(
$variableTypeHolder->getExpr(),
$generalizedType,
$variableTypeHolder->getCertainty(),
);
$newVariableTypeHolders[$variableExprString] = $variableTypeHolder->generalize($otherVariableTypeHolders[$variableExprString], $generalizedType);
}

return $newVariableTypeHolders;
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15942.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Bug15942;

use function PHPStan\Testing\assertType;

/** @param mixed $items */
function assignsInBody($items): int
{
$list = [];
foreach ($items as $item) {
$list[] = $item;
}

assertType('mixed', $items);
return count($items) + count($list);
}

/** @param array<string, mixed> $data */
function assignsInBodyOffset(array $data): int
{
$list = [];
foreach ($data['items'] as $item) {
$list[] = $item;
}

assertType('mixed', $data['items']);
return count($data['items']) + count($list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4040,6 +4040,12 @@ public function testBug15943(): void
]);
}

public function testBug15942(): void
{
$this->checkMixedWitnessTypes = true;
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-15942.php'], []);
}

public function testRecursiveTypes(): void
{
$this->analyse([__DIR__ . '/data/recursive-types.php'], [
Expand Down
17 changes: 17 additions & 0 deletions turbo-ext/src/ExpressionTypeHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class ExpressionTypeHolder
return zv::Val::adopt(result);
}

/* UNDEF = pending exception */
zv::Val generalize(zval *other, zval *type) const
{
zval result;
if (UNEXPECTED(!pt_holder_generalize(self, other, type, &result))) return zv::Val();
return zv::Val::adopt(result);
}

/* a holder of the same value refined into $type */
zv::Val narrow(zval *expr, zval *type, zval *certainty) const
{
Expand Down Expand Up @@ -122,6 +130,15 @@ PT_MINIT_REGISTRATION(pt_register_expression_type_holder)
ExpressionTypeHolder(ZEND_THIS).narrow(expr, type, certainty).intoReturnValue(return_value);
});

cls.method(sigs::generalize, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *other, *type;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(other, pt_ce_expr_type_holder)
Z_PARAM_OBJECT(type)
ZEND_PARSE_PARAMETERS_END();
ExpressionTypeHolder(ZEND_THIS).generalize(other, type).intoReturnValue(return_value);
});

cls.method(sigs::createYes, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *expr, *type;
if (!zp::parse<zp::Obj, zp::Obj>(execute_data, expr, type)) RETURN_THROWS();
Expand Down
4 changes: 1 addition & 3 deletions turbo-ext/src/MutatingScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11135,10 +11135,8 @@ class MutatingScope
if (!equal) {
generalizedExpressions.set(variableExprString.get(), zv::Val::copyOf(variableExpr.ref()));
}
zend_long certainty = holderCertainty(zv::Ref(entry.value));
if (UNEXPECTED(certainty < 0)) return zv::Val();
zval holderZv;
pt_holder_create(&holderZv, variableExpr.raw(), generalizedType.raw(), certainty);
if (UNEXPECTED(!pt_holder_generalize(entry.value, zv::Ref(otherHolder).deref().raw(), generalizedType.raw(), &holderZv))) return zv::Val();
newVariableTypeHolders.set(variableExprString.get(), zv::Val::adopt(holderZv));
}

Expand Down
25 changes: 15 additions & 10 deletions turbo-ext/src/generated/ExpressionTypeHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ inline constexpr char strings[] =
"equalTypes\0" /* 176 */
"equals\0" /* 187 */
"and\0" /* 194 */
"narrow\0" /* 198 */
"getExpr\0" /* 205 */
"getType\0" /* 213 */
"getCertainty\0" /* 221 */
"getNarrowedFrom"; /* 234 */
"generalize\0" /* 198 */
"narrow\0" /* 209 */
"getExpr\0" /* 216 */
"getType\0" /* 224 */
"getCertainty\0" /* 232 */
"getNarrowedFrom"; /* 245 */
inline constexpr reg::PackedArg args[] = {
reg::packed(0, 0, 5), /* __construct $expr */
reg::packed(25, 0, 30), /* __construct $type */
Expand All @@ -72,6 +73,9 @@ inline constexpr reg::PackedArg args[] = {
reg::packed(109, MAY_BE_BOOL), /* equals return */
reg::packed(170, 0, 110), /* and $other */
reg::packed(109, 0, 110), /* and return */
reg::packed(170, 0, 110), /* generalize $other */
reg::packed(25, 0, 30), /* generalize $type */
reg::packed(109, 0, 110), /* generalize return */
reg::packed(0, 0, 5), /* narrow $expr */
reg::packed(25, 0, 30), /* narrow $type */
reg::packed(48, 0, 58), /* narrow $certainty */
Expand All @@ -92,11 +96,12 @@ inline constexpr sigtab::Sig createMaybe = { { 158 /* createMaybe */, 2, 7, 2, 9
inline constexpr sigtab::Sig equalTypes = { { 176 /* equalTypes */, 1, 10, 1, 11, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig equals = { { 187 /* equals */, 1, 12, 1, 13, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig and_ = { { 194 /* and */, 1, 14, 1, 15, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig narrow = { { 198 /* narrow */, 3, 16, 3, 19, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getExpr = { { 205 /* getExpr */, 0, 20, 0, 20, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getType = { { 213 /* getType */, 0, 21, 0, 21, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getCertainty = { { 221 /* getCertainty */, 0, 22, 0, 22, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getNarrowedFrom = { { 234 /* getNarrowedFrom */, 0, 23, 0, 23, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig generalize = { { 198 /* generalize */, 2, 16, 2, 18, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig narrow = { { 209 /* narrow */, 3, 19, 3, 22, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getExpr = { { 216 /* getExpr */, 0, 23, 0, 23, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getType = { { 224 /* getType */, 0, 24, 0, 24, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getCertainty = { { 232 /* getCertainty */, 0, 25, 0, 25, ZEND_ACC_PUBLIC } };
inline constexpr sigtab::Sig getNarrowedFrom = { { 245 /* getNarrowedFrom */, 0, 26, 0, 26, ZEND_ACC_PUBLIC } };
} // namespace sig

} // namespace ptdecl::ExpressionTypeHolder
Expand Down
12 changes: 12 additions & 0 deletions turbo-ext/src/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,18 @@ void pt_holder_narrow(zval *holder, zval *expr, zval *type, zend_long certainty,
pt_holder_create_narrowed(result, expr, type, certainty, pt_holder_origin(Z_OBJ_P(holder)));
}

bool pt_holder_generalize(zval *holder, zval *other, zval *type, zval *result)
{
zend_object *ho = Z_OBJ_P(holder);
zend_object *oo = Z_OBJ_P(other);
zval *origin = pt_holder_origin(ho);
bool sameOrigin = (Z_TYPE_P(OBJ_PROP_NUM(ho, PT_ETH_PROP_NARROWED_FROM)) == IS_OBJECT || Z_TYPE_P(OBJ_PROP_NUM(oo, PT_ETH_PROP_NARROWED_FROM)) == IS_OBJECT)
&& pt_types_identical_or_equal(origin, pt_holder_origin(oo));
if (UNEXPECTED(EG(exception))) return false;
pt_holder_create_narrowed(result, OBJ_PROP_NUM(ho, PT_ETH_PROP_EXPR), type, pt_holder_certainty_value(ho), sameOrigin ? origin : NULL);
return true;
}

bool pt_holder_and(zval *a, zval *b, zval *result)
{
zend_object *ao = Z_OBJ_P(a);
Expand Down
2 changes: 2 additions & 0 deletions turbo-ext/src/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ void pt_holder_create_narrowed(zval *result, zval *expr, zval *type, zend_long c
/* $holder->narrow($expr, $type, $certainty) */
void pt_holder_narrow(zval *holder, zval *expr, zval *type, zend_long certainty, zval *result);
bool pt_holder_and(zval *a, zval *b, zval *result);
/* $holder->generalize($other, $type); false = pending exception */
bool pt_holder_generalize(zval *holder, zval *other, zval *type, zval *result);
bool pt_holder_equals(zval *a, zval *b, bool *out);
bool pt_holder_equal_types(zval *a, zval *b, bool *out);

Expand Down
23 changes: 23 additions & 0 deletions turbo-ext/tests/smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ function check(bool $cond, string $msg): void
check($pa->getExpr() === $na->getExpr() || $pa->getExpr()->name === $na->getExpr()->name, "ETH and expr");
}
}
// generalize(): keeps the origin only when both holders were narrowed from the same value
$narrowedFromOf = static fn ($holder) => (fn () => $this->narrowedFrom)->call($holder)?->describe(\PHPStan\Type\VerbosityLevel::precise());
$generalizeHolders = static fn ($h, $all) => [
'narrowed' => $h($expr1, $int, $all['yes'])->narrow($expr1, $string, $all['yes']),
'narrowedTwice' => $h($expr1, $int, $all['maybe'])->narrow($expr1, $string, $all['maybe'])->narrow($expr1, $string, $all['maybe']),
'origin' => $h($expr1, $int, $all['maybe']),
'other' => $h($expr1, $string, $all['yes']),
'otherNarrowed' => $h($expr1, $string, $all['yes'])->narrow($expr1, $string, $all['yes']),
];
$pGeneralize = $generalizeHolders($pH, $pAll);
$nGeneralize = $generalizeHolders($nH, $nAll);
foreach ($pGeneralize as $a => $pA) {
foreach ($pGeneralize as $b => $pB) {
$pg = $pA->generalize($pB, $int2);
$ng = $nGeneralize[$a]->generalize($nGeneralize[$b], $int2);
check($pg->getType() === $int2 && $ng->getType() === $int2, "ETH generalize type $a/$b");
check($pg->getCertainty()->describe() === $ng->getCertainty()->describe(), "ETH generalize certainty $a/$b");
check($pg->getExpr() === $ng->getExpr(), "ETH generalize expr $a/$b");
check($narrowedFromOf($pg) === $narrowedFromOf($ng), "ETH generalize narrowedFrom $a/$b: " . var_export($narrowedFromOf($pg), true) . ' vs ' . var_export($narrowedFromOf($ng), true));
}
}
check($narrowedFromOf($nGeneralize['narrowed']->generalize($nGeneralize['origin'], $int2)) === 'int', 'ETH generalize keeps a shared origin');
check($narrowedFromOf($nGeneralize['narrowed']->generalize($nGeneralize['otherNarrowed'], $int2)) === null, 'ETH generalize drops differing origins');
// identity semantics of and(): same type object, certainty yes+yes -> $this
$n1 = $nH($expr1, $int, $nYes);
$n2 = $nH($expr2, $int, $nYes);
Expand Down
Loading