-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Fix panic due to simplifying Divergent types out of intersections types
#21253
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
[ty] Fix panic due to simplifying Divergent types out of intersections types
#21253
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
|
Nice, thank you! I was initially confused about why we didn't need this for the branch above this, but I see that it should be handled by an earlier branch. We could possibly add a diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs
index 6efa8950f2..45a4b2aad5 100644
--- a/crates/ty_python_semantic/src/types.rs
+++ b/crates/ty_python_semantic/src/types.rs
@@ -1699,15 +1699,22 @@ impl<'db> Type<'db> {
// holds true if `T` is also a dynamic type or a union that contains a dynamic type.
// Similarly, `T <: Any` only holds true if `T` is a dynamic type or an intersection
// that contains a dynamic type.
- (Type::Dynamic(_), _) => ConstraintSet::from(match relation {
- TypeRelation::Subtyping => false,
- TypeRelation::Assignability => true,
- TypeRelation::Redundancy => match target {
- Type::Dynamic(_) => true,
- Type::Union(union) => union.elements(db).iter().any(Type::is_dynamic),
- _ => false,
- },
- }),
+ (Type::Dynamic(dynamic), _) => {
+ // If a `Divergent` type is involved, it must not be eliminated.
+ debug_assert!(
+ !matches!(dynamic, DynamicType::Divergent(_)),
+ "DynamicType::Divergent should have been handled in an earlier branch"
+ );
+ ConstraintSet::from(match relation {
+ TypeRelation::Subtyping => false,
+ TypeRelation::Assignability => true,
+ TypeRelation::Redundancy => match target {
+ Type::Dynamic(_) => true,
+ Type::Union(union) => union.elements(db).iter().any(Type::is_dynamic),
+ _ => false,
+ },
+ })
+ }
(_, Type::Dynamic(_)) => ConstraintSet::from(match relation {
TypeRelation::Subtyping => false,
TypeRelation::Assignability => true, |
Divergent types within a union typeDivergent types out of intersections types
|
Especially with the changes in #20566, I start to wonder if |
Summary
While investigating the cause of the panics seen since #20962, I found that intersection types containing
Divergenttypes were incorrectly reduced by unioning them with another dynamic types.This prevents tracking of
Divergenttypes in fixed-point iteration.This PR fixes the logic of
Type::is_redundant_withto prevent the panic from occurring.(BTW, another panic in
pr_20962_comprehension_panics.mdhas been confirmed to disappear by #20566)Test Plan
corpus/cyclic_comprehensions.pyis added.