Skip to content

Commit bf47905

Browse files
committed
Update type_ordering.rs
* exclude ordering of nested unions (which cannot exist in DNF) as unreachable codes * try to avoid performance degradation in ordering of intersections
1 parent a089eb9 commit bf47905

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

crates/red_knot_python_semantic/src/types/type_ordering.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,37 +269,31 @@ pub(super) fn union_elements_ordering<'db>(
269269
(Type::Dynamic(_), _) => Ordering::Less,
270270
(_, Type::Dynamic(_)) => Ordering::Greater,
271271

272-
(Type::Union(left), Type::Union(right)) => {
273-
if left.elements(db).len() != right.elements(db).len() {
274-
return left.elements(db).len().cmp(&right.elements(db).len());
275-
}
276-
// Lexicographically compare the elements of the two unions.
277-
for (left, right) in left.elements(db).iter().zip(right.elements(db)) {
278-
let ordering = union_elements_ordering(left, right, db);
279-
if ordering != Ordering::Equal {
280-
return ordering;
281-
}
282-
}
283-
Ordering::Equal
272+
(Type::Union(_), Type::Union(_)) => {
273+
unreachable!("our type representation does not permit nested unions");
284274
}
285275
(Type::Union(_), _) => Ordering::Less,
286276
(_, Type::Union(_)) => Ordering::Greater,
287277

288278
(Type::Intersection(left), Type::Intersection(right)) => {
289279
// Lexicographically compare the elements of the two intersections.
290-
if left.positive(db).len() != right.positive(db).len() {
291-
return left.positive(db).len().cmp(&right.positive(db).len());
280+
let left_positive = left.positive(db);
281+
let right_positive = right.positive(db);
282+
if left_positive.len() != right_positive.len() {
283+
return left_positive.len().cmp(&right_positive.len());
292284
}
293-
if left.negative(db).len() != right.negative(db).len() {
294-
return left.negative(db).len().cmp(&right.negative(db).len());
285+
let left_negative = left.negative(db);
286+
let right_negative = right.negative(db);
287+
if left_negative.len() != right_negative.len() {
288+
return left_negative.len().cmp(&right_negative.len());
295289
}
296-
for (left, right) in left.positive(db).iter().zip(right.positive(db)) {
290+
for (left, right) in left_positive.iter().zip(right_positive) {
297291
let ordering = union_elements_ordering(left, right, db);
298292
if ordering != Ordering::Equal {
299293
return ordering;
300294
}
301295
}
302-
for (left, right) in left.negative(db).iter().zip(right.negative(db)) {
296+
for (left, right) in left_negative.iter().zip(right_negative) {
303297
let ordering = union_elements_ordering(left, right, db);
304298
if ordering != Ordering::Equal {
305299
return ordering;

0 commit comments

Comments
 (0)