Skip to content

Fix stack overflow during subscript inference on recursive type aliases - #4635

Open
kavix wants to merge 1 commit into
facebook:mainfrom
kavix:fix-issue-4631
Open

Fix stack overflow during subscript inference on recursive type aliases#4635
kavix wants to merge 1 commit into
facebook:mainfrom
kavix:fix-issue-4631

Conversation

@kavix

@kavix kavix commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #4631.

Nested mapping-pattern inference re-entered subscript inference through recursive UntypedAlias values. Each expansion recreated the alias-containing type, causing unbounded recursion and a native stack overflow crash.

Changes

  • Updated subscript_infer_for_type_with_key_present to maintain an active SmallSet<TypeAliasData>.
  • Shared this set across recursive calls (DataFrame, Series, restricted TypeVars, and aliases).
  • When an alias is encountered again on the active expansion path, recursion terminates and returns an implicit Any.
  • Added regression test test_recursive_alias_mapping_pattern_does_not_overflow in pyrefly/lib/test/pattern_match.rs.

Test Plan

  • cargo test pattern_match (verified all 98 tests pass)
  • cargo test test_recursive_alias_mapping_pattern_does_not_overflow -- --nocapture
  • python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema

Recursive type aliases involved in nested mapping patterns (such as pattern
matching on nested dictionary structures) could trigger recursive subscript
inferences without termination, causing a stack overflow.

Track active type aliases during subscript inference using a cycle-detection
set. When an alias is encountered again on the active subscript path, terminate
recursion by returning an implicit Any type.
@meta-codesync

meta-codesync Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D117121160. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow in subscript_infer_for_type_with_key_present

1 participant