Skip to main content

SemanticIndex

Struct SemanticIndex 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn expression_scope_id<E>(&self, expression: &E) -> FileScopeId
where E: HasTrackedScope,

Returns the ID of the expression’s enclosing scope.

Source

pub fn try_expression_scope_id<E>(&self, expression: &E) -> Option<FileScopeId>
where E: HasTrackedScope,

Returns the ID of the expression’s enclosing scope.

Source

pub fn expression_scope(&self, expression: &impl HasTrackedScope) -> &Scope

Returns the Scope of the expression’s enclosing scope.

Source

pub fn scope(&self, id: FileScopeId) -> &Scope

Returns the Scope with the given id.

Source

pub fn scope_ids(&self) -> impl Iterator<Item = ScopeId<'db>> + '_

Source

pub fn symbol_is_global_in_scope( &self, symbol: ScopedSymbolId, scope: FileScopeId, ) -> bool

Source

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:

  1. The given scope is the global scope.
  2. The symbol is explicitly declared global in the given scope.
  3. 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).

Source

pub fn parent_scope_id(&self, scope_id: FileScopeId) -> Option<FileScopeId>

Returns the id of the parent scope.

Source

pub fn parent_scope(&self, scope_id: FileScopeId) -> Option<&Scope>

Returns the parent scope of scope_id.

Source

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.

Source

pub fn enclosing_lambda_statement( &self, lambda: ExpressionNodeKey, ) -> Option<Statement<'db>>

Source

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.

Source

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.

Source

pub fn is_in_type_checking_block( &self, scope_id: FileScopeId, range: TextRange, ) -> bool

Source

pub fn child_scopes(&self, scope: FileScopeId) -> ChildrenIter<'_>

Returns an iterator over the direct child scopes of scope.

Source

pub fn ancestor_scopes(&self, scope: FileScopeId) -> AncestorsIter<'_>

Returns an iterator over all ancestors of scope, starting with scope itself.

Source

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=2

The method function can see the global scope but not the class scope.

Source

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.

Source

pub fn try_definitions( &self, definition_node: AnyNodeRef<'_>, ) -> Option<&[Definition<'db>]>

Returns the definition::Definition salsa ingredient(s) for definition_node, if any.

Source

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.

Source

pub fn try_definition( &self, definition_key: impl Into<DefinitionNodeKey>, ) -> Option<Definition<'db>>

Source

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.

Source

pub fn try_expression( &self, expression_key: impl Into<ExpressionNodeKey>, ) -> Option<Expression<'db>>

Source

pub fn try_unpack( &self, target: impl Into<ExpressionNodeKey>, ) -> Option<Unpack<'db>>

Returns the unpack::Unpack ingredient for an unpacking target, if any.

Source

pub fn is_standalone_expression( &self, expression_key: impl Into<ExpressionNodeKey>, ) -> bool

Source

pub fn try_statement( &self, statement_key: impl Into<StatementNodeKey>, ) -> Option<Statement<'db>>

Source

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.

Source

pub fn try_node_scope(&self, node: NodeWithScopeRef<'_>) -> Option<FileScopeId>

Returns the id of the scope that node creates, if it exists.

Source

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.

Source

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.

Source

pub fn enclosing_snapshot( &self, enclosing_scope: FileScopeId, expr: PlaceExprRef<'_>, nested_scope: FileScopeId, ) -> EnclosingSnapshotResult<'_, 'db>

Returns

  • NoLongerInEagerContext if 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.
  • NotFound if the narrowing constraint / bindings do not exist in the nested scope.
Source

pub fn semantic_syntax_errors(&self) -> &[SemanticSyntaxError]

Trait Implementations§

Source§

impl<'db> Debug for SemanticIndex<'db>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'db> GetSize for SemanticIndex<'db>

Source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
Source§

fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)

Determines how many bytes this object occupies inside the heap while using a tracker. Read more
Source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
Source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
Source§

fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)
where T: GetSizeTracker,

Determines the total size of the object while using a tracker. Read more
Source§

impl<'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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Lookup<T> for T

Source§

fn into_owned(self) -> T

Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows 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
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows 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
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .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
where Self: BorrowMut<B>, B: ?Sized,

Calls .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
where Self: AsRef<R>, R: ?Sized,

Calls .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
where Self: AsMut<R>, R: ?Sized,

Calls .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
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more