pub struct SemanticIndex<'db> { /* private fields */ }Expand description
The place tables and use-def maps for all scopes in a file.
Implementations§
Source§impl<'db> SemanticIndex<'db>
impl<'db> SemanticIndex<'db>
Sourcepub fn place_table(&self, scope_id: FileScopeId) -> &PlaceTable
pub fn place_table(&self, scope_id: FileScopeId) -> &PlaceTable
Returns the place table for a specific scope.
Use the Salsa cached place_table() query if you only need the
place table for a single scope.
Sourcepub fn narrowing_alias_predicate(
&self,
key: impl Into<ExpressionNodeKey>,
) -> Option<&NarrowingAliasPredicate<'db>>
pub fn narrowing_alias_predicate( &self, key: impl Into<ExpressionNodeKey>, ) -> Option<&NarrowingAliasPredicate<'db>>
Returns alias metadata for an alias Name node in a predicate, if one exists.
Sourcepub fn use_def_map(&self, scope_id: FileScopeId) -> &UseDefMap<'db>
pub fn use_def_map(&self, scope_id: FileScopeId) -> &UseDefMap<'db>
Returns the use-def map for a specific scope.
Use the Salsa cached use_def_map() query if you only need the
use-def map for a single scope.
Sourcepub fn imported_modules(&self) -> impl Iterator<Item = &ModuleName>
pub fn imported_modules(&self) -> impl Iterator<Item = &ModuleName>
Returns the set of modules that are imported anywhere in this file.
This set only considers import statements, not from...import statements.
See ModuleLiteralType::available_submodule_attributes for discussion
of why this analysis is intentionally limited.
Sourcepub fn expression_scope_id<E>(&self, expression: &E) -> FileScopeIdwhere
E: HasTrackedScope,
pub fn expression_scope_id<E>(&self, expression: &E) -> FileScopeIdwhere
E: HasTrackedScope,
Returns the ID of the expression’s enclosing scope.
Sourcepub fn try_expression_scope_id<E>(&self, expression: &E) -> Option<FileScopeId>where
E: HasTrackedScope,
pub fn try_expression_scope_id<E>(&self, expression: &E) -> Option<FileScopeId>where
E: HasTrackedScope,
Returns the ID of the expression’s enclosing scope.
Sourcepub fn expression_scope(&self, expression: &impl HasTrackedScope) -> &Scope
pub fn expression_scope(&self, expression: &impl HasTrackedScope) -> &Scope
Returns the Scope of the expression’s enclosing scope.
Sourcepub fn scope(&self, id: FileScopeId) -> &Scope
pub fn scope(&self, id: FileScopeId) -> &Scope
Returns the Scope with the given id.
pub fn scope_ids(&self) -> impl Iterator<Item = ScopeId<'db>> + '_
pub fn symbol_is_global_in_scope( &self, symbol: ScopedSymbolId, scope: FileScopeId, ) -> bool
Sourcepub fn symbol_resolves_to_global_scope(
&self,
symbol: ScopedSymbolId,
scope: FileScopeId,
) -> bool
pub fn symbol_resolves_to_global_scope( &self, symbol: ScopedSymbolId, scope: FileScopeId, ) -> bool
Returns true if the given symbol in the given scope resolves to the global scope, either
because:
- The given scope is the global scope.
- The symbol is explicitly declared
globalin the given scope. - The symbol is a free variable in the given scope, and no enclosing function scope defines it.
The third case requires walking ancestor scopes until we encounter either a binding or a
nonlocal declaration (those aren’t allowed to resolve to the global scope, so we don’t
need to chase them).
Sourcepub fn parent_scope_id(&self, scope_id: FileScopeId) -> Option<FileScopeId>
pub fn parent_scope_id(&self, scope_id: FileScopeId) -> Option<FileScopeId>
Returns the id of the parent scope.
Sourcepub fn parent_scope(&self, scope_id: FileScopeId) -> Option<&Scope>
pub fn parent_scope(&self, scope_id: FileScopeId) -> Option<&Scope>
Returns the parent scope of scope_id.
Sourcepub fn class_definition_of_method(
&self,
function_body_scope: FileScopeId,
) -> Option<Definition<'db>>
pub fn class_definition_of_method( &self, function_body_scope: FileScopeId, ) -> Option<Definition<'db>>
Return the Definition of the class enclosing this method, given the
method’s body scope, or None if it is not a method.
pub fn enclosing_lambda_statement( &self, lambda: ExpressionNodeKey, ) -> Option<Statement<'db>>
Sourcepub fn unannotated_collection_initializer(
&self,
collection_use: &Expr,
) -> Option<Definition<'db>>
pub fn unannotated_collection_initializer( &self, collection_use: &Expr, ) -> Option<Definition<'db>>
If this is a potentially constraining use of an unannotated collection initializer, returns its definition.
Sourcepub fn constraining_collection_uses(
&self,
collection_def: Definition<'db>,
) -> impl Iterator<Item = (Statement<'db>, ExpressionNodeKey)>
pub fn constraining_collection_uses( &self, collection_def: Definition<'db>, ) -> impl Iterator<Item = (Statement<'db>, ExpressionNodeKey)>
Returns all potentially constraining uses of the given unannotated collection initializer.
pub fn is_in_type_checking_block( &self, scope_id: FileScopeId, range: TextRange, ) -> bool
Sourcepub fn child_scopes(&self, scope: FileScopeId) -> ChildrenIter<'_> ⓘ
pub fn child_scopes(&self, scope: FileScopeId) -> ChildrenIter<'_> ⓘ
Returns an iterator over the direct child scopes of scope.
Sourcepub fn ancestor_scopes(&self, scope: FileScopeId) -> AncestorsIter<'_> ⓘ
pub fn ancestor_scopes(&self, scope: FileScopeId) -> AncestorsIter<'_> ⓘ
Returns an iterator over all ancestors of scope, starting with scope itself.
Sourcepub fn visible_ancestor_scopes(
&self,
scope: FileScopeId,
) -> VisibleAncestorsIter<'_> ⓘ
pub fn visible_ancestor_scopes( &self, scope: FileScopeId, ) -> VisibleAncestorsIter<'_> ⓘ
Returns an iterator over ancestors of scope that are visible for name resolution,
starting with scope itself. This follows Python’s lexical scoping rules where
class scopes are skipped during name resolution (except for the starting scope
if it happens to be a class scope).
For example, in this code:
x = 1
class A:
x = 2
def method(self):
print(x) # Refers to global x=1, not class x=2The method function can see the global scope but not the class scope.
Sourcepub fn definitions(
&self,
definition_key: impl Into<DefinitionNodeKey>,
) -> &[Definition<'db>]
pub fn definitions( &self, definition_key: impl Into<DefinitionNodeKey>, ) -> &[Definition<'db>]
Returns the definition::Definition salsa ingredient(s) for definition_key.
There will only ever be >1 Definition associated with a definition_key
if the definitions are created by a wildcard (*) import.
Sourcepub fn try_definitions(
&self,
definition_node: AnyNodeRef<'_>,
) -> Option<&[Definition<'db>]>
pub fn try_definitions( &self, definition_node: AnyNodeRef<'_>, ) -> Option<&[Definition<'db>]>
Returns the definition::Definition salsa ingredient(s) for definition_node, if any.
Sourcepub fn expect_single_definition(
&self,
definition_key: impl Into<DefinitionNodeKey> + Debug + Copy,
) -> Definition<'db>
pub fn expect_single_definition( &self, definition_key: impl Into<DefinitionNodeKey> + Debug + Copy, ) -> Definition<'db>
Returns the definition::Definition salsa ingredient for definition_key.
§Panics
If the number of definitions associated with the key is not exactly 1 and
the debug_assertions feature is enabled, this method will panic.
It is generally safe to use this method for any AST node that does not
correspond to a * (wildcard) import, since those are the only situations
that can result in multiple definitions being associated with a single AST
node.
pub fn try_definition( &self, definition_key: impl Into<DefinitionNodeKey>, ) -> Option<Definition<'db>>
Sourcepub fn expression(
&self,
expression_key: impl Into<ExpressionNodeKey>,
) -> Expression<'db>
pub fn expression( &self, expression_key: impl Into<ExpressionNodeKey>, ) -> Expression<'db>
Returns the Expression ingredient for an expression node.
Panics if we have no expression ingredient for that node. We can only call this method for
standalone-inferable expressions, which we call add_standalone_expression for in
SemanticIndexBuilder.
pub fn try_expression( &self, expression_key: impl Into<ExpressionNodeKey>, ) -> Option<Expression<'db>>
Sourcepub fn try_unpack(
&self,
target: impl Into<ExpressionNodeKey>,
) -> Option<Unpack<'db>>
pub fn try_unpack( &self, target: impl Into<ExpressionNodeKey>, ) -> Option<Unpack<'db>>
Returns the unpack::Unpack ingredient for an unpacking target, if any.
pub fn is_standalone_expression( &self, expression_key: impl Into<ExpressionNodeKey>, ) -> bool
pub fn try_statement( &self, statement_key: impl Into<StatementNodeKey>, ) -> Option<Statement<'db>>
Sourcepub fn node_scope(&self, node: NodeWithScopeRef<'_>) -> FileScopeId
pub fn node_scope(&self, node: NodeWithScopeRef<'_>) -> FileScopeId
Returns the id of the scope that node creates.
This is different from definition::Definition::scope which
returns the scope in which that definition is defined in.
Sourcepub fn try_node_scope(&self, node: NodeWithScopeRef<'_>) -> Option<FileScopeId>
pub fn try_node_scope(&self, node: NodeWithScopeRef<'_>) -> Option<FileScopeId>
Returns the id of the scope that node creates, if it exists.
Sourcepub fn node_scope_by_key(&self, key: NodeWithScopeKey) -> FileScopeId
pub fn node_scope_by_key(&self, key: NodeWithScopeKey) -> FileScopeId
Returns the id of the scope that the node identified by key creates.
This is useful when you have a NodeWithScopeKey constructed from an
AstNodeRef and want to avoid loading
the parsed module just to look up the scope.
Sourcepub fn has_future_annotations(&self) -> bool
pub fn has_future_annotations(&self) -> bool
Checks if there is an import of __future__.annotations in the global scope, which affects
the logic for type inference.
Sourcepub fn enclosing_snapshot(
&self,
enclosing_scope: FileScopeId,
expr: PlaceExprRef<'_>,
nested_scope: FileScopeId,
) -> EnclosingSnapshotResult<'_, 'db>
pub fn enclosing_snapshot( &self, enclosing_scope: FileScopeId, expr: PlaceExprRef<'_>, nested_scope: FileScopeId, ) -> EnclosingSnapshotResult<'_, 'db>
Returns
NoLongerInEagerContextif the nested scope is no longer in an eager context (that is, not every scope that will be traversed is eager) and no lazy snapshots were found.- an iterator of bindings for a particular nested scope reference if the bindings exist.
- a narrowing constraint if there are no bindings, but there is a narrowing constraint for an enclosing scope place.
NotFoundif the narrowing constraint / bindings do not exist in the nested scope.
pub fn semantic_syntax_errors(&self) -> &[SemanticSyntaxError]
Trait Implementations§
Source§impl<'db> Debug for SemanticIndex<'db>
impl<'db> Debug for SemanticIndex<'db>
Source§impl<'db> GetSize for SemanticIndex<'db>
impl<'db> GetSize for SemanticIndex<'db>
Source§fn get_heap_size(&self) -> usize
fn get_heap_size(&self) -> usize
Source§fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>(
&self,
tracker: TRACKER,
) -> (usize, TRACKER)
fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)
tracker. Read moreSource§fn get_stack_size() -> usize
fn get_stack_size() -> usize
Source§fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
tracker. Read moreimpl<'db> SalsaValue for SemanticIndex<'db>
Auto Trait Implementations§
impl<'db> Freeze for SemanticIndex<'db>
impl<'db> RefUnwindSafe for SemanticIndex<'db>
impl<'db> Send for SemanticIndex<'db>
impl<'db> Sync for SemanticIndex<'db>
impl<'db> Unpin for SemanticIndex<'db>
impl<'db> UnsafeUnpin for SemanticIndex<'db>
impl<'db> UnwindSafe for SemanticIndex<'db>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.