Skip to content

Infer never for generic callable return types - #7117

Open
hrabosh wants to merge 2 commits into
phpstan:2.3.xfrom
hrabosh:infer-never-from-generic-callable-return
Open

hrabosh wants to merge 2 commits into
phpstan:2.3.xfrom
hrabosh:infer-never-from-generic-callable-return

Conversation

@hrabosh

@hrabosh hrabosh commented Nov 21, 2026 •

Copy link
Copy Markdown

Summary

A callback that always throws has return type never. When the expected return type is a generic object such as Opt<E>, PHPStan could not infer E and reported argument.templateType. The call then returned Opt<mixed> in the analysis.

Infer never for unresolved template arguments of the generic return object while preserving declared defaults and types inferred from callable parameters or assertions. This lets Opt<T|E> simplify to Opt<T> in the reported case. Apply the same inference to Closure, callable, and their native implementation.

Verification

  • The regression tests reproduce the incorrect type and diagnostic before the fix. With the fix, the original playground example infers Opt<int> without argument.templateType.
  • The full PHPUnit suite completed without failures in both modes. It collected 23,800 tests per run, with 226 skips without phpstan_turbo and 241 with it.
  • The original playground example produced identical analysis output with and without the extension. Native signature parity passed. The standalone native smoke test timed out locally.

CI follow-up

Removed a redundant object check identified by mutation testing and added a regression assertion for a nullable generic callback return.

The four PHAR integration failures for OpenEMR, phpstan-dba, phpstan-laravel and Shopware have identical diagnostics in the earlier CI run for #7115.

Closes phpstan/phpstan#15770

Infer the bottom type for unresolved template arguments of a
generic object when the supplied callable cannot return. Preserve
template types inferred from callable parameters and assertions.

Closes phpstan/phpstan#15770
The class reflection lookup already excludes nullable unions.
Remove the redundant trinary check and cover the nullable
callback return type in the regression fixture.
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.

Closure returning never leaves the template unresolved when the parameter is Closure(): Foo<E>

1 participant