Skip to content

Keep a merged call whose uninformative check leads back to itself - #7088

Merged
staabm merged 1 commit into
phpstan:2.3.xfrom
dmaicher:merged-call-reentry-guard
Nov 16, 2026
Merged

staabm merged 1 commit into
phpstan:2.3.xfrom
dmaicher:merged-call-reentry-guard

Conversation

@dmaicher

Copy link
Copy Markdown
Contributor

Closes phpstan/phpstan#15754

Since bcca2b0 (#6953), merging two scopes checks whether a remembered call's merged type is what the call resolves to on its own (findUninformativeMergedCalls()). Resolving the call processes its arguments again. After $s = strlen($s ?: '') ?: 0;, conditional expressions tie strlen($s ?: '') to $s, so merging the branches of the inner $s ?: '' finds the same call differing between them and resolves it again. That repeats until the memory limit is hit.

This happens both in the regular walk (when the assignment is repeated) and when a rule asks for the statement's type at execution end, as ParameterOutExecutionEndTypeRule does for the single assignment in the issue.

Now, when a check leads back to a call whose own check is still in progress, it keeps the merged entry instead, as every merge did before bcca2b0. The C++ twin uses the same static property as the guard.

Without the fix, the regression test runs out of memory. That's true without the extension, and with the extension built with only the C++ change reverted.

Verified with:

  • the full test suite without the extension, and tests/PHPStan/Analyser with the extension active
  • phpcs, and PHPStan on MutatingScope.php
  • the turbo smoke test, signature parity and turbo-ext/bin/side-by-side.php

Since bcca2b0, a merge drops a remembered call whose merged type is
what the call resolves to on its own. Resolving the call processes its
arguments again, and a ternary among them merges its own branches. When
conditional expressions tie the call's type to the ternary's condition,
as after `$s = strlen($s ?: '') ?: 0;`, that merge finds the same call
differing between the branches and resolves it again, without end, until
the memory limit is hit. A check that leads back to a call whose check
is in progress now keeps the merged entry, as every merge did before
bcca2b0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@staabm

staabm commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

//cc @SanderMuller

@SanderMuller SanderMuller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this locally against the merge base 02480d8, with and without the Turbo extension.

  • The snippet from phpstan/phpstan#15754 runs out of a 1 GB memory limit after 53 seconds on the base. On this PR it finishes in 3 seconds, without errors at level max. With the extension loaded, it also finishes.
  • The new test hangs when I take MutatingScope.php from the base (killed after 180 seconds). With the extension loaded, it also hangs when only MutatingScope.cpp and its generated header come from the base (killed after 120 seconds). So both guards are needed.
  • I tried 9 variants of the pattern. They cover count(), trim() and preg_split() in the call, a nested ?:, a narrowed count() like in bug-13750, and the assignment twice. The dumped types are the same as on the 2.3.5 PHAR, and the same as on the base where the base finishes. Only the assignment twice hangs on the base.
  • With the assignment repeated 1 to 16 times in one function, analysis takes 0.86 to 0.96 seconds on this PR. The base does not finish any of them within 30 seconds.
  • tests/PHPStan/Analyser passes, with and without the extension (3,430 tests). The Turbo smoke test, bin/side-by-side.php and the signature parity check pass on a local build.
  • Performance: I counted AnalysisBench and the MutatingScope*Bench cases (145) with Tally, base against this PR. The instruction count does not change by more than 0.084 % in any case (median 0), and memory changes by at most 0.001 %.

The two red jobs (phpstan-dba and phpstan-laravel tests) also fail on #7090 and #7093. They come from the type changes in 2.3.6: see staabm/phpstan-dba#830 and calebdw/phpstan-laravel#55.

I did not run the full make tests, make phpstan or the walk trace locally. CI covers the first two, and they are green.

@staabm
staabm merged commit 73fbad4 into phpstan:2.3.x Nov 16, 2026
938 of 941 checks passed
@staabm

staabm commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analysis never finishes on $s = strlen($s ?: "") ?: 0; (regression in 2.3.6)

4 participants