Skip to content

Conversation

@dcreager
Copy link
Member

@dcreager dcreager commented Oct 24, 2025

It's possible for a constraint to mention two typevars. For instance, in the body of

def f[S: int, T: S](): ...

the baseline constraint set would be (T ≤ S) ∧ (S ≤ int). That is, S must specialize to some subtype of int, and T must specialize to a subtype of the type that S specializes to.

This PR updates the new "constraint implication" relationship from #21010 to work on these kinds of constraint sets. For instance, in the example above, we should be able to see that T ≤ int must always hold:

def f[S, T]():
    constraints = ConstraintSet::range(Never, S, int) & constraint_set(Never, T, S)
    static_assert(constraints.implies_subtype_of(T, int))  # now succeeds!

This did not require major changes to the implementation of implies_subtype_of. That method already relies on how our simplify and domain methods expand a constraint set to include the transitive closure of the constraints that it mentions, and to mark certain combinations of constraints as impossible. Previously, that transitive closure logic only looked at pairs of constraints that constrain the same typevar. (For instance, to notice that (T ≤ bool) ∧ ¬(T ≤ int) is impossible.)

Now we also look at pairs of constraints that constraint different typevars, if one of the constraints is bound by the other — that is, pairs of the form T ≤ S and S ≤ something, or S ≤ T and something ≤ S. In those cases, transitivity lets us add a new derived constraint that T ≤ something or something ≤ T, respectively. Having done that, our existing implies_subtype_of logic finds and takes into account that derived constraint.

@dcreager dcreager added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Oct 24, 2025
@dcreager dcreager force-pushed the dcreager/subtype-given-typevars branch from fdea956 to c15f58c Compare October 24, 2025 18:20
@dcreager dcreager changed the base branch from main to dcreager/new-relation October 24, 2025 18:20
@dcreager dcreager force-pushed the dcreager/subtype-given-typevars branch from c15f58c to eed87d1 Compare October 24, 2025 20:15
@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

Base automatically changed from dcreager/new-relation to main October 28, 2025 02:01
@dcreager dcreager force-pushed the dcreager/subtype-given-typevars branch from eed87d1 to 5e98232 Compare October 28, 2025 02:02
@dcreager dcreager force-pushed the dcreager/subtype-given-typevars branch from 5e98232 to e8fd554 Compare October 28, 2025 14:30
@dcreager dcreager changed the base branch from main to dcreager/constraint-set-api October 28, 2025 14:31
@dcreager dcreager marked this pull request as ready for review October 28, 2025 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants