Skip to content

Merge multiple possible legacy type parameters in same scope#4134

Open
defr0std wants to merge 2 commits into
facebook:mainfrom
defr0std:main
Open

Merge multiple possible legacy type parameters in same scope#4134
defr0std wants to merge 2 commits into
facebook:mainfrom
defr0std:main

Conversation

@defr0std

Copy link
Copy Markdown

Summary

This fix resolves a bug where Pyrefly would create separate narrows for modules containing legacy type variables instead of merging them, causing legacy type variables to be lost or shadowed in scope. This behavior causes incorrect type checking failures when multiple TypeVars are imported from a different module.

Previously, in scopes referencing multiple potential legacy type variables, one possible-legacy-tparam binding would overwrite/shadow the others, leading to false-positive "not in scope" errors.

To fix this, we update Binding::PossibleLegacyTParam to hold a collection (ThinVec) of key indices instead of a single key index, and modify Key::PossibleLegacyTParam to carry both the original range and the scope range in which it occurs.

By incorporating the scope_range into the key, distinct possible legacy type parameters in different scopes remain separated, while those within the same scope are merged under the same key. When constructing bindings, subsequent potential legacy type variables in the same scope are merged into the existing Binding::PossibleLegacyTParam instead of overwriting it. During type solving, the solver iterates over all merged keys to apply narrows for all of them to the module info.

Test Plan

Ran project test cases, fixing a known issue captured in the tests. Also ran the modified type checker on our project where we currently experience these type check failuers.

defr0std added 2 commits July 13, 2026 14:49
This fix resolves a bug where Pyrefly would create separate narrows for modules containing legacy type variables instead of merging them, causing legacy type variables to be lost or shadowed in scope.
This behavior causes incorrect type checking failures when multiple TypeVars are imported from a different module.

Previously, in scopes referencing multiple potential legacy type variables, one possible-legacy-tparam binding would overwrite/shadow the others, leading to false-positive "not in scope" errors.

To fix this, we update `Binding::PossibleLegacyTParam` to hold a collection (`ThinVec`) of key indices instead of a single key index, and modify `Key::PossibleLegacyTParam` to carry both the original range and the scope range in which it occurs.

By incorporating the `scope_range` into the key, distinct possible legacy type parameters in different scopes remain separated, while those within the same scope are merged under the same key. When constructing bindings, subsequent potential legacy type variables in the same scope are merged into the existing `Binding::PossibleLegacyTParam` instead of overwriting it. During type solving, the solver iterates over all merged keys to apply narrows for all of them to the module info.
@meta-cla

meta-cla Bot commented Jul 13, 2026

Copy link
Copy Markdown

Hi @defr0std!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jul 13, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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.

1 participant