From d6fa64fa01c695c484a403921c1c0818345c6af1 Mon Sep 17 00:00:00 2001 From: InSyncWithFoo Date: Wed, 8 Jan 2025 17:12:47 +0000 Subject: [PATCH] Simplify per review --- crates/red_knot_python_semantic/src/types.rs | 71 +++++++++----------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 847a2ccfd2957c..9e4cc4ba83a9ec 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -1,6 +1,4 @@ -use rustc_hash::FxHasher; -use std::collections::HashSet; -use std::hash::{BuildHasherDefault, Hash}; +use std::hash::Hash; use std::iter; use context::InferContext; @@ -558,8 +556,6 @@ pub enum Type<'db> { // TODO protocols, callable types, overloads, generics, type vars } -type OrderedTypeSet<'a, 'db> = HashSet<&'a Type<'db>, BuildHasherDefault>; - impl<'db> Type<'db> { pub const fn is_unknown(&self) -> bool { matches!(self, Type::Unknown) @@ -1490,9 +1486,6 @@ impl<'db> Type<'db> { return false; } - let first_elements = first_elements.iter().collect::(); - let second_elements = second_elements.iter().collect::(); - iter::zip(first_elements, second_elements).all(equivalent) } @@ -1509,12 +1502,6 @@ impl<'db> Type<'db> { return false; } - let first_positive = first_positive.iter().collect::(); - let first_negative = first_negative.iter().collect::(); - - let second_positive = second_positive.iter().collect::(); - let second_negative = second_negative.iter().collect::(); - iter::zip(first_positive, second_positive).all(equivalent) && iter::zip(first_negative, second_negative).all(equivalent) } @@ -4746,6 +4733,7 @@ pub(crate) mod tests { assert!(!from.into_type(&db).is_fully_static(&db)); } + #[test_case(Ty::Todo, Ty::Todo)] #[test_case(Ty::Any, Ty::Any)] #[test_case(Ty::Unknown, Ty::Unknown)] #[test_case(Ty::Any, Ty::Unknown)] @@ -4758,30 +4746,32 @@ pub(crate) mod tests { #[test_case(Ty::SliceLiteral(0, 1, 2), Ty::SliceLiteral(0, 1, 2))] #[test_case(Ty::BuiltinClassLiteral("str"), Ty::BuiltinClassLiteral("str"))] #[test_case(Ty::SubclassOfAny, Ty::SubclassOfUnknown)] - #[test_case( - Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]), - Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")]) - )] - #[test_case( - Ty::Intersection { - pos: vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")], - neg: vec![Ty::BuiltinInstance("bytes"), Ty::None] - }, - Ty::Intersection { - pos: vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")], - neg: vec![Ty::None, Ty::BuiltinInstance("bytes")] - } - )] - #[test_case( - Ty::Intersection { - pos: vec![Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")])], - neg: vec![Ty::SubclassOfAny] - }, - Ty::Intersection { - pos: vec![Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])], - neg: vec![Ty::SubclassOfUnknown] - } - )] + #[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfBuiltinClass("object"))] + // TODO: Compare unions/intersections with different orders + // #[test_case( + // Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]), + // Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")]) + // )] + // #[test_case( + // Ty::Intersection { + // pos: vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")], + // neg: vec![Ty::BuiltinInstance("bytes"), Ty::None] + // }, + // Ty::Intersection { + // pos: vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")], + // neg: vec![Ty::None, Ty::BuiltinInstance("bytes")] + // } + // )] + // #[test_case( + // Ty::Intersection { + // pos: vec![Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")])], + // neg: vec![Ty::SubclassOfAny] + // }, + // Ty::Intersection { + // pos: vec![Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str")])], + // neg: vec![Ty::SubclassOfUnknown] + // } + // )] fn is_gradual_equivalent_to(a: Ty, b: Ty) { let db = setup_db(); let a = a.into_type(&db); @@ -4791,7 +4781,10 @@ pub(crate) mod tests { assert!(b.is_gradual_equivalent_to(&db, a)); } - #[test_case(Ty::Todo, Ty::Todo)] + #[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfAny)] + #[test_case(Ty::BuiltinInstance("type"), Ty::SubclassOfUnknown)] + #[test_case(Ty::SubclassOfBuiltinClass("object"), Ty::SubclassOfAny)] + #[test_case(Ty::SubclassOfBuiltinClass("object"), Ty::SubclassOfUnknown)] #[test_case( Ty::Union(vec![Ty::BuiltinInstance("str"), Ty::BuiltinInstance("int")]), Ty::Union(vec![Ty::BuiltinInstance("int"), Ty::BuiltinInstance("str"), Ty::BuiltinInstance("bytes")])