Skip to content

Commit 0e1f6f1

Browse files
committed
refactor & update docs
1 parent a4ef995 commit 0e1f6f1

1 file changed

Lines changed: 49 additions & 49 deletions

File tree

  • crates/ty_python_semantic/src/semantic_index

crates/ty_python_semantic/src/semantic_index/use_def.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@
160160
//! complete module, not a partially-executed module. (We may want to get a little smarter than
161161
//! this in the future for some closures, but for now this is where we start.)
162162
//!
163-
//! The data structure we build to answer these questions is the `UseDefMap`. It has an interned
164-
//! bindings table plus a `bindings_by_use` vector of interned bindings IDs indexed by
165-
//! [`ScopedUseId`], a
166-
//! `declarations_by_binding` vector of [`Declarations`] indexed by [`ScopedDefinitionId`], a
163+
//! The data structure we build to answer these questions is the `UseDefMap`. It has a
164+
//! `bindings_by_use` vector of [`ScopedBindingsId`] indexed by [`ScopedUseId`]
165+
//! (plus an interned bindings table), a
166+
//! `declarations_by_binding` vector of [`ScopedDeclarationsId`] indexed by [`ScopedDefinitionId`], a
167167
//! `bindings_by_declaration` vector of [`Bindings`] indexed by [`ScopedDefinitionId`], and
168-
//! `public_bindings` and `public_definitions` vectors indexed by [`ScopedPlaceId`]. The values in
168+
//! `end_of_scope_symbols` and `end_of_scope_members` vectors indexed by [`ScopedSymbolId`]/[`ScopedMemberId`]. The values in
169169
//! each of these vectors are (in principle) a list of live bindings at that use/definition, or at
170170
//! the end of the scope for that place, with a list of the dominating constraints for each
171171
//! binding.
@@ -274,30 +274,30 @@ pub(crate) use place_state::{LiveBinding, ScopedDefinitionId};
274274
/// Uniquely identifies an interned [`Bindings`] entry in [`UseDefMap::interned_bindings`].
275275
#[newtype_index]
276276
#[derive(salsa::Update, get_size2::GetSize)]
277-
struct ScopedBindingsId;
277+
struct InternedBindingsId;
278278

279279
/// Uniquely identifies an interned [`Declarations`] entry in [`UseDefMap::interned_declarations`].
280280
#[newtype_index]
281281
#[derive(salsa::Update, get_size2::GetSize)]
282-
struct ScopedDeclarationsId;
282+
struct InternedDeclarationsId;
283283

284284
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, salsa::Update, get_size2::GetSize)]
285-
struct ScopedPlaceStateId(ScopedBindingsId, ScopedDeclarationsId);
285+
struct InternedPlaceStateId(InternedBindingsId, InternedDeclarationsId);
286286

287-
impl ScopedPlaceStateId {
288-
fn bindings_id(self) -> ScopedBindingsId {
287+
impl InternedPlaceStateId {
288+
fn bindings_id(self) -> InternedBindingsId {
289289
self.0
290290
}
291291

292-
fn declarations_id(self) -> ScopedDeclarationsId {
292+
fn declarations_id(self) -> InternedDeclarationsId {
293293
self.1
294294
}
295295
}
296296

297297
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, salsa::Update, get_size2::GetSize)]
298-
enum ScopedEnclosingSnapshotDataId {
298+
enum InternedEnclosingSnapshotId {
299299
Constraint(ScopedNarrowingConstraint),
300-
Bindings(ScopedBindingsId),
300+
Bindings(InternedBindingsId),
301301
}
302302

303303
/// Applicable definitions and constraints for every use of a name.
@@ -314,12 +314,12 @@ pub(crate) struct UseDefMap<'db> {
314314
reachability_constraints: ReachabilityConstraints,
315315

316316
/// Interned [`Bindings`] values.
317-
interned_bindings: IndexVec<ScopedBindingsId, Bindings>,
317+
interned_bindings: IndexVec<InternedBindingsId, Bindings>,
318318
/// Interned [`Declarations`] values.
319-
interned_declarations: IndexVec<ScopedDeclarationsId, Declarations>,
319+
interned_declarations: IndexVec<InternedDeclarationsId, Declarations>,
320320

321321
/// [`Bindings`] reaching a [`ScopedUseId`].
322-
bindings_by_use: IndexVec<ScopedUseId, ScopedBindingsId>,
322+
bindings_by_use: IndexVec<ScopedUseId, InternedBindingsId>,
323323

324324
/// Tracks whether or not a given AST node is reachable from the start of the scope.
325325
node_reachability: FxHashMap<NodeKey, ScopedReachabilityConstraintId>,
@@ -330,7 +330,7 @@ pub(crate) struct UseDefMap<'db> {
330330
/// If the definition is both a declaration and a binding -- `x: int = 1` for example -- then
331331
/// we don't actually need anything here, all we'll need to validate is that our own RHS is a
332332
/// valid assignment to our own annotation.
333-
declarations_by_binding: FxHashMap<Definition<'db>, ScopedDeclarationsId>,
333+
declarations_by_binding: FxHashMap<Definition<'db>, InternedDeclarationsId>,
334334

335335
/// If the definition is a declaration (only) -- `x: int` for example -- then we need
336336
/// [`Bindings`] to know whether this declaration is consistent with the previously
@@ -342,13 +342,13 @@ pub(crate) struct UseDefMap<'db> {
342342
///
343343
/// If we see a binding to a `Final`-qualified symbol, we also need this map to find previous
344344
/// bindings to that symbol. If there are any, the assignment is invalid.
345-
bindings_by_definition: FxHashMap<Definition<'db>, ScopedBindingsId>,
345+
bindings_by_definition: FxHashMap<Definition<'db>, InternedBindingsId>,
346346

347347
/// [`PlaceState`] visible at end of scope for each symbol.
348348
end_of_scope_symbols: IndexVec<ScopedSymbolId, PlaceState>,
349349

350350
/// [`PlaceState`] visible at end of scope for each member.
351-
end_of_scope_members: IndexVec<ScopedMemberId, ScopedPlaceStateId>,
351+
end_of_scope_members: IndexVec<ScopedMemberId, InternedPlaceStateId>,
352352

353353
/// All potentially reachable bindings and declarations, for each symbol.
354354
reachable_definitions_by_symbol: IndexVec<ScopedSymbolId, ReachableDefinitions>,
@@ -358,7 +358,7 @@ pub(crate) struct UseDefMap<'db> {
358358

359359
/// Snapshot of bindings in this scope that can be used to resolve a reference in a nested
360360
/// scope.
361-
enclosing_snapshots: IndexVec<ScopedEnclosingSnapshotId, ScopedEnclosingSnapshotDataId>,
361+
enclosing_snapshots: IndexVec<ScopedEnclosingSnapshotId, InternedEnclosingSnapshotId>,
362362

363363
/// Whether or not the end of the scope is reachable.
364364
///
@@ -542,10 +542,10 @@ impl<'db> UseDefMap<'db> {
542542
};
543543

544544
match self.enclosing_snapshots.get(snapshot_id) {
545-
Some(ScopedEnclosingSnapshotDataId::Constraint(constraint)) => {
545+
Some(InternedEnclosingSnapshotId::Constraint(constraint)) => {
546546
EnclosingSnapshotResult::FoundConstraint(*constraint)
547547
}
548-
Some(ScopedEnclosingSnapshotDataId::Bindings(bindings_id)) => {
548+
Some(InternedEnclosingSnapshotId::Bindings(bindings_id)) => {
549549
EnclosingSnapshotResult::FoundBindings(
550550
self.bindings_iterator(
551551
&self.interned_bindings[*bindings_id],
@@ -1585,7 +1585,7 @@ impl<'db> UseDefMapBuilder<'db> {
15851585
}
15861586
for enclosing_snapshot in &enclosing_snapshots {
15871587
// Bindings are already marked above.
1588-
if let ScopedEnclosingSnapshotDataId::Constraint(constraint) = enclosing_snapshot {
1588+
if let InternedEnclosingSnapshotId::Constraint(constraint) = enclosing_snapshot {
15891589
self.reachability_constraints.mark_used(*constraint);
15901590
}
15911591
}
@@ -1612,10 +1612,10 @@ impl<'db> UseDefMapBuilder<'db> {
16121612

16131613
fn intern_bindings_by_definition(
16141614
bindings_by_definition: FxHashMap<Definition<'db>, Bindings>,
1615-
interned_bindings: &mut IndexVec<ScopedBindingsId, Bindings>,
1616-
interned_ids_by_bindings: &mut FxHashMap<Bindings, ScopedBindingsId>,
1617-
) -> FxHashMap<Definition<'db>, ScopedBindingsId> {
1618-
let mut interned_ids_by_definition: FxHashMap<Definition<'db>, ScopedBindingsId> =
1615+
interned_bindings: &mut IndexVec<InternedBindingsId, Bindings>,
1616+
interned_ids_by_bindings: &mut FxHashMap<Bindings, InternedBindingsId>,
1617+
) -> FxHashMap<Definition<'db>, InternedBindingsId> {
1618+
let mut interned_ids_by_definition: FxHashMap<Definition<'db>, InternedBindingsId> =
16191619
FxHashMap::with_capacity_and_hasher(bindings_by_definition.len(), FxBuildHasher);
16201620

16211621
for (definition, bindings) in bindings_by_definition {
@@ -1635,10 +1635,10 @@ impl<'db> UseDefMapBuilder<'db> {
16351635

16361636
fn intern_declarations_by_binding(
16371637
declarations_by_binding: FxHashMap<Definition<'db>, Declarations>,
1638-
interned_declarations: &mut IndexVec<ScopedDeclarationsId, Declarations>,
1639-
interned_ids_by_declarations: &mut FxHashMap<Declarations, ScopedDeclarationsId>,
1640-
) -> FxHashMap<Definition<'db>, ScopedDeclarationsId> {
1641-
let mut interned_ids_by_binding: FxHashMap<Definition<'db>, ScopedDeclarationsId> =
1638+
interned_declarations: &mut IndexVec<InternedDeclarationsId, Declarations>,
1639+
interned_ids_by_declarations: &mut FxHashMap<Declarations, InternedDeclarationsId>,
1640+
) -> FxHashMap<Definition<'db>, InternedDeclarationsId> {
1641+
let mut interned_ids_by_binding: FxHashMap<Definition<'db>, InternedDeclarationsId> =
16421642
FxHashMap::with_capacity_and_hasher(declarations_by_binding.len(), FxBuildHasher);
16431643

16441644
for (binding, declarations) in declarations_by_binding {
@@ -1659,10 +1659,10 @@ impl<'db> UseDefMapBuilder<'db> {
16591659

16601660
fn intern_bindings_by_use(
16611661
bindings_by_use: IndexVec<ScopedUseId, Bindings>,
1662-
interned_bindings: &mut IndexVec<ScopedBindingsId, Bindings>,
1663-
interned_ids_by_bindings: &mut FxHashMap<Bindings, ScopedBindingsId>,
1664-
) -> IndexVec<ScopedUseId, ScopedBindingsId> {
1665-
let mut interned_ids_by_use: IndexVec<ScopedUseId, ScopedBindingsId> =
1662+
interned_bindings: &mut IndexVec<InternedBindingsId, Bindings>,
1663+
interned_ids_by_bindings: &mut FxHashMap<Bindings, InternedBindingsId>,
1664+
) -> IndexVec<ScopedUseId, InternedBindingsId> {
1665+
let mut interned_ids_by_use: IndexVec<ScopedUseId, InternedBindingsId> =
16661666
IndexVec::with_capacity(bindings_by_use.len());
16671667

16681668
for bindings in bindings_by_use {
@@ -1682,14 +1682,14 @@ impl<'db> UseDefMapBuilder<'db> {
16821682

16831683
fn intern_end_of_scope_members(
16841684
end_of_scope_members: IndexVec<ScopedMemberId, PlaceState>,
1685-
interned_bindings: &mut IndexVec<ScopedBindingsId, Bindings>,
1686-
interned_ids_by_bindings: &mut FxHashMap<Bindings, ScopedBindingsId>,
1687-
interned_declarations: &mut IndexVec<ScopedDeclarationsId, Declarations>,
1688-
interned_ids_by_declarations: &mut FxHashMap<Declarations, ScopedDeclarationsId>,
1689-
) -> IndexVec<ScopedMemberId, ScopedPlaceStateId> {
1690-
let mut interned_ids_by_member: IndexVec<ScopedMemberId, ScopedPlaceStateId> =
1685+
interned_bindings: &mut IndexVec<InternedBindingsId, Bindings>,
1686+
interned_ids_by_bindings: &mut FxHashMap<Bindings, InternedBindingsId>,
1687+
interned_declarations: &mut IndexVec<InternedDeclarationsId, Declarations>,
1688+
interned_ids_by_declarations: &mut FxHashMap<Declarations, InternedDeclarationsId>,
1689+
) -> IndexVec<ScopedMemberId, InternedPlaceStateId> {
1690+
let mut interned_ids_by_member: IndexVec<ScopedMemberId, InternedPlaceStateId> =
16911691
IndexVec::with_capacity(end_of_scope_members.len());
1692-
let mut interned_ids_by_place_state: FxHashMap<PlaceState, ScopedPlaceStateId> =
1692+
let mut interned_ids_by_place_state: FxHashMap<PlaceState, InternedPlaceStateId> =
16931693
FxHashMap::with_capacity_and_hasher(end_of_scope_members.len(), FxBuildHasher);
16941694

16951695
for place_state in end_of_scope_members {
@@ -1718,7 +1718,7 @@ impl<'db> UseDefMapBuilder<'db> {
17181718
.insert(place_state.declarations().clone(), declarations_id);
17191719
declarations_id
17201720
};
1721-
let place_state_id = ScopedPlaceStateId(bindings_id, declarations_id);
1721+
let place_state_id = InternedPlaceStateId(bindings_id, declarations_id);
17221722
interned_ids_by_place_state.insert(place_state, place_state_id);
17231723
place_state_id
17241724
};
@@ -1731,12 +1731,12 @@ impl<'db> UseDefMapBuilder<'db> {
17311731

17321732
fn intern_enclosing_snapshots(
17331733
enclosing_snapshots: EnclosingSnapshots,
1734-
interned_bindings: &mut IndexVec<ScopedBindingsId, Bindings>,
1735-
interned_ids_by_bindings: &mut FxHashMap<Bindings, ScopedBindingsId>,
1736-
) -> IndexVec<ScopedEnclosingSnapshotId, ScopedEnclosingSnapshotDataId> {
1734+
interned_bindings: &mut IndexVec<InternedBindingsId, Bindings>,
1735+
interned_ids_by_bindings: &mut FxHashMap<Bindings, InternedBindingsId>,
1736+
) -> IndexVec<ScopedEnclosingSnapshotId, InternedEnclosingSnapshotId> {
17371737
let mut interned_ids_by_snapshot: IndexVec<
17381738
ScopedEnclosingSnapshotId,
1739-
ScopedEnclosingSnapshotDataId,
1739+
InternedEnclosingSnapshotId,
17401740
> = IndexVec::with_capacity(enclosing_snapshots.len());
17411741

17421742
for snapshot in enclosing_snapshots {
@@ -1750,10 +1750,10 @@ impl<'db> UseDefMapBuilder<'db> {
17501750
interned_ids_by_bindings.insert(bindings, interned_id);
17511751
interned_id
17521752
};
1753-
ScopedEnclosingSnapshotDataId::Bindings(interned_bindings_id)
1753+
InternedEnclosingSnapshotId::Bindings(interned_bindings_id)
17541754
}
17551755
EnclosingSnapshot::Constraint(constraint) => {
1756-
ScopedEnclosingSnapshotDataId::Constraint(constraint)
1756+
InternedEnclosingSnapshotId::Constraint(constraint)
17571757
}
17581758
};
17591759
interned_ids_by_snapshot.push(interned_id);

0 commit comments

Comments
 (0)