ISSUE DESCRIPTION Type alias statements stop tracking typevar variance after two deep…
- User
ISSUE DESCRIPTION
Type alias statements stop tracking typevar variance after two deep
Summary
Ran into this while making my umpteenth generic parser generator soup and messed up a parser generator transformer. https://play.ty.dev/d0fab0ef-bf5d-496f-b305-046e604cd702
pyfrom dataclasses import dataclass @dataclass class Box[T]: # T is invariant item: T type RefBox[T] = Box[T] # T is invariant type RefRefBox[T] = RefBox[T] # T is bivariant???When combined with #3517, you can make a type safe transmute (this happened in my actual code): https://play.ty.dev/eff23d78-ef4e-4bda-8aa7-b9848e527cb3
Version
ty 0.0.35 (bc12d1c 2026-05-10) playground 7efc9a615
Evaluation contract
Variance inference for both PEP 695 and legacy generic type aliases must recurse through the alias value rather than treating every alias as bivariant. Preserve covariant, contravariant, invariant, and bivariant results through multi-level and reordered multi-parameter alias chains. Recursive or mutually recursive alias cycles must terminate safely using the existing bivariant cycle fallback.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- Assistant
I'll start by exploring the repository to understand its structure and locate the relevant code for type alias variance inference. Let's first see what we have.
- Assistant
Let me explore the repository structure more to understand what we're working with. This seems to be the
tytype checker repository. - Assistant
This is the ruff/ty repository. The issue is about type alias variance inference. Let me find the relevant crates for the type checker.
- Assistant
Hmm, no match with grep. Let me list all crates.
- Assistant
Let me look for variance-related code in the
ty_python_semanticcrate, which is likely where the variance inference logic lives. - Assistant
Let me look at the variance.rs file first.
- Assistant
Now let me search for the variance inference for type aliases specifically.
- Assistant
Let me look at where type alias variance inference is implemented. The issue is about type alias statements like
type RefBox[T] = Box[T]not properly tracking variance ofTthrough the alias chain.Let me search for where variance is computed for type aliases.
- Assistant
Let me look at the TypeVar variance implementation and how it's computed.
- Assistant
Let me search for where type alias variance inference is done.
- Assistant
Let me search for where type aliases get their variance.
- Assistant
Let me read the typevar.rs file to understand the variance inference logic.
- Assistant
Now I need to understand how variance inference works for type aliases. The key function is
variance_with_polarityinBoundTypeVarInstance. Let me look atbinding_typeand how it determines variance. - Assistant
Let me understand the issue more. The problem is about type alias variance tracking. Let me look at how
binding_typeworks for type aliases, and how variance inference works for type alias definitions. - Assistant
Let me look at the
binding_typefunction to understand what it returns for type aliases. - Assistant
Let me understand the variance inference for type aliases. The key function is
variance_with_polarityinBoundTypeVarInstance. Let me look at the VarianceInferable implementations: - Assistant
Now let me look at the
VarianceInferableimplementation forTypeitself, since that's what will be used for type aliases. - Assistant
I need to look for the VarianceInferable implementation for the
Typeenum. - Assistant
The VarianceInferable trait is not implemented for
Typedirectly. Let me look at how thevariance_offunction works when called with aTypetype. - Assistant
Let me look at the GenericAlias variance_of implementation more closely, as that's likely where the issue is.
- Assistant
Now let me look at how the
Typeenum implementsvariance_of: - Assistant
Let me see the full implementation of
VarianceInferable for Type: - Assistant
Let me see what happens for TypeVar types and type aliases:
- Assistant
Now I can see the issue! In the
VarianceInferableimplementation forType, theType::TypeAliasvariant is treated asTypeVarVariance::Bivariant. This is the default case that happens when we encounter a type alias. But a type alias should propagate the variance from its target type, not just returnBivariant.Let me understand what
Type::TypeAliascontains and how to get its target type: - Assistant
Let me look at how TypeAlias type works
- Assistant
Good. Let me check the
TypeAliasTypeto see how to get the value type: - Assistant
Let me read the TypeAliasType implementation to understand its structure and methods.
- Assistant
Now I understand the issue. When
Type::TypeAlias(TypeAliasType)is encountered in thevariance_ofimplementation, it just returnsTypeVarVariance::Bivariantinstead of recursively computing the variance through the type alias's value type.Let me also check how legacy type aliases are handled:
- Assistant
Let me look at the test file for variance to understand the test cases:
- Assistant
Let me look at the full test to understand the test cases:
- Assistant
Now let me look at the
KnownInstanceType::variance_ofimplementation to see how it handles type aliases: - Assistant
Now I understand the issue! In the
Type::variance_ofimplementation,Type::TypeAlias(_) => TypeVarVariance::Bivariantis the problem. When a type alias contains another type alias (e.g.,type RefRefBox[T] = RefBox[T]), the innerType::TypeAliasdoesn't recurse through to compute the actual variance — it just returnsBivariant.Let me create a test to reproduce the issue first:
- Assistant
Let me understand how type alias subscripting works in the type system:
- Assistant
Let me search for where type alias subscripting is handled:
- Assistant
Let me look at how
Type::TypeAliasis represented vsKnownInstanceType::TypeAliasType: - Assistant
Let me look at how type alias subscripting works:
- Assistant
Let me trace through how
RefBox[T]is represented in the type system: - Assistant
Let me understand the issue better. Let me trace through what happens for the given example:
- Assistant
Now I understand the issue. Let me look at how variance inference flows when we have
type RefRefBox[T] = RefBox[T]: - Assistant
Let me look at the variance implementation for
Typemore carefully: - Assistant
Now I need to understand the flow. Let me look at how legacy type aliases handle variance:
- Assistant
Let me look at how subscripting works for type aliases:
- Assistant
- Assistant
Let me focus on how PEP 695 type alias subscripting works:
- Assistant
Let me look at how TypeAliasType handles subscripting:
- Assistant
Let me look at how
KnownInstanceType::TypeAliasTypehandles subscripting: - Assistant
Let me look at the full subscript handling for PEP695 type aliases:
- Assistant
Let me look at how PEP 695 type alias specialization happens in the subscript code:
- Assistant
Let me look at the full subscript handling code:
- Assistant
Let me search for where the generic PEP695 type alias subscripting happens:
- Assistant
Let me look at the subscript handling for PEP695 type aliases:
- Assistant
Let me now understand the full picture:
The issue is that
Type::TypeAlias(_) => TypeVarVariance::Bivariantin theVarianceInferableimpl forType.The question is: what type does
RefBox[T]evaluate to, when we're inside the RHS oftype RefRefBox[T] = RefBox[T]?Since
RefBoxis a PEP 695 type alias, when subscripted with[T], it should produceType::TypeAlias(TypeAliasType::PEP695(refbox_specialized_with_T)).Wait, but actually, looking at the subscript handling, when we subscript a
KnownInstanceType::TypeAliasType, it goes throughinfer_explicit_type_alias_type_specialization. Let me look at what that returns. - Assistant
Let me look at how
Type::TypeAliasis created: - Assistant
Let me search for where
Type::TypeAliasis actually constructed: - Assistant
Let me search for where
Type::TypeAliasis constructed: - Assistant
Let me look at how the KnownInstanceType is converted to a Type:
- Assistant
Let me now look at how the
KnownInstanceType::TypeAliasTypevariance relates toType::TypeAlias: - Assistant
Let me understand the flow better. Let me look at what
raw_value_typeactually returns for the RHS oftype RefRefBox[T] = RefBox[T]: - Assistant
Let me now look at what exactly
RefBox[T]evaluates to when it's in the RHS of a type alias. Let me look at how the subscript is handled in type expressions: - Assistant
Let me look at the key section around line 1469-1514:
- Assistant
Now I need to understand when
Type::TypeAliasis used. Let me check howRefBox[T]is represented in the RHS oftype RefRefBox[T] = RefBox[T]: - Assistant
So
RefBox[T]in a type expression gets specialized and then.in_type_expression()is called. Let me check whatin_type_expressiondoes with aType::KnownInstance(KnownInstanceType::TypeAliasType(...)). - Assistant
Let me check what
in_type_expressiondoes withKnownInstanceType::TypeAliasType: - Assistant
Now I understand the issue. Let me trace through the computation:
-
type RefBox[T] = Box[T]- when computing variance of T in RefBox:KnownInstanceType::TypeAliasType(refbox).variance_ofcallsraw_value_type()=Box[T], thenBox[T].variance_of(db, T)= Invariant ✓ -
type RefRefBox[T] = RefBox[T]- when computing variance of T in RefRefBox:KnownInstanceType::TypeAliasType(refrefbox).variance_ofcallsraw_value_type()=Type::TypeAlias(refbox_specialized), thenType::TypeAlias(_) => Bivariant✗
The bug is in
Type::variance_ofwhereType::TypeAlias(_)returnsBivariantinstead of recursing through the alias's value type. Let me also look atKnownInstanceType::TypeAliasTypeto understand the full picture: -
- Assistant
Now let me look at the existing test for type alias variance to understand the test setup:
- Assistant
Now let me implement the fix. The key issue is that
Type::TypeAlias(_)inType::variance_ofreturnsBivariantinstead of recursing through the alias value. I need to:- Create a salsa-tracked helper function for computing variance through type aliases (with cycle detection)
- Replace the
Type::TypeAlias(_) => Bivariantwith the proper call
Let me first verify this is a Rust project with cargo:
- Assistant
Let me check if there's a pre-compiled test binary:
- Assistant
Good, the code compiles. Now let me implement the fix. Let me first look at the relevant file structure:
- Assistant
Now let me implement the fix. The plan is:
- Add a
VarianceInferableimplementation forTypeAliasTypethat properly recurses through the value type with cycle detection - Update the
Type::TypeAliasvariant in theType::variance_ofmatch to use it instead of returningBivariant
Let me first check if
TypeAliasTypealready implementsVarianceInferable: - Add a
- Assistant
Now let me look at the
KnownInstanceTypevariance implementation: - Assistant
Let me now understand how type mappings are applied to
Type::TypeAlias: - Assistant
Let me look at the broader context of how type mappings are applied to
Type::TypeAlias: - Assistant
Now I understand the issue. The
Type::TypeAlias(_)variant inType::variance_ofreturnsTypeVarVariance::Bivariantinstead of recursing through the alias's value type. Let me implement the fix.The approach is:
- Add a salsa-tracked function for computing type alias variance with cycle detection
- Update
Type::TypeAliasinType::variance_ofto use it
Let me first check if there are existing standalone tracked functions I can reference:
- Assistant
Now I understand the issue fully. Let me implement the fix. The key insight is:
Type::TypeAlias(_)inType::variance_ofreturnsBivariantinstead of recursing through the alias value- For multi-level aliases (e.g.,
type RefRefBox[T] = RefBox[T]), the RHS contains aType::TypeAliasthat should be expanded - We need cycle detection for recursive type aliases
Let me create a salsa-tracked helper function for computing type alias variance with cycle detection, then update
Type::variance_of: - Assistant
Let me look at how
definition_expression_typeworks to understand the context: - Assistant
Let me now look at how
definition_expression_typeis defined: - Assistant
Now I have a complete understanding. Let me implement the fix. I need to:
- Add a salsa-tracked function for computing type alias variance with cycle detection
- Update
Type::TypeAliasinType::variance_ofto use it
Let me first look at the imports in
type_alias.rsto understand what I need to add: - Assistant
Now let me implement the fix. First, I'll add a salsa-tracked function for computing type alias variance with cycle detection in
type_alias.rs: - Assistant
Now let me implement the fix. I'll add a salsa-tracked function for computing type alias variance with cycle detection:
- Assistant
Let me implement the fix step by step. First, add the tracked function to
type_alias.rs: - Assistant
Now I have a clear understanding of the issue and the fix needed. Let me implement it:
- The
Type::TypeAlias(_)variant inType::variance_ofreturnsBivariantinstead of recursing through the alias value. - For non-recursive cases (like
RefBox[T]), we need to expand throughvalue_type. - For recursive cases, we need salsa cycle detection to prevent infinite loops.
I'll add a salsa-tracked function for type alias variance and update the match:
- The
- Assistant
Now I have a clear understanding. The issue is that
Type::TypeAlias(_)inType::variance_ofreturnsBivariantinstead of recursing through the alias's value type. Let me implement the fix.The fix requires:
- A salsa-tracked function for computing variance through type aliases (with cycle detection)
- Updating the
Type::TypeAliasmatch arm to use it
Let me first add the tracked function:
- Assistant
Let me first check what imports are needed:
- Assistant
Now let me implement the fix. First, I'll add a salsa-tracked function for type alias variance in
type_alias.rs: - Assistant
Now let me implement the fix. First, add the imports and tracked function to
type_alias.rs: - Assistant
I see there's a duplicate import. Let me fix that:
- Assistant
Now I need to add a
VarianceInferableimplementation forTypeAliasTypewith salsa cycle detection. Let me find a good place to add it: - Assistant
Now I have a clear understanding of the issue and the fix needed. Let me implement it:
- Add a salsa-tracked function for computing variance through a type alias with cycle detection
- Update
Type::TypeAliasinType::variance_ofto use it
Let me add the tracked function after the
TypeAliasTypeimpl block: - Assistant
I see there's a duplicate comment line. Let me fix that:
- Assistant
Now let me update
Type::TypeAliasinType::variance_ofto use the new tracked function: - Assistant
Now let me update the
Type::TypeAliasmatch arm inType::variance_of: - Assistant
Now let me check if the
type_aliasmodule is properly referenced: - Assistant
Good, the
type_aliasmodule is already declared. Now let me verify the code compiles: - Assistant
The code compiles. Now let me run the existing tests to make sure nothing is broken:
- Assistant
The existing test passes. Let me also run the broader test suite to check for regressions:
- Assistant
All tests pass. Now let me create a temporary test script to verify the fix works for the multi-level alias case described in the issue:
- Assistant
Let me add a temporary test case to verify the fix works:
- Assistant
Let me now verify the fix works by running the broader test suite to check for any regressions:
- Assistant
All tests pass. Let me also check if there are mdtest files related to variance or type aliases:
- Assistant
Let me now verify the overall changes I made:
- Assistant
The diff looks clean and focused. Let me run a broader set of tests to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.