Skip to content

Commit e7db49d

Browse files
committed
Use derived Hash when possible
1 parent a2dc6a6 commit e7db49d

File tree

1 file changed

+9
-8
lines changed
  • crates/ty_python_semantic/src/semantic_index

1 file changed

+9
-8
lines changed

crates/ty_python_semantic/src/semantic_index/place.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl PlaceExprSubSegment {
3838
}
3939

4040
/// An expression that can be the target of a `Definition`.
41-
#[derive(Eq, PartialEq, Debug, Hash)]
41+
#[derive(Eq, PartialEq, Debug)]
4242
pub struct PlaceExpr {
4343
root_name: Name,
4444
sub_segments: SmallVec<[PlaceExprSubSegment; 1]>,
@@ -342,6 +342,7 @@ impl PlaceExprWithFlags {
342342
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
343343
pub struct PlaceExprRef<'a> {
344344
pub(crate) root_name: &'a Name,
345+
/// Sub-segments is empty for a simple target (e.g. `foo`).
345346
pub(crate) sub_segments: &'a [PlaceExprSubSegment],
346347
}
347348

@@ -708,13 +709,13 @@ impl PlaceTable {
708709
let place_expr: PlaceExprRef = place_expr.into();
709710

710711
let mut hasher = FxHasher::default();
711-
place_expr.root_name.as_str().hash(&mut hasher);
712-
for segment in place_expr.sub_segments {
713-
match segment {
714-
PlaceExprSubSegment::Member(name) => name.hash(&mut hasher),
715-
PlaceExprSubSegment::IntSubscript(int) => int.hash(&mut hasher),
716-
PlaceExprSubSegment::StringSubscript(string) => string.hash(&mut hasher),
717-
}
712+
713+
// Special case for simple names (e.g. "foo"). Only hash the name so
714+
// that a lookup by name can find it (see `place_by_name`).
715+
if place_expr.sub_segments.is_empty() {
716+
place_expr.root_name.as_str().hash(&mut hasher);
717+
} else {
718+
place_expr.hash(&mut hasher);
718719
}
719720
hasher.finish()
720721
}

0 commit comments

Comments
 (0)