Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/semantic_index/use_def.rs (Rust)

Code before:
    ///
    /// If the definition is both a declaration and a binding -- `x: int = 1` for example -- then
    /// we don't actually need anything here, all we'll need to validate is that our own RHS is a
    /// valid assignment to our own annotation.
    declarations_by_binding: FxHashMap<Definition<'db>, Declarations>,

    /// If the definition is a declaration (only) -- `x: int` for example -- then we need
    /// [`Bindings`] to know whether this declaration is consistent with the previously
    /// inferred type.
    ///
    /// If the definition is both a declaration and a binding -- `x: int = 1` for example -- then
    /// we don't actually need anything here, all we'll need to validate is that our own RHS is a
    /// valid assignment to our own annotation.
    ///
    /// If we see a binding to a `Final`-qualified symbol, we also need this map to find previous
    /// bindings to that symbol. If there are any, the assignment is invalid.
    bindings_by_definition: FxHashMap<Definition<'db>, Bindings>,

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

    /// [`PlaceState`] visible at end of scope for each member.
    end_of_scope_members: IndexVec<ScopedMemberId, PlaceState>,

    /// All potentially reachable bindings and declarations, for each symbol.
    reachable_definitions_by_symbol: IndexVec<ScopedSymbolId, ReachableDefinitions>,

    /// All potentially reachable bindings and declarations, for each member.
    reachable_definitions_by_member: IndexVec<ScopedMemberId, ReachableDefinitions>,

    /// Snapshot of bindings in this scope that can be used to resolve a reference in a nested
    /// scope.
    enclosing_snapshots: EnclosingSnapshots,

    /// Whether or not the end of the scope is reachable.
    ///
    /// This is used to check if the function can implicitly return `None`.
    /// For example:
    /// ```py
    /// def f(cond: bool) -> int | None:
    ///     if cond:
    ///        return 1
    ///
    /// def g() -> int:
    ///     if True:
    ///        return 1
    /// ```
    ///
    /// Function `f` may implicitly return `None`, but `g` cannot.
    ///
    /// This is used by [`UseDefMap::can_implicitly_return_none`].

Review comment (performance):
I like how you chose to only do the interning in the final `UseDefMap`. An alternative would have been to build up the interned values in `UseDefMapBuilder` as bindings/etc are created. But that would add complexity to the building code, and would only be _needed_ if you needed the intern IDs as references, to store in other bits of state. We don't, and so this approach is simpler. Very nice!

Code after:
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, salsa::Update, get_size2::GetSize)]
enum InternedEnclosingSnapshotId {
    Constraint(ScopedNarrowingConstraint),
    Bindings(InternedBindingsId),
}

/// Applicable definitions and constraints for every use of a name.
#[derive(Debug, PartialEq, Eq, salsa::Update, get_size2::GetSize)]
pub(crate) struct UseDefMap<'db> {
    /// Array of [`Definition`] in this scope. Only the first entry should be [`DefinitionState::Undefined`];
    /// this represents the implicit "unbound"/"undeclared" definition of every place.
    all_definitions: IndexVec<ScopedDefinitionId, DefinitionState<'db>>,

    /// Array of predicates in this scope.
    predicates: Predicates<'db>,

    /// Array of reachability constraints in this scope.
    reachability_constraints: ReachabilityConstraints,

    /// Interned [`Bindings`] values.
    interned_bindings: IndexVec<InternedBindingsId, Bindings>,
    /// Interned [`Declarations`] values.
    interned_declarations: IndexVec<InternedDeclarationsId, Declarations>,

    /// [`Bindings`] reaching a [`ScopedUseId`].
    bindings_by_use: IndexVec<ScopedUseId, InternedBindingsId>,

    /// Tracks whether or not a given AST node is reachable from the start of the scope.
    node_reachability: FxHashMap<NodeKey, ScopedReachabilityConstraintId>,

    /// If the definition is a binding (only) -- `x = 1` for example -- then we need
    /// [`Declarations`] to know whether this binding is permitted by the live declarations.
    ///
    /// If the definition is both a declaration and a binding -- `x: int = 1` for example -- then
    /// we don't actually need anything here, all we'll need to validate is that our own RHS is a
    /// valid assignment to our own annotation.
    declarations_by_binding: FxHashMap<Definition<'db>, InternedDeclarationsId>,

    /// If the definition is a declaration (only) -- `x: int` for example -- then we need
    /// [`Bindings`] to know whether this declaration is consistent with the previously
    /// inferred type.
    ///
    /// If the definition is both a declaration and a binding -- `x: int = 1` for example -- then
    /// we don't actually need anything here, all we'll need to validate is that our own RHS is a
    /// valid assignment to our own annotation.
    ///
    /// If we see a binding to a `Final`-qualified symbol, we also need this map to find previous
    /// bindings to that symbol. If there are any, the assignment is invalid.