Skip to content

Commit

Permalink
[red-knot] refactor CFG outside of symbol table (#11746)
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm authored Jun 5, 2024
1 parent 2e0a975 commit 895eb3e
Show file tree
Hide file tree
Showing 12 changed files with 1,582 additions and 1,433 deletions.
6 changes: 3 additions & 3 deletions crates/red_knot/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::files::FileId;
use crate::lint::{LintSemanticStorage, LintSyntaxStorage};
use crate::module::ModuleResolver;
use crate::parse::ParsedStorage;
use crate::semantic::SemanticIndexStorage;
use crate::semantic::TypeStore;
use crate::source::SourceStorage;
use crate::symbols::SymbolTablesStorage;
use crate::types::TypeStore;

mod jars;
mod query;
Expand Down Expand Up @@ -125,7 +125,7 @@ pub struct SourceJar {
#[derive(Debug, Default)]
pub struct SemanticJar {
pub module_resolver: ModuleResolver,
pub symbol_tables: SymbolTablesStorage,
pub semantic_indices: SemanticIndexStorage,
pub type_store: TypeStore,
}

Expand Down
3 changes: 1 addition & 2 deletions crates/red_knot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ pub mod lint;
pub mod module;
mod parse;
pub mod program;
mod semantic;
pub mod source;
mod symbols;
mod types;
pub mod watch;

pub(crate) type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
Expand Down
22 changes: 11 additions & 11 deletions crates/red_knot/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::db::{LintDb, LintJar, QueryResult};
use crate::files::FileId;
use crate::module::{resolve_module, ModuleName};
use crate::parse::parse;
use crate::source::{source_text, Source};
use crate::symbols::{
resolve_global_symbol, symbol_table, Definition, GlobalSymbolId, SymbolId, SymbolTable,
use crate::semantic::{infer_definition_type, infer_symbol_public_type, Type};
use crate::semantic::{
resolve_global_symbol, semantic_index, Definition, GlobalSymbolId, SemanticIndex, SymbolId,
};
use crate::types::{infer_definition_type, infer_symbol_public_type, Type};
use crate::source::{source_text, Source};

#[tracing::instrument(level = "debug", skip(db))]
pub(crate) fn lint_syntax(db: &dyn LintDb, file_id: FileId) -> QueryResult<Diagnostics> {
Expand Down Expand Up @@ -82,13 +82,13 @@ pub(crate) fn lint_semantic(db: &dyn LintDb, file_id: FileId) -> QueryResult<Dia
storage.get(&file_id, |file_id| {
let source = source_text(db.upcast(), *file_id)?;
let parsed = parse(db.upcast(), *file_id)?;
let symbols = symbol_table(db.upcast(), *file_id)?;
let semantic_index = semantic_index(db.upcast(), *file_id)?;

let context = SemanticLintContext {
file_id: *file_id,
source,
parsed: &parsed,
symbols,
semantic_index,
db,
diagnostics: RefCell::new(Vec::new()),
};
Expand All @@ -102,7 +102,7 @@ pub(crate) fn lint_semantic(db: &dyn LintDb, file_id: FileId) -> QueryResult<Dia

fn lint_unresolved_imports(context: &SemanticLintContext) -> QueryResult<()> {
// TODO: Consider iterating over the dependencies (imports) only instead of all definitions.
for (symbol, definition) in context.symbols().all_definitions() {
for (symbol, definition) in context.semantic_index().symbol_table().all_definitions() {
match definition {
Definition::Import(import) => {
let ty = context.infer_symbol_public_type(symbol)?;
Expand Down Expand Up @@ -152,7 +152,7 @@ fn lint_bad_overrides(context: &SemanticLintContext) -> QueryResult<()> {

// TODO we should maybe index definitions by type instead of iterating all, or else iterate all
// just once, match, and branch to all lint rules that care about a type of definition
for (symbol, definition) in context.symbols().all_definitions() {
for (symbol, definition) in context.semantic_index().symbol_table().all_definitions() {
if !matches!(definition, Definition::FunctionDef(_)) {
continue;
}
Expand Down Expand Up @@ -194,7 +194,7 @@ pub struct SemanticLintContext<'a> {
file_id: FileId,
source: Source,
parsed: &'a Parsed<ModModule>,
symbols: Arc<SymbolTable>,
semantic_index: Arc<SemanticIndex>,
db: &'a dyn LintDb,
diagnostics: RefCell<Vec<String>>,
}
Expand All @@ -212,8 +212,8 @@ impl<'a> SemanticLintContext<'a> {
self.parsed.syntax()
}

pub fn symbols(&self) -> &SymbolTable {
&self.symbols
pub fn semantic_index(&self) -> &SemanticIndex {
&self.semantic_index
}

pub fn infer_symbol_public_type(&self, symbol_id: SymbolId) -> QueryResult<Type> {
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use smol_str::SmolStr;

use crate::db::{QueryResult, SemanticDb, SemanticJar};
use crate::files::FileId;
use crate::symbols::Dependency;
use crate::semantic::Dependency;
use crate::FxDashMap;

/// Representation of a Python module.
Expand Down Expand Up @@ -697,7 +697,7 @@ mod tests {
path_to_module, resolve_module, set_module_search_paths, ModuleKind, ModuleName,
ModuleSearchPath, ModuleSearchPathKind,
};
use crate::symbols::Dependency;
use crate::semantic::Dependency;

struct TestCase {
temp_dir: tempfile::TempDir,
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot/src/program/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::files::FileId;
use crate::lint::{lint_semantic, lint_syntax, Diagnostics};
use crate::module::{file_to_module, resolve_module};
use crate::program::Program;
use crate::symbols::{symbol_table, Dependency};
use crate::semantic::{semantic_index, Dependency};

impl Program {
/// Checks all open files in the workspace and its dependencies.
Expand All @@ -28,8 +28,8 @@ impl Program {
fn check_file(&self, file: FileId, context: &CheckFileContext) -> QueryResult<Diagnostics> {
self.cancelled()?;

let symbol_table = symbol_table(self, file)?;
let dependencies = symbol_table.dependencies();
let index = semantic_index(self, file)?;
let dependencies = index.symbol_table().dependencies();

if !dependencies.is_empty() {
let module = file_to_module(self, file)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Program {
let (source, semantic, lint) = self.jars_mut();
for change in aggregated_changes.iter() {
semantic.module_resolver.remove_module_by_file(change.id);
semantic.symbol_tables.remove(&change.id);
semantic.semantic_indices.remove(&change.id);
source.sources.remove(&change.id);
source.parsed.remove(&change.id);
// TODO: remove all dependent modules as well
Expand Down
Loading

0 comments on commit 895eb3e

Please sign in to comment.