Skip to content

[ty] Optimize materialized recursive protocol comparisons - #28079

Open
carljm wants to merge 2 commits into
mainfrom
cjm/ty-4269-materialized-protocols
Open

[ty] Optimize materialized recursive protocol comparisons#28079
carljm wants to merge 2 commits into
mainfrom
cjm/ty-4269-materialized-protocols

Conversation

@carljm

@carljm carljm commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Comparing explicit receiver types on overloaded recursive protocol methods can repeatedly expand materialized protocol specializations until ty overflows its stack. This extends the non-recursive-member shortcut to eligible materialized assignability comparisons, avoiding that expansion while preserving inference constraints.

For two specializations of the same protocol, the shortcut checks the non-recursive target requirements against the complete source interface. It skips recursive requirements only when those constraints mention every type variable in both specializations, including through aliases, and imply the nominal type-argument relation. Opposite top-to-bottom materializations and comparisons that cannot establish this proof retain the full structural path.

On main, the added benchmark overflows the stack. With this change it completes in about 10 ms in a profiling build.

Part of astral-sh/ty#4269.

Test plan

  • Overloaded recursive protocol methods with tuple-specialized receivers and a gradual fallback terminate and preserve the callback's return type.
  • Generic inference retains evidence supplied only by recursive members for both top and bottom materializations, including aliased parameters and variables on the source side of contravariant comparisons.
  • Source members that contain the protocol remain available when the corresponding target requirement is non-recursive.
  • Assignability and constraint inference reject top-to-bottom comparisons when a fixed Any in a recursive method becomes an incompatible return requirement.
  • An added benchmark checks the expanding recursive-overload case.

@carljm carljm added the ty Multi-file analysis & type inference label Aug 26, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.79%. The percentage of expected errors that received a diagnostic held steady at 94.33%. The number of fully passing files held steady at 112/136.

@astral-sh-bot

astral-sh-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@carljm
carljm marked this pull request as ready for review August 26, 2026 18:28
@carljm
carljm requested a review from a team as a code owner August 26, 2026 18:28
@astral-sh-bot
astral-sh-bot Bot requested a review from dcreager August 26, 2026 18:28
@carljm
carljm requested review from ibraheemdev and removed request for dcreager August 26, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant