diff --git a/src/Analyser/ExpressionTypeHolder.php b/src/Analyser/ExpressionTypeHolder.php index d0d34e9a97a..d05a081ebc5 100644 --- a/src/Analyser/ExpressionTypeHolder.php +++ b/src/Analyser/ExpressionTypeHolder.php @@ -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 { diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 7dc63fbc9df..915ec1ed3a0 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -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; diff --git a/tests/PHPStan/Analyser/nsrt/bug-15942.php b/tests/PHPStan/Analyser/nsrt/bug-15942.php new file mode 100644 index 00000000000..7c687fab944 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15942.php @@ -0,0 +1,29 @@ + $data */ +function assignsInBodyOffset(array $data): int +{ + $list = []; + foreach ($data['items'] as $item) { + $list[] = $item; + } + + assertType('mixed', $data['items']); + return count($data['items']) + count($list); +} diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 46023b80695..b9a626a425f 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -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'], [ diff --git a/turbo-ext/src/ExpressionTypeHolder.cpp b/turbo-ext/src/ExpressionTypeHolder.cpp index 707e694f6d1..89b833b6522 100644 --- a/turbo-ext/src/ExpressionTypeHolder.cpp +++ b/turbo-ext/src/ExpressionTypeHolder.cpp @@ -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 { @@ -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(execute_data, expr, type)) RETURN_THROWS(); diff --git a/turbo-ext/src/MutatingScope.cpp b/turbo-ext/src/MutatingScope.cpp index 1900500981c..04680e299cc 100644 --- a/turbo-ext/src/MutatingScope.cpp +++ b/turbo-ext/src/MutatingScope.cpp @@ -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)); } diff --git a/turbo-ext/src/generated/ExpressionTypeHolder.h b/turbo-ext/src/generated/ExpressionTypeHolder.h index b6eb9fd501b..78e7f31cef7 100644 --- a/turbo-ext/src/generated/ExpressionTypeHolder.h +++ b/turbo-ext/src/generated/ExpressionTypeHolder.h @@ -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 */ @@ -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 */ @@ -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 diff --git a/turbo-ext/src/support.cpp b/turbo-ext/src/support.cpp index 71a97cf02ec..cdc68435ce4 100644 --- a/turbo-ext/src/support.cpp +++ b/turbo-ext/src/support.cpp @@ -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); diff --git a/turbo-ext/src/support.h b/turbo-ext/src/support.h index cdc9d6356fe..0793d1ff7ec 100644 --- a/turbo-ext/src/support.h +++ b/turbo-ext/src/support.h @@ -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); diff --git a/turbo-ext/tests/smoke.php b/turbo-ext/tests/smoke.php index 1f76020192c..6278f3989d1 100644 --- a/turbo-ext/tests/smoke.php +++ b/turbo-ext/tests/smoke.php @@ -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);