Repository navigation
Keep unresolved template arguments of different sites apart, solve covariant ones like Hack - #6650
Merged
Merged
Conversation
…variant ones like Hack A template slot fed by several unresolved sites (zip(of(1), of(2))) made a union of their markers. Unions treated a marker as its delegate, so generalizing unresolved(2)|unresolved(3) collapsed it into one marker and tripped the "never itself unresolved" invariant; marker|int silently lost the marker. - TypeCombinator::union() keeps template-argument markers opaque: only the same site's marker merges, into the wider inferred type. - A template occurring only covariantly in a call's return type, or a covariant class template of `new`, gets no marker when something was inferred: it is the union of what it was inferred from (Hack's eager solution of covariant-only type variables). A send of the result splits across the markers in that union as upper bounds. - An upper bound of a site also bounds the sites its inferred argument was made of (collect(of(1), of(2)) returning Coll<U>). - The covariant fallback of a site with nothing inferred prefers concrete upper bounds over another site's variable, fixing Box<mixed> false positives for objects passed through generic functions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0159PawMjk3w72qmhaGSqSH9
ondrejmirtes
force-pushed
the
issue-15331
branch
from
October 2, 2026 07:35
d5eda42 to
ca8452c
Compare
Contributor
|
this PR was merged with "Turbo Lint" errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A template slot fed by several unresolved sites —
Box::of(1)->zip(Box::of(2), Box::of(3))with@return self<T|U>— made a union of their markers. Unions treated a marker as its delegate, so generalizingunresolved(2)|unresolved(3)(for the display name of an impure method call) collapsed it into one marker and trippedThe initial type of an unresolved template argument is never itself unresolved.;marker|intsilently lost the marker too.I compared what other checkers do with one type variable fed by several unresolved ones (Hack, Kotlin, Java, Scala 3, Rust, OCaml, Haskell, Swift, TypeScript, Flow, mypy, pyright — all run). Hack, the closest precedent to the two-pass design, keeps distinct type variables apart in unions, eagerly solves a covariant-only variable to the union of its lower bounds, and splits
(#1 | #2) <: intinto upper bounds on each member. This PR does the same:TypeCombinator::union()keeps template-argument markers opaque: only the same site's marker merges, into the wider inferred type (closure-signature markers keep their old behaviour).new, gets no marker when something was inferred — the result carries the union of what it was inferred from (Box<m1|m2|m3>). A send of the result puts the declared argument as an upper bound on every marker in it.collect(Box<U>, Box<U>): Coll<U>sent toColl<int>).Box<mixed>false positives for objects passed through generic functions (joinBoxes(new Box(), new Box())sent toBox<int>:expects Box<int>, Box<mixed> given).TemplateArgumentObserver::replaceInferableTemplates()relied on the old absorption to turnT|nullinto the marker; it now absorbs the covered siblings itself.Turbo mirrors are ported (with differential coverage in
type-family.php); the full suite passes with and without the extension, and self-analysis output is identical either way.Tests:
AnalyserIntegrationTest::testBug15331(the issue's snippet) andnsrt/template-argument-multiple-sites.phpcovering covariant/invariant/contravariant combinations, nothing-inferred sites, loop generalization and covariantnew.Closes phpstan/phpstan#15331
🤖 Generated with Claude Code
https://claude.ai/code/session_0159PawMjk3w72qmhaGSqSH9