Skip to content

Commit

Permalink
Implement HasTy for Alias
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 22, 2024
1 parent 89405d8 commit 86ec41f
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 267 deletions.
39 changes: 8 additions & 31 deletions crates/red_knot_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ruff_python_ast as ast;
use crate::node_key::NodeKey;
use crate::semantic_index::ast_ids::{AstId, AstIds, ScopedClassId, ScopedFunctionId};
use crate::semantic_index::builder::SemanticIndexBuilder;
use crate::semantic_index::definition::DefinitionNodeKey;
use crate::semantic_index::symbol::{
FileScopeId, PublicSymbolId, Scope, ScopeId, ScopedSymbolId, SymbolTable,
};
Expand Down Expand Up @@ -83,8 +84,11 @@ pub struct SemanticIndex {
/// an [`ast::Expr`] to an [`ExpressionId`] (which requires knowing the scope).
scopes_by_expression: FxHashMap<NodeKey, FileScopeId>,

/// Map from nodes that introduce a scope to the scope they define.
scopes_by_node: FxHashMap<NodeWithScopeKey, FileScopeId>,
/// Map from definitions to their scope. The scope depends on whether the definition introduces a scope or not.
///
/// * introduce a scope: The scope that the definition introduces
/// * other definitions: The enclosing scope
scopes_by_definition: FxHashMap<DefinitionNodeKey, FileScopeId>,

/// Lookup table to map between node ids and ast nodes.
///
Expand Down Expand Up @@ -158,9 +162,9 @@ impl SemanticIndex {

pub(crate) fn definition_scope(
&self,
node_with_scope: impl Into<NodeWithScopeKey>,
definition: impl Into<DefinitionNodeKey>
) -> FileScopeId {
self.scopes_by_node[&node_with_scope.into()]
self.scopes_by_definition[&definition.into()]
}
}

Expand Down Expand Up @@ -267,33 +271,6 @@ pub(crate) enum NodeWithScopeId {
FunctionTypeParams(AstId<ScopedFunctionId>),
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct NodeWithScopeKey(NodeKey);

impl From<&ast::StmtClassDef> for NodeWithScopeKey {
fn from(node: &ast::StmtClassDef) -> Self {
Self(NodeKey::from_node(node))
}
}

impl From<&ast::StmtFunctionDef> for NodeWithScopeKey {
fn from(value: &ast::StmtFunctionDef) -> Self {
Self(NodeKey::from_node(value))
}
}

impl From<&ast::TypeParams> for NodeWithScopeKey {
fn from(value: &ast::TypeParams) -> Self {
Self(NodeKey::from_node(value))
}
}

impl From<&ast::ModModule> for NodeWithScopeKey {
fn from(value: &ast::ModModule) -> Self {
Self(NodeKey::from_node(value))
}
}

#[cfg(test)]
mod tests {
use ruff_db::parsed::parsed_module;
Expand Down
Loading

0 comments on commit 86ec41f

Please sign in to comment.