From 3feb3dfb6d923c07785e9f3df5bccff6d126fe88 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 3 Jan 2026 10:49:10 +0000 Subject: [PATCH] fix `is_empty` impl and add comments --- crates/ty_python_semantic/src/types.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index 9950d3fb85..04f8df47c7 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -14423,8 +14423,12 @@ impl<'db> NegativeIntersectionElements<'db> { } } - pub(crate) const fn is_empty(&self) -> bool { - matches!(self, Self::Empty) + pub(crate) fn is_empty(&self) -> bool { + // We could try to maintain the invariant that length-0 collections are always + // represented using `Self::Empty`, in which case this could just be + // `matches!(self, Self::Empty)`. But maintaining that invariant seems like + // it could add unnecessary overhead. + self.len() == 0 } pub(crate) fn insert(&mut self, ty: Type<'db>) { @@ -14479,6 +14483,9 @@ impl<'db> NegativeIntersectionElements<'db> { false } } + // We don't try to maintain the invariant here that length-0 collections + // are *always* `Self::Empty` and length-1 collections are *always* + // `Self::Single`. It's unnecessary to do so, and would probably add overhead. Self::Multiple(set) => set.swap_remove(ty), } } @@ -14495,6 +14502,9 @@ impl<'db> NegativeIntersectionElements<'db> { None } } + // We don't try to maintain the invariant here that length-0 collections + // are *always* `Self::Empty` and length-1 collections are *always* + // `Self::Single`. It's unnecessary to do so, and would probably add overhead. Self::Multiple(set) => set.swap_remove_index(index), } }