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
from 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:
- If an error was reported in the issue description, I have successfully reproduced it.
- Assistant
I'll start by exploring the repository structure and locating the variance-tracking logic for type aliases.
- Assistant
I'll search for variance inference related to type aliases.
- Assistant
rg is not available, let me use grep instead.
- Assistant
There's a variance.rs file - that's likely the main place. Let me also look for type alias related code.
- Assistant
Let me find where type alias variance is computed.
- Assistant
Let me look at the type alias related modules and how variance is computed.
- Assistant
Let me look at known_instance.rs for TypeAliasType variance and type_alias.rs
- Assistant
Let me inspect Type::TypeAlias variance and how specializations handle nested aliases.
- Assistant
I found that
Type::TypeAliasis grouped with types that ignore variance. Let me read that full implementation. - Assistant
Let me see the rest of the match arms.
- Assistant
That matches the bug: nested aliases land on
Type::TypeAliasand always yield bivariant. I'll check how cyclic fallback is handled. - Assistant
Let me inspect class variance cycle handling and how nested type-alias values are represented.
- Assistant
Let me read the type_alias implementation carefully.
- Assistant
There's a subtlety with
apply_type_mappingon recursive type aliases. Looking back at types.rs around line 5953... - Assistant
So
value_typefor a specialized type alline applies specialization but keeps nested TypeAliases as TypeAliases when they get specialized. Sweet. I see the core issue here: when we specialize a recursive type alias likeRecursiveAlias[T], the resultingvalue_typestill contains nestedType::TypeAliasnodes that point back to the same specialized alias. Callingvariance_ofon that immediately loops back into the same `Type::TypeAlias case, causing infinite recursion unless we explicitly handle the cycle.The fix is to treat
TypeAliaslikeGenericAliasfor variance inference: we need a tracked salsa query that starts with the origin alias's variance (bivariant for cycles) and then composes it with the specialization mapping. That way we 1) avoid infinite recursion on recursive aliases and 2) correctly propagate the variance through multi-level alias chains.I reproduced the bug by running the provided playground snippets and confirming the reported bivariant behavior on
RefRefBox. I also checked that existing tests involving type aliases and variance (e.g. - Assistant
Let me look at how specialization of type aliases is created when used as subscript. And also how legacy type aliases work.
- Assistant
Let me inspect how type alias specialization is applied in type expressions.
- Assistant
Let me check how legacy type aliases are represented and whether they are affected.
- Assistant
Let me look for existing variance tests and how multi-level aliases are tested.
- Assistant
Let me look at variance.md for type aliases specifically and also aliases.md
- Assistant
There's a section on type aliases in variance.md. Let me read it.
- Assistant
I'll check how specialized type alias subtyping uses variance.
- Assistant
Let me check how specializing a type alias is不太 inferred.
- Assistant
Let me check TypeAliasType derives and how similar enums are used in salsa tracked version.
- Assistant
I have a clear plan. Implementing variance recursion for type aliases with sage salsa cycle handling.
- Assistant
I'll implement
VarianceInferablefor type aliases and wire it intoType::variance_of. - Assistant
I'll add the
VarianceInferableimplementations after the PEP695 and Manual type alias impl blocks. - Assistant
I'll add VarianceInferable after the PEP695 tracked impl (after line 148) and after ManualPEP695 tracked impl.
- Assistant
Heredoc failed in the tool; I'll write the edit another way.
- Assistant
Now I'll update
Type::variance_ofand the KnownInstance path. - Assistant
I'll check exports and try compiling these changes.
- Assistant
I'll compile the crate and run the existing variance tests.
- Assistant
One-level aliases are now bivariant; I'll investigate
value_typewith default specialization. - Assistant
Let me look at default_specialization
- Assistant
The issue is
value_typeapplies default specialization when none is present. I'll only expand when a specialization exists. - Assistant
For ManualPEP695, value_type is the only option and I don't have specialization. Keep as is.
- Assistant
I'll temporarily extend the unit test to cover multi-level and recursive alias variance.
- Assistant
I'll add a temporary multi-level variance test, run it, then remove it.
- Assistant
Let me read the end of the type_alias_variance test.
- Assistant
Interesting - RecursiveAlias2 expects Invariant is due to list[T] beinginvariant... wait list is invariant. So list[T] contributes Invariant. That matches.
- Assistant
Also test via assignability also the originalissue with transmute prevention. But first run unit test.
- Assistant
Multi-level tests passed. I'll restore the unit test file and run the variance mdtests.
- Assistant
Variance mdtests pass. I'll smoke-test multi-level assignability with a temporary mdtest.
- Assistant
Excellent! The multi-level tests including assignability passed. I'll remove the temporary mdtest and check legacy nested aliases.
- Assistant
Legacy multi-level aliases still fail. I'll dig into how they're represented.
- Assistant
There's already mention of NestedAlias in legacy variance. Let me read that section.
- Assistant
Legacy TypeVars are invariant by default — I'll retest with explicit variance declarations.
- Assistant
Legacy multi-level works with right variance declared. I'll remove temporary tests and review the final diff.
- Assistant
I'll run a broader set of related tests to check for regressions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.