Move binding accesses into SemanticModel method (#5084)

This commit is contained in:
Charlie Marsh 2023-06-14 10:07:46 -04:00 committed by GitHub
parent 1e497162d1
commit c74ef77e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 53 additions and 54 deletions

View file

@ -198,15 +198,10 @@ impl nohash_hasher::IsEnabled for BindingId {}
pub struct Bindings<'a>(IndexVec<BindingId, Binding<'a>>);
impl<'a> Bindings<'a> {
/// Pushes a new binding and returns its id
/// Pushes a new [`Binding`] and returns its [`BindingId`].
pub fn push(&mut self, binding: Binding<'a>) -> BindingId {
self.0.push(binding)
}
/// Returns the id that will be assigned when pushing the next binding
pub fn next_id(&self) -> BindingId {
self.0.next_index()
}
}
impl<'a> Deref for Bindings<'a> {

View file

@ -106,6 +106,18 @@ impl<'a> SemanticModel<'a> {
}
}
/// Return the [`Binding`] for the given [`BindingId`].
#[inline]
pub fn binding(&self, id: BindingId) -> &Binding {
&self.bindings[id]
}
/// Resolve the [`Reference`] for the given [`ReferenceId`].
#[inline]
pub fn reference(&self, id: ReferenceId) -> &Reference {
&self.references[id]
}
/// Return `true` if the `Expr` is a reference to `typing.${target}`.
pub fn match_typing_expr(&self, expr: &Expr, target: &str) -> bool {
self.resolve_call_path(expr).map_or(false, |call_path| {
@ -717,11 +729,6 @@ impl<'a> SemanticModel<'a> {
self.bindings[binding_id].references.push(reference_id);
}
/// Resolve a [`ReferenceId`].
pub fn reference(&self, reference_id: ReferenceId) -> &Reference {
self.references.resolve(reference_id)
}
/// Return the [`ExecutionContext`] of the current scope.
pub const fn execution_context(&self) -> ExecutionContext {
if self.in_type_checking_block()

View file

@ -1,6 +1,7 @@
use ruff_text_size::TextRange;
use std::ops::Deref;
use ruff_index::{newtype_index, IndexVec};
use ruff_index::{newtype_index, IndexSlice, IndexVec};
use crate::context::ExecutionContext;
use crate::scope::ScopeId;
@ -38,7 +39,7 @@ pub struct ReferenceId;
pub struct References(IndexVec<ReferenceId, Reference>);
impl References {
/// Pushes a new read reference and returns its unique id.
/// Pushes a new [`Reference`] and returns its [`ReferenceId`].
pub fn push(
&mut self,
scope_id: ScopeId,
@ -51,9 +52,12 @@ impl References {
context,
})
}
}
/// Returns the [`Reference`] with the given id.
pub fn resolve(&self, id: ReferenceId) -> &Reference {
&self.0[id]
impl Deref for References {
type Target = IndexSlice<ReferenceId, Reference>;
fn deref(&self) -> &Self::Target {
&self.0
}
}