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
Now let's find where variance_of is computed for types, particularly for TypeAlias types.
Assistant
Let's look at known_instance.rs around line 202, and also generics.rs which computes variance for generic classes (used by class typevars). Also check typevar.rs:832 for variance computation of the type alias's own typevars.
Assistant
Good, this confirms the pattern (3-arg fn → 4 underscores in cycle_initial). Now let's implement similarly for PEP695TypeAliasType. Need
#[salsa::tracked]impl block wrapper too (check GenericAlias uses#[salsa::tracked] impl<'db> VarianceInferable<'db> for GenericAlias<'db>).Assistant
Good. Now let's implement the change in type_alias.rs.
Assistant
Now let's update imports and add the method.
Assistant
Good, it's a direct child. Now let's add the variance_of method.
Assistant
Now let's implement
VarianceInferableforTypeAliasTypeenum (dispatch to PEP695's variance_of, and ManualPEP695 fallback to value_type since it has no generic_context).Assistant
Now let's add the
variance_ofdispatch method within this impl block, and implementVarianceInferabletrait forTypeAliasType.Assistant
Now update the
Type::TypeAliasarm in types.rs to recurse properly instead of returning Bivariant.Assistant
Now let's build.
Assistant
Compiles cleanly. Now let's run the existing variance test and write a reproduce script for the issue.
Assistant
Good. Now let's write a proper reproduction test for the actual issue (two-level type alias with dataclass) using ty CLI or mdtest, to verify the invariance is now correctly propagated.
Assistant
This confirms the fix works. Let's double check against the original (unfixed) code to make sure this test actually fails without the fix (sanity check), then also test with three levels deep and reordered params.
Assistant
Good, changes are back. Let's verify build freshness by touching source and forcing rebuild for the "before" test properly.
Assistant
This (fixed) version correctly reports error. Let's check the original code fails to detect this (i.e., no error).
Assistant
New test passes with fix. Now confirm it fails without the fix (stash core changes but keep the test file temp modifications).
Assistant
Confirmed: the bug reproduces (Bivariant instead of Covariant for 2-level alias) without the fix, and the fix resolves it. Now let's restore tests.rs to original (revert my temporary test additions since I shouldn't add new tests to the repo).
Assistant
Good, tests.rs restored to original. Now let's run the full existing test suite for this crate (or at least the types module and relevant mdtest) to check nothing broke.
Assistant
All 338 mdtests pass along with all unit tests. Let's check the unit test count too (top portion).
Assistant
All pass. Now let's test legacy generic type aliases (e.g.
RefBox = Box[T]withouttypestatement) since the issue mentions legacy aliases too.Assistant
Reordered multi-parameter aliases work correctly (raises the expected error and no unexpected ones, ignoring the unrelated empty-body warnings). Let's fix those trivial issues and re-run cleanly, and also test mutual recursion.
Assistant
Let's run rustfmt to fix formatting (line length etc.) and re-check.
Assistant
Let's also run the full mdtest suite once more to be sure formatting didn't break anything.
Assistant
All test suites pass. Let's view the final diff once more for review.
Assistant
This looks clean and complete. Let's finally reproduce the exact original issue scenario one more time to be fully sure.
Assistant
The fix correctly resolves the exact scenario from the issue.
Assistant
Only the intended source files are modified (tests.rs was correctly reverted). Now let's go through the checklist.
Checklist review:
- Reproduced the reported error: Yes — I reproduced it both via a direct unit-test style check (
KnownInstanceType::TypeAliasType(...).variance_of(...)returningBivariantinstead ofCovariantfor a 2-level alias) and via a realty checkscenario mirroring the dataclass example from the issue, confirming thatRefRefBox[int]was wrongly assignable to and vice versa before the fix.
- Reproduced the reported error: Yes — I reproduced it both via a direct unit-test style check (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.