-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Fix negation upper bounds in constraint sets #21897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -435,6 +435,11 @@ impl<'db> ConstraintSet<'db> { | |
| pub(crate) fn display(self, db: &'db dyn Db) -> impl Display { | ||
| self.node.simplify_for_display(db).display(db) | ||
| } | ||
|
|
||
| #[expect(dead_code)] // Keep this around for debugging purposes | ||
| pub(crate) fn display_graph(self, db: &'db dyn Db, prefix: &dyn Display) -> impl Display { | ||
| self.node.display_graph(db, prefix) | ||
| } | ||
| } | ||
|
|
||
| impl From<bool> for ConstraintSet<'_> { | ||
|
|
@@ -498,11 +503,13 @@ impl<'db> ConstrainedTypeVar<'db> { | |
| debug_assert_eq!(upper, upper.top_materialization(db)); | ||
|
|
||
| // It's not useful for an upper bound to be an intersection type, or for a lower bound to | ||
| // be a union type. Both of those can be rewritten as simpler BDDs: | ||
| // be a union type. Because the following equivalences hold, we can break these bounds | ||
| // apart and create an equivalent BDD with more nodes but simpler constraints. (Fewer, | ||
| // simpler constraints mean that our sequent maps won't grow pathologically large.) | ||
| // | ||
| // T ≤ α & β ⇒ (T ≤ α) ∧ (T ≤ β) | ||
| // T ≤ α & ¬β ⇒ (T ≤ α) ∧ ¬(T ≤ β) | ||
| // α | β ≤ T ⇒ (α ≤ T) ∧ (β ≤ T) | ||
| // T ≤ (α & β) ⇔ (T ≤ α) ∧ (T ≤ β) | ||
| // T ≤ (¬α & ¬β) ⇔ (T ≤ ¬α) ∧ (T ≤ ¬β) | ||
| // (α | β) ≤ T ⇔ (α ≤ T) ∧ (β ≤ T) | ||
| if let Type::Union(lower_union) = lower { | ||
| let mut result = Node::AlwaysTrue; | ||
| for lower_element in lower_union.elements(db) { | ||
|
|
@@ -513,7 +520,12 @@ impl<'db> ConstrainedTypeVar<'db> { | |
| } | ||
| return result; | ||
| } | ||
| if let Type::Intersection(upper_intersection) = upper { | ||
| // A negated type ¬α is represented as an intersection with no positive elements, and a | ||
| // single negative element. We _don't_ want to treat that an "intersection" for the | ||
| // purposes of simplifying upper bounds. | ||
| if let Type::Intersection(upper_intersection) = upper | ||
| && !upper_intersection.is_simple_negation(db) | ||
| { | ||
| let mut result = Node::AlwaysTrue; | ||
| for upper_element in upper_intersection.iter_positive(db) { | ||
| result = result.and( | ||
|
|
@@ -524,7 +536,7 @@ impl<'db> ConstrainedTypeVar<'db> { | |
| for upper_element in upper_intersection.iter_negative(db) { | ||
| result = result.and( | ||
| db, | ||
| ConstrainedTypeVar::new_node(db, typevar, lower, upper_element).negate(db), | ||
| ConstrainedTypeVar::new_node(db, typevar, lower, upper_element.negate(db)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So subtle 😃 |
||
| ); | ||
| } | ||
| return result; | ||
|
|
@@ -716,7 +728,7 @@ impl<'db> ConstrainedTypeVar<'db> { | |
| return IntersectionResult::Disjoint; | ||
| } | ||
|
|
||
| if lower.is_union() || upper.is_intersection() { | ||
| if lower.is_union() || upper.is_nontrivial_intersection(db) { | ||
| return IntersectionResult::CannotSimplify; | ||
| } | ||
|
|
||
|
|
@@ -1579,7 +1591,6 @@ impl<'db> Node<'db> { | |
| /// │ └─₀ never | ||
| /// └─₀ never | ||
| /// ``` | ||
| #[cfg_attr(not(test), expect(dead_code))] // Keep this around for debugging purposes | ||
| fn display_graph(self, db: &'db dyn Db, prefix: &dyn Display) -> impl Display { | ||
| struct DisplayNode<'a, 'db> { | ||
| db: &'db dyn Db, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This DSL is so cool