ruff/crates
Douglas Creager f67236b932
[ty] Better handling of "derived information" in constraint sets (#21463)
This saga began with a regression in how we handle constraint sets where
a typevar is constrained by another typevar, which #21068 first added
support for:

```py
def mutually_constrained[T, U]():
    # If [T = U ∧ U ≤ int], then [T ≤ int] must be true as well.
    given_int = ConstraintSet.range(U, T, U) & ConstraintSet.range(Never, U, int)
    static_assert(given_int.implies_subtype_of(T, int))
```

While working on #21414, I saw a regression in this test, which was
strange, since that PR has nothing to do with this logic! The issue is
that something in that PR made us instantiate the typevars `T` and `U`
in a different order, giving them differently ordered salsa IDs. And
importantly, we use these salsa IDs to define the variable ordering that
is used in our constraint set BDDs. This showed that our "mutually
constrained" logic only worked for one of the two possible orderings.
(We can — and now do — test this in a brute-force way by copy/pasting
the test with both typevar orderings.)

The underlying bug was in our `ConstraintSet::simplify_and_domain`
method. It would correctly detect `(U ≤ T ≤ U) ∧ (U ≤ int)`, because
those two constraints affect different typevars, and from that, infer `T
≤ int`. But it wouldn't detect the equivalent pattern in `(T ≤ U ≤ T) ∧
(U ≤ int)`, since those constraints affect the same typevar. At first I
tried adding that as yet more pattern-match logic in the ever-growing
`simplify_and_domain` method. But doing so caused other tests to start
failing.

At that point, I realized that `simplify_and_domain` had gotten to the
point where it was trying to do too much, and for conflicting consumers.
It was first written as part of our display logic, where the goal is to
remove redundant information from a BDD to make its string rendering
simpler. But we also started using it to add "derived facts" to a BDD. A
derived fact is a constraint that doesn't appear in the BDD directly,
but which we can still infer to be true. Our failing test relies on
derived facts — being able to infer that `T ≤ int` even though that
particular constraint doesn't appear in the original BDD. Before,
`simplify_and_domain` would trace through all of the constraints in a
BDD, figure out the full set of derived facts, and _add those derived
facts_ to the BDD structure. This is brittle, because those derived
facts are not universally true! In our example, `T ≤ int` only holds
along the BDD paths where both `T = U` and `U ≤ int`. Other paths will
test the negations of those constraints, and on those, we _shouldn't_
infer `T ≤ int`. In theory it's possible (and we were trying) to use BDD
operators to express that dependency...but that runs afoul of how we
were simultaneously trying to _remove_ information to make our displays
simpler.

So, I ripped off the band-aid. `simplify_and_domain` is now _only_ used
for display purposes. I have not touched it at all, except to remove
some logic that is definitely not used by our `Display` impl. Otherwise,
I did not want to touch that house of cards for now, since the display
logic is not load-bearing for any type inference logic.

For all non-display callers, we have a new **_sequent map_** data type,
which tracks exactly the same derived information. But it does so (a)
without trying to remove anything from the BDD, and (b) lazily, without
updating the BDD structure.

So the end result is that all of the tests (including the new
regressions) pass, via a more efficient (and hopefully better
structured/documented) implementation, at the cost of hanging onto a
pile of display-related tech debt that we'll want to clean up at some
point.
2025-11-18 12:02:25 -05:00
..
ruff Fix analyze graph tests on windows (#21481) 2025-11-16 18:27:07 +00:00
ruff_annotate_snippets [ty] Add hyperlinks to rule codes in CLI (#21502) 2025-11-18 16:36:59 +01:00
ruff_benchmark [ty] Faster subscript assignment checks for (unions of) TypedDicts (#21378) 2025-11-12 20:16:38 +01:00
ruff_cache
ruff_db [ty] Add hyperlinks to rule codes in CLI (#21502) 2025-11-18 16:36:59 +01:00
ruff_dev Update Rust toolchain to 1.91 (#21179) 2025-11-01 01:50:58 +00:00
ruff_diagnostics
ruff_formatter [ty] Use "cannot" consistently over "can not" (#21255) 2025-11-03 10:38:20 -05:00
ruff_graph analyze: Add option to skip over imports in TYPE_CHECKING blocks (#21472) 2025-11-16 12:30:24 +00:00
ruff_index
ruff_linter [ruff] Avoid false positive on ClassVar reassignment (RUF012) (#21478) 2025-11-17 15:52:24 -05:00
ruff_macros Document when a rule was added (#21035) 2025-10-23 14:48:41 -04:00
ruff_memory_usage
ruff_notebook [ty] Respect notebook cell boundaries when adding an auto import (#21322) 2025-11-13 18:58:08 +01:00
ruff_options_metadata
ruff_python_ast [ruff] Ignore str() when not used for simple conversion (RUF065) (#21330) 2025-11-10 18:04:41 -05:00
ruff_python_ast_integration_tests
ruff_python_codegen Configurable "unparse mode" for ruff_python_codegen::Generator (#21041) 2025-10-24 15:44:48 +00:00
ruff_python_formatter Fix panic when formatting comments in unary expressions (#21501) 2025-11-18 10:48:14 -05:00
ruff_python_importer [ty] Respect notebook cell boundaries when adding an auto import (#21322) 2025-11-13 18:58:08 +01:00
ruff_python_index
ruff_python_literal
ruff_python_parser [ty] name is parameter and global is a syntax error (#21312) 2025-11-14 18:15:34 +00:00
ruff_python_semantic [pyflakes] Revert to stable behavior if imports for module lie in alternate branches for F401 (#20878) 2025-10-27 10:23:36 -05:00
ruff_python_stdlib
ruff_python_trivia
ruff_python_trivia_integration_tests
ruff_server Fix missing diagnostics for notebooks (#21156) 2025-10-31 01:16:43 +00:00
ruff_source_file
ruff_text_size [ruff] Update schemars to v1 (#20942) 2025-10-20 08:59:52 +02:00
ruff_wasm Bump 0.14.5 (#21435) 2025-11-13 14:37:31 -05:00
ruff_workspace analyze: Add option to skip over imports in TYPE_CHECKING blocks (#21472) 2025-11-16 12:30:24 +00:00
ty [ty] Better invalid-assignment diagnostics (#21476) 2025-11-18 14:31:04 +01:00
ty_combine
ty_completion_eval [ty] Sync vendored typeshed stubs (#21466) 2025-11-15 17:12:32 +00:00
ty_ide [ty] suppress invalid suggestions in import statements (#21484) 2025-11-17 09:58:49 -05:00
ty_project [ty] Fix --exclude and src.exclude merging (#21341) 2025-11-10 12:52:45 +01:00
ty_python_semantic [ty] Better handling of "derived information" in constraint sets (#21463) 2025-11-18 12:02:25 -05:00
ty_server [ty] Add hyperlinks to rule codes in CLI (#21502) 2025-11-18 16:36:59 +01:00
ty_static
ty_test [ty] Provide proper error on dangling revealed (#21416) 2025-11-16 08:34:54 +00:00
ty_vendored [ty] Sync vendored typeshed stubs (#21466) 2025-11-15 17:12:32 +00:00
ty_wasm [ty] Inlay hint call argument location (#20349) 2025-11-17 11:33:09 +01:00