diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index febc36f6690..52398d7320e 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4731,7 +4731,7 @@ private function mergeSameGuardConditionalExpressions( $ourGuards, new ExpressionTypeHolder( $ourTypeHolder->getExpr(), - TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType()), + TypeCombinator::widen(TypeCombinator::union($ourTypeHolder->getType(), $theirTypeHolder->getType())), $ourTypeHolder->getCertainty(), ), ); diff --git a/tests/bench/data/bug-15862.php b/tests/bench/data/bug-15862.php new file mode 100644 index 00000000000..7d6a99922a3 --- /dev/null +++ b/tests/bench/data/bug-15862.php @@ -0,0 +1,97 @@ + $rows */ +function f(array $rows): void { + $r = [ + 'T_7' => 0, + 'T_30' => 0, + 'T_90' => 0, + 'C0_7' => 0, + 'C0_30' => 0, + 'C0_90' => 0, + 'C1_7' => 0, + 'C1_30' => 0, + 'C1_90' => 0, + 'C2_7' => 0, + 'C2_30' => 0, + 'C2_90' => 0, + 'C3_7' => 0, + 'C3_30' => 0, + 'C3_90' => 0, + ]; + foreach ($rows as $row) { + $a = $row['a'] ?? false; + $b = $row['b'] ?? false; + $c = $row['c'] ?? false; + $s = $row['s'] ?? false; + if ($a) { + $r['T_7']++; + $r['T_30']++; + $r['T_90']++; + } elseif ($b) { + $r['T_30']++; + $r['T_90']++; + } elseif ($c) { + $r['T_90']++; + } + switch ($row['t']) { + case 'c0': + if ($a) { + $r['C0_7']++; + $r['C0_30']++; + $r['C0_90']++; + } elseif ($b) { + $r['C0_30']++; + $r['C0_90']++; + } elseif ($c) { + $r['C0_90']++; + } + break; + case 'c1': + if ($a) { + $r['C1_7']++; + $r['C1_30']++; + $r['C1_90']++; + } elseif ($b) { + $r['C1_30']++; + $r['C1_90']++; + } elseif ($c) { + $r['C1_90']++; + } + break; + case 'c2': + if ($a) { + $r['C2_7']++; + $r['C2_30']++; + $r['C2_90']++; + } elseif ($b) { + $r['C2_30']++; + $r['C2_90']++; + } elseif ($c) { + $r['C2_90']++; + } + break; + case 'c3': + if ($a) { + $r['C3_7']++; + $r['C3_30']++; + $r['C3_90']++; + } elseif ($b) { + $r['C3_30']++; + $r['C3_90']++; + } elseif ($c) { + $r['C3_90']++; + } + break; + } + } + \PHPStan\dumpType($r); +} diff --git a/turbo-ext/src/MutatingScope.cpp b/turbo-ext/src/MutatingScope.cpp index 8fa592728ec..c580841b247 100644 --- a/turbo-ext/src/MutatingScope.cpp +++ b/turbo-ext/src/MutatingScope.cpp @@ -8961,7 +8961,10 @@ class MutatingScope zv::Val theirType = holderType(theirTypeHolder.ref()); zv::Val ourExpr = holderExpr(ourTypeHolder.ref()); if (UNEXPECTED(ourType.isUndef() || theirType.isUndef() || ourExpr.isUndef())) return zv::Val(); - zv::Val united = unionTypes(ourType.raw(), theirType.raw()); + /* TypeCombinator::widen(TypeCombinator::union(...)): a join of branches, widened like the joins of scopes */ + zv::Val joined = unionTypes(ourType.raw(), theirType.raw()); + if (UNEXPECTED(joined.isUndef())) return zv::Val(); + zv::Val united = pt_type_combinator_call(PT_LC("widen"), 1, joined.raw()); if (UNEXPECTED(united.isUndef())) return zv::Val(); zval typeHolder; pt_holder_create(&typeHolder, ourExpr.raw(), united.raw(), ourCertainty);