Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Type alias statements stop tracking typevar variance after two deep…

  1. 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /ruff, current file: none) bash-$

  2. GLM Assistant

    Let me start by exploring the repository structure to understand the codebase and find the relevant files related to type alias variance inference.

  3. GLM Assistant

    Let me explore the repository structure first.

  4. GLM Assistant

    This is the Ruff repository which contains the ty type checker. Let me explore the structure to find the variance inference code.

  5. GLM Assistant

    Let me search for variance-related code in the ty crates.

  6. GLM Assistant

    Now let me look at the typevar.rs file and the generics.rs file:

  7. GLM Assistant

    Let me look at the generics.rs file which handles variance inference:

  8. GLM Assistant

    Let me search for the variance_of implementation for type aliases:

  9. GLM Assistant

    Let me look for where TypeAliasType or type aliases implement VarianceInferable:

  10. GLM Assistant

    I don't see a VarianceInferable impl for TypeAliasType. Let me look at how Type implements VarianceInferable:

  11. GLM Assistant

    I found the bug. On line 6854, when a Type::TypeAlias is encountered during variance inference, it returns Bivariant instead of recursing into the alias's value type. Let me look at the TypeAliasType structure:

  12. GLM Assistant

    Now let me reproduce the issue. Let me first check how to run the ty type checker:

  13. GLM Assistant

    Let me check if there's a pre-built ty binary:

  14. GLM Assistant

    Let me look at the existing variance tests to understand the test patterns:

  15. GLM Assistant

    Let me look at the KnownInstanceType variance_of implementation:

  16. GLM Assistant

    Let me look at how variance is computed at the top level and whether there's cycle handling:

  17. GLM Assistant

    So variance is called on BoundTypeVarInstance, which calls variance_with_polarity. The variance is computed by looking at binding_type(db, definition) and then calling .with_polarity(polarity).variance_of(db, self).

    Now this is a salsa tracked function? Let me check if variance or variance_with_polarity is annotated with #[salsa::tracked].

  18. GLM Assistant

    Let me look at the NominalInstanceType::variance_of implementation:

  19. GLM Assistant

    Let me look at how variance is computed for type aliases and whether there's a salsa tracked entry point:

  20. GLM Assistant

    Let me look at how the type alias's typevar variance is used in the broader system. Let me search for where variance is checked for type aliases:

  21. GLM Assistant

    Let me look at how variance is exposed on GenericContext and used for type aliases:

  22. GLM Assistant

    Let me check if binding_type is salsa tracked and how the recursion flows:

  23. GLM Assistant

    Let me look at the PEP695TypeAliasType definition:

  24. GLM Assistant

    Let me look at the ManualPEP695TypeAliasType definition:

  25. GLM Assistant

    Let me look at the full variance test to understand the expected behavior:

  26. GLM Assistant

    Let me check how GenericAlias is defined (interned or tracked):

  27. GLM Assistant

    Let me check how BoundTypeVarInstance, TypeVarVariance, and VarianceInferable are exported:

  28. GLM Assistant

    Let me check the types module structure:

  29. GLM Assistant

    Let me see how class.rs imports VarianceInferable and related types:

  30. GLM Assistant

    Now I have a clear understanding of the issue and the fix. Let me reproduce the issue first by building and running a test. Let me create a reproduction test based on the existing test pattern:

  31. GLM Assistant

    Let me build the ty binary first to reproduce the issue:

  32. GLM Assistant

    The build succeeded. Now let me create a reproduction Python file:

  33. GLM Assistant

    I need to specify a Python version. Let me run with the appropriate flag:

  34. GLM Assistant

    Let me write a better reproduction that tests the assignability directly:

  35. GLM Assistant

    Let me use the underlying type directly since PEP 695 aliases aren't callable:

  36. GLM Assistant

    Interesting - even the first level passes. Let me check the base case with Box directly:

  37. GLM Assistant

    Let me use reveal_type to understand what types are being inferred:

  38. GLM Assistant

    Let me use types that are definitely not subtypes (str vs int):

  39. GLM Assistant

    Now I can see the error with direct Box. The int/float passed due to numeric tower. Let me test the alias cases:

  40. GLM Assistant

    Let me construct a test where alias variance actually matters - comparing two alias instances:

  41. GLM Assistant

    The assignment expands the alias. Let me look at where the alias's typevar variance is actually used without expansion:

  42. GLM Assistant

    Let me look at how Type::TypeAlias is handled in the type relation checker:

  43. GLM Assistant

    Let me search for where specialization pairs are checked for type aliases:

  44. GLM Assistant

    Let me look at where the variance is used in inference (lines 1673, 1901):

  45. GLM Assistant

    Let me look at the markdown tests related to variance to understand expected behavior:

  46. GLM Assistant

    Let me look at the disjointness check for type aliases (line 2388):

  47. GLM Assistant

    Let me look at the materialization context where specialization_variance is used:

  48. GLM Assistant

    Let me look at the structure of the tests file to understand how to create a temporary verification:

  49. GLM Assistant

    Let me temporarily add a test to confirm the bug, then revert. First let me look at the end of the type_alias_variance test to add a temporary repro:

  50. GLM Assistant

    I've confirmed the bug: RefRefInvariant (two-level nested alias) returns Bivariant instead of Invariant, while RefInvariant (one level) correctly returns Invariant. Now let me implement the fix.

    First, let me update the imports in type_alias.rs:

  51. GLM Assistant

    Now let me add the salsa-tracked value_type_variance method to PEP695TypeAliasType. Let me view the relevant section:

  52. GLM Assistant

    Now let me add the value_type_variance method to PEP695TypeAliasType after value_type:

  53. GLM Assistant

    There's a duplicate comment line. Let me fix it:

  54. GLM Assistant

    Let me fix the remaining duplicate and add a blank line:

  55. GLM Assistant

    Now let me add the same method to ManualPEP695TypeAliasType. Let me view that section:

  56. GLM Assistant

    The edit got messed up - it replaced the wrong line. Let me revert the file and redo carefully:

  57. GLM Assistant

    Let me redo the edits using a precise Python script:

  58. GLM Assistant

    Now let me add the value_type_variance method to PEP695TypeAliasType using a precise replacement:

  59. GLM Assistant

    Let me verify the edits look correct:

  60. GLM Assistant

    The type_alias.rs edits look good. Now let me update the Type::TypeAlias arm in types.rs:

  61. GLM Assistant

    Now let me update types.rs to handle Type::TypeAlias by recursing, and remove it from the Bivariant arm:

  62. GLM Assistant

    Now let me check the KnownInstanceType::TypeAliasType variance_of which uses raw_value_type. Let me examine whether it can also be a cycle concern:

  63. GLM Assistant

    Let me check how KnownInstanceType::TypeAliasType is constructed to understand if it can be specialized:

  64. GLM Assistant

    The KnownInstanceType::TypeAliasType is always unspecialized, so value_type equals raw_value_type for it. To also handle bare self-referential aliases like type A[T] = A (where raw_value_type returns the alias itself, bypassing Type::TypeAlias), let me route the entry point through the tracked value_type_variance too:

  65. GLM Assistant

    Now let me build to check for compilation errors:

  66. GLM Assistant

    It compiled. Now let me run the temporary reproduction test to verify the fix:

  67. GLM Assistant

    The one-level case regressed. Let me check what default_specialization does - value_type for the unspecialized alias might map the typevar to something else:

  68. GLM Assistant

    I see - value_type for the unspecialized entry maps the typevar to unspecialized (losing it), so the entry must use raw_value_type. Let me revert the known_instance.rs change and add a separate tracked raw_value_type_variance for the entry point:

  69. GLM Assistant

    Now let me add raw_value_type_variance to PEP695TypeAliasType (using raw_value_type, which preserves the alias's own typevar):

  70. GLM Assistant

    Now let me add the raw_value_type_variance delegate on TypeAliasType:

  71. GLM Assistant

    Now let me update the known_instance.rs entry point to use raw_value_type_variance:

  72. GLM Assistant

    Now let me build and run the temporary test:

  73. GLM Assistant

    The fix works. Now let me run the existing type_alias_variance test to ensure I didn't break anything:

  74. GLM Assistant

    The existing test passes. Now let me test edge cases - recursive, mutually recursive, bare self-reference, and reordered multi-parameter aliases. Let me update my temporary test:

  75. GLM Assistant

    All edge cases pass. Let me run the broader test suite - the mdtest variance tests and the full ty_python_semantic test suite:

  76. GLM Assistant

    All unit tests pass. Now let me run the mdtest variance tests:

  77. GLM Assistant

    Let me run the full mdtest suite to make sure nothing else broke:

  78. GLM Assistant

    All 338 mdtests pass. Let me also check the legacy variance mdtest and run the ty binary on the original issue scenario:

  79. GLM Assistant

    Now let me rebuild the ty binary and verify the original issue scenario:

  80. GLM Assistant

    These errors are due to alias expansion in the relation checker. Let me find where the alias variance actually matters behaviorally. Let me check materialization for type aliases:

  81. GLM Assistant

    Most operations expand the alias. The variance inference is the core fix (verified by unit tests). Let me run the broader test suites to ensure nothing breaks - the ty_test and ty_ide crates:

  82. GLM Assistant

    All ty_ide tests pass. Let me run the ty_test crate which has the main test framework.

  83. GLM Assistant

    Let me run the ty_project tests as well:

  84. GLM Assistant

    Let me run all generics mdtests to be thorough:

  85. GLM Assistant

    Let me look at the legacy variance mdtest to understand "legacy generic type aliases":

  86. GLM Assistant

    Let me verify how legacy TypeAlias-annotated aliases are represented in ty:

  87. GLM Assistant

    Legacy TypeAlias-annotated aliases resolve to GenericAlias (class form), which already handles nesting via GenericAlias::variance_of. The bug was specifically in Type::TypeAlias (PEP 695 type statement and types.TypeAliasType), which I've fixed.

    Now let me clean up the temporary test from tests.rs:

  88. GLM Assistant

    Let me verify the tests.rs has no changes and check the overall diff:

  89. GLM Assistant

    Let me review the full diff to ensure correctness:

  90. GLM Assistant

    The diff looks clean. Let me run a final build and the key tests to confirm everything works after cleanup:

  91. GLM Assistant

    Let me run clippy on the changed crate to ensure no lint issues:

  92. GLM Assistant

    Clippy passes. Let me run rustfmt to ensure proper formatting:

  93. GLM Assistant

    Let me apply rustfmt formatting:

  94. GLM Assistant

    Formatting is clean. Let me do a final review of the formatted type_alias.rs changes:

  95. GLM Assistant

    The diff looks clean. Let me clean up temp files and run a final comprehensive verification:

  96. GLM Assistant

    Let me do a final comprehensive test run on the affected crate to confirm the submitted state is good:

  97. GLM Assistant

    Let me re-add the comprehensive edge-case test one final time to be 100% sure the fix handles all contract cases, then remove it:

  98. GLM Assistant

    All edge cases pass, including forward-referenced alias chains (where the referenced alias is defined later). Let me remove the temporary test:

  99. GLM Assistant

    Let me do a final build to confirm the final state compiles cleanly:

  100. GLM Assistant

    The fix is complete and all tests pass. Let me verify the final diff one more time before submitting:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.