Revert "add IntersectionBuilder::intersection_hashes"

This reverts commit af8b75b3fd.
This commit is contained in:
Shunsuke Shibayama 2025-12-19 09:43:43 +09:00
parent d77b45c995
commit bbad56bfd3

View file

@ -44,7 +44,7 @@ use crate::types::{
TypeVarBoundOrConstraints, UnionType,
};
use crate::{Db, FxOrderSet};
use rustc_hash::{FxHashSet, FxHasher};
use rustc_hash::FxHashSet;
use smallvec::SmallVec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -691,22 +691,16 @@ pub(crate) struct IntersectionBuilder<'db> {
// but if a union is added to the intersection, we'll distribute ourselves over that union and
// create a union of intersections.
intersections: Vec<InnerIntersectionBuilder<'db>>,
/// Stores hash values of `intersections` to prevent adding identical `InnerIntersectionBuilder`s.
intersection_hashes: FxHashSet<u64>,
order_elements: bool,
db: &'db dyn Db,
}
impl<'db> IntersectionBuilder<'db> {
pub(crate) fn new(db: &'db dyn Db) -> Self {
let inner = InnerIntersectionBuilder::default();
let mut intersection_hashes = FxHashSet::default();
intersection_hashes.insert(inner.hash_value());
Self {
db,
order_elements: false,
intersections: vec![inner],
intersection_hashes,
intersections: vec![InnerIntersectionBuilder::default()],
}
}
@ -715,22 +709,6 @@ impl<'db> IntersectionBuilder<'db> {
db,
order_elements: false,
intersections: vec![],
intersection_hashes: FxHashSet::default(),
}
}
fn update_hashes(&mut self) {
self.intersection_hashes.clear();
for intersection in &self.intersections {
self.intersection_hashes.insert(intersection.hash_value());
}
}
fn extend(&mut self, other: IntersectionBuilder<'db>) {
for intersection in other.intersections {
if self.intersection_hashes.insert(intersection.hash_value()) {
self.intersections.push(intersection);
}
}
}
@ -750,7 +728,6 @@ impl<'db> IntersectionBuilder<'db> {
for inner in &mut self.intersections {
inner.positive.insert(ty);
}
self.update_hashes();
return self;
}
seen_aliases.push(ty);
@ -771,7 +748,7 @@ impl<'db> IntersectionBuilder<'db> {
.iter()
.map(|elem| self.clone().add_positive_impl(*elem, seen_aliases))
.fold(IntersectionBuilder::empty(self.db), |mut builder, sub| {
builder.extend(sub);
builder.intersections.extend(sub.intersections);
builder
})
}
@ -823,7 +800,6 @@ impl<'db> IntersectionBuilder<'db> {
for inner in &mut self.intersections {
inner.add_positive(self.db, ty);
}
self.update_hashes();
self
}
}
@ -833,7 +809,6 @@ impl<'db> IntersectionBuilder<'db> {
for inner in &mut self.intersections {
inner.add_positive(self.db, ty);
}
self.update_hashes();
self
}
}
@ -863,7 +838,6 @@ impl<'db> IntersectionBuilder<'db> {
for inner in &mut self.intersections {
inner.negative.insert(ty);
}
self.update_hashes();
return self;
}
seen_aliases.push(ty);
@ -905,7 +879,7 @@ impl<'db> IntersectionBuilder<'db> {
positive_side.chain(negative_side).fold(
IntersectionBuilder::empty(self.db),
|mut builder, sub| {
builder.extend(sub);
builder.intersections.extend(sub.intersections);
builder
},
)
@ -931,7 +905,6 @@ impl<'db> IntersectionBuilder<'db> {
for inner in &mut self.intersections {
inner.add_negative(self.db, ty);
}
self.update_hashes();
self
}
}
@ -1345,30 +1318,6 @@ impl<'db> InnerIntersectionBuilder<'db> {
}
}
}
/// An element-order-independent hash value, unrelated to the hash value of the actual `IntersectionType`.
fn hash_value(&self) -> u64 {
use std::hash::{Hash, Hasher};
let mut sum = 0u64;
for ty in &self.positive {
sum = sum.wrapping_add({
let mut hasher = FxHasher::default();
ty.hash(&mut hasher);
hasher.finish()
});
}
for ty in &self.negative {
sum = sum.wrapping_add({
let mut hasher = FxHasher::default();
ty.hash(&mut hasher);
// We add a constant to the negative types to differentiate them from positive types.
1.hash(&mut hasher);
hasher.finish()
});
}
sum
}
}
#[cfg(test)]