From b84b58760ee00747afba2a8b437145f0e2c40514 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 30 Apr 2025 11:58:55 +0100 Subject: [PATCH] [red-knot] Computing a type ordering for two non-normalized types is meaningless (#17734) --- .../src/types/type_ordering.rs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types/type_ordering.rs b/crates/red_knot_python_semantic/src/types/type_ordering.rs index 64d9bb726a..ba2adf6b40 100644 --- a/crates/red_knot_python_semantic/src/types/type_ordering.rs +++ b/crates/red_knot_python_semantic/src/types/type_ordering.rs @@ -26,6 +26,17 @@ pub(super) fn union_or_intersection_elements_ordering<'db>( left: &Type<'db>, right: &Type<'db>, ) -> Ordering { + debug_assert_eq!( + *left, + left.normalized(db), + "`left` must be normalized before a meaningful ordering can be established" + ); + debug_assert_eq!( + *right, + right.normalized(db), + "`right` must be normalized before a meaningful ordering can be established" + ); + if left == right { return Ordering::Equal; } @@ -85,19 +96,11 @@ pub(super) fn union_or_intersection_elements_ordering<'db>( (Type::DataclassTransformer(_), _) => Ordering::Less, (_, Type::DataclassTransformer(_)) => Ordering::Greater, - (Type::Callable(left), Type::Callable(right)) => { - debug_assert_eq!(*left, left.normalized(db)); - debug_assert_eq!(*right, right.normalized(db)); - left.cmp(right) - } + (Type::Callable(left), Type::Callable(right)) => left.cmp(right), (Type::Callable(_), _) => Ordering::Less, (_, Type::Callable(_)) => Ordering::Greater, - (Type::Tuple(left), Type::Tuple(right)) => { - debug_assert_eq!(*left, left.normalized(db)); - debug_assert_eq!(*right, right.normalized(db)); - left.cmp(right) - } + (Type::Tuple(left), Type::Tuple(right)) => left.cmp(right), (Type::Tuple(_), _) => Ordering::Less, (_, Type::Tuple(_)) => Ordering::Greater, @@ -325,13 +328,6 @@ pub(super) fn union_or_intersection_elements_ordering<'db>( } (Type::Intersection(left), Type::Intersection(right)) => { - debug_assert_eq!(*left, left.normalized(db)); - debug_assert_eq!(*right, right.normalized(db)); - - if left == right { - return Ordering::Equal; - } - // Lexicographically compare the elements of the two unequal intersections. let left_positive = left.positive(db); let right_positive = right.positive(db); @@ -356,7 +352,7 @@ pub(super) fn union_or_intersection_elements_ordering<'db>( } } - unreachable!("Two equal intersections that both have sorted elements should share the same Salsa ID") + unreachable!("Two equal, normalized intersections should share the same Salsa ID") } } }