Skip to content

Commit

Permalink
stop using 'root' term
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Jul 11, 2024
1 parent 3cbbd50 commit 865b92a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 82 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn lint_bad_override(context: &SemanticLintContext, class: &ast::StmtClassDef) {
return;
};

let override_ty = semantic.root_symbol_ty(&typing, "override");
let override_ty = semantic.module_global_symbol_ty(&typing, "override");

let Type::Class(class_ty) = class.ty(semantic) else {
return;
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_db::{Db as SourceDb, Upcast};
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::ScopeId;
use crate::semantic_index::usedef::Expression;
use crate::semantic_index::{root_scope, semantic_index, symbol_table, use_def_map};
use crate::semantic_index::{module_global_scope, semantic_index, symbol_table, use_def_map};
use crate::types::{
infer_definition_types, infer_expression_types, infer_scope_types, ClassType, FunctionType,
IntersectionType, UnionType,
Expand All @@ -23,7 +23,7 @@ pub struct Jar(
IntersectionType<'_>,
symbol_table,
use_def_map,
root_scope,
module_global_scope,
semantic_index,
infer_definition_types,
infer_expression_types,
Expand Down
124 changes: 71 additions & 53 deletions crates/red_knot_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ pub(crate) fn use_def_map<'db>(db: &'db dyn Db, scope: ScopeId<'db>) -> Arc<UseD
index.use_def_map(scope.file_scope_id(db))
}

/// Returns the root scope of `file`.
/// Returns the module global scope of `file`.
#[salsa::tracked]
pub(crate) fn root_scope(db: &dyn Db, file: File) -> ScopeId<'_> {
let _span = tracing::trace_span!("root_scope", ?file).entered();
pub(crate) fn module_global_scope(db: &dyn Db, file: File) -> ScopeId<'_> {
let _span = tracing::trace_span!("module_global_scope", ?file).entered();

FileScopeId::root().to_scope_id(db, file)
FileScopeId::module_global().to_scope_id(db, file)
}

/// The symbol tables and use-def maps for all scopes in a file.
Expand Down Expand Up @@ -289,7 +289,7 @@ mod tests {

use crate::db::tests::TestDb;
use crate::semantic_index::symbol::{FileScopeId, Scope, ScopeKind, SymbolTable};
use crate::semantic_index::{root_scope, semantic_index, symbol_table, use_def_map};
use crate::semantic_index::{module_global_scope, semantic_index, symbol_table, use_def_map};
use crate::Db;

struct TestCase {
Expand All @@ -316,38 +316,38 @@ mod tests {
#[test]
fn empty() {
let TestCase { db, file } = test_case("");
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

let root_names = names(&root_table);
let module_global_names = names(&module_global_table);

assert_eq!(root_names, Vec::<&str>::new());
assert_eq!(module_global_names, Vec::<&str>::new());
}

#[test]
fn simple() {
let TestCase { db, file } = test_case("x");
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

assert_eq!(names(&root_table), vec!["x"]);
assert_eq!(names(&module_global_table), vec!["x"]);
}

#[test]
fn annotation_only() {
let TestCase { db, file } = test_case("x: int");
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

assert_eq!(names(&root_table), vec!["int", "x"]);
assert_eq!(names(&module_global_table), vec!["int", "x"]);
// TODO record definition
}

#[test]
fn import() {
let TestCase { db, file } = test_case("import foo");
let scope = root_scope(&db, file);
let root_table = symbol_table(&db, scope);
let scope = module_global_scope(&db, file);
let module_global_table = symbol_table(&db, scope);

assert_eq!(names(&root_table), vec!["foo"]);
let foo = root_table.symbol_id_by_name("foo").unwrap();
assert_eq!(names(&module_global_table), vec!["foo"]);
let foo = module_global_table.symbol_id_by_name("foo").unwrap();

let use_def = use_def_map(&db, scope);
assert_eq!(use_def.public_definitions(foo).len(), 1);
Expand All @@ -356,28 +356,28 @@ mod tests {
#[test]
fn import_sub() {
let TestCase { db, file } = test_case("import foo.bar");
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

assert_eq!(names(&root_table), vec!["foo"]);
assert_eq!(names(&module_global_table), vec!["foo"]);
}

#[test]
fn import_as() {
let TestCase { db, file } = test_case("import foo.bar as baz");
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

assert_eq!(names(&root_table), vec!["baz"]);
assert_eq!(names(&module_global_table), vec!["baz"]);
}

#[test]
fn import_from() {
let TestCase { db, file } = test_case("from bar import foo");
let scope = root_scope(&db, file);
let root_table = symbol_table(&db, scope);
let scope = module_global_scope(&db, file);
let module_global_table = symbol_table(&db, scope);

assert_eq!(names(&root_table), vec!["foo"]);
assert_eq!(names(&module_global_table), vec!["foo"]);
assert!(
root_table
module_global_table
.symbol_by_name("foo")
.is_some_and(|symbol| { symbol.is_defined() && !symbol.is_used() }),
"symbols that are defined get the defined flag"
Expand All @@ -386,7 +386,11 @@ mod tests {
let use_def = use_def_map(&db, scope);
assert_eq!(
use_def
.public_definitions(root_table.symbol_id_by_name("foo").expect("symbol exists"))
.public_definitions(
module_global_table
.symbol_id_by_name("foo")
.expect("symbol exists")
)
.len(),
1
);
Expand All @@ -395,20 +399,24 @@ mod tests {
#[test]
fn assign() {
let TestCase { db, file } = test_case("x = foo");
let scope = root_scope(&db, file);
let root_table = symbol_table(&db, scope);
let scope = module_global_scope(&db, file);
let module_global_table = symbol_table(&db, scope);

assert_eq!(names(&root_table), vec!["foo", "x"]);
assert_eq!(names(&module_global_table), vec!["foo", "x"]);
assert!(
root_table
module_global_table
.symbol_by_name("foo")
.is_some_and(|symbol| { !symbol.is_defined() && symbol.is_used() }),
"a symbol used but not defined in a scope should have only the used flag"
);
let use_def = use_def_map(&db, scope);
assert_eq!(
use_def
.public_definitions(root_table.symbol_id_by_name("x").expect("symbol exists"))
.public_definitions(
module_global_table
.symbol_id_by_name("x")
.expect("symbol exists")
)
.len(),
1
);
Expand All @@ -423,13 +431,13 @@ class C:
y = 2
",
);
let root_table = symbol_table(&db, root_scope(&db, file));
let module_global_table = symbol_table(&db, module_global_scope(&db, file));

assert_eq!(names(&root_table), vec!["C", "y"]);
assert_eq!(names(&module_global_table), vec!["C", "y"]);

let index = semantic_index(&db, file);

let scopes: Vec<_> = index.child_scopes(FileScopeId::root()).collect();
let scopes: Vec<_> = index.child_scopes(FileScopeId::module_global()).collect();
assert_eq!(scopes.len(), 1);

let (class_scope_id, class_scope) = scopes[0];
Expand Down Expand Up @@ -459,11 +467,13 @@ y = 2
",
);
let index = semantic_index(&db, file);
let root_table = index.symbol_table(FileScopeId::root());
let module_global_table = index.symbol_table(FileScopeId::module_global());

assert_eq!(names(&root_table), vec!["func", "y"]);
assert_eq!(names(&module_global_table), vec!["func", "y"]);

let scopes = index.child_scopes(FileScopeId::root()).collect::<Vec<_>>();
let scopes = index
.child_scopes(FileScopeId::module_global())
.collect::<Vec<_>>();
assert_eq!(scopes.len(), 1);

let (function_scope_id, function_scope) = scopes[0];
Expand Down Expand Up @@ -497,10 +507,10 @@ def func():
",
);
let index = semantic_index(&db, file);
let root_table = index.symbol_table(FileScopeId::root());
let module_global_table = index.symbol_table(FileScopeId::module_global());

assert_eq!(names(&root_table), vec!["func"]);
let scopes: Vec<_> = index.child_scopes(FileScopeId::root()).collect();
assert_eq!(names(&module_global_table), vec!["func"]);
let scopes: Vec<_> = index.child_scopes(FileScopeId::module_global()).collect();
assert_eq!(scopes.len(), 2);

let (func_scope1_id, func_scope_1) = scopes[0];
Expand All @@ -517,10 +527,14 @@ def func():
assert_eq!(names(&func1_table), vec!["x"]);
assert_eq!(names(&func2_table), vec!["y"]);

let use_def = index.use_def_map(FileScopeId::root());
let use_def = index.use_def_map(FileScopeId::module_global());
assert_eq!(
use_def
.public_definitions(root_table.symbol_id_by_name("func").expect("symbol exists"))
.public_definitions(
module_global_table
.symbol_id_by_name("func")
.expect("symbol exists")
)
.len(),
1
);
Expand All @@ -536,11 +550,11 @@ def func[T]():
);

let index = semantic_index(&db, file);
let root_table = index.symbol_table(FileScopeId::root());
let module_global_table = index.symbol_table(FileScopeId::module_global());

assert_eq!(names(&root_table), vec!["func"]);
assert_eq!(names(&module_global_table), vec!["func"]);

let scopes: Vec<_> = index.child_scopes(FileScopeId::root()).collect();
let scopes: Vec<_> = index.child_scopes(FileScopeId::module_global()).collect();
assert_eq!(scopes.len(), 1);
let (ann_scope_id, ann_scope) = scopes[0];

Expand Down Expand Up @@ -568,11 +582,11 @@ class C[T]:
);

let index = semantic_index(&db, file);
let root_table = index.symbol_table(FileScopeId::root());
let module_global_table = index.symbol_table(FileScopeId::module_global());

assert_eq!(names(&root_table), vec!["C"]);
assert_eq!(names(&module_global_table), vec!["C"]);

let scopes: Vec<_> = index.child_scopes(FileScopeId::root()).collect();
let scopes: Vec<_> = index.child_scopes(FileScopeId::module_global()).collect();

assert_eq!(scopes.len(), 1);
let (ann_scope_id, ann_scope) = scopes[0];
Expand Down Expand Up @@ -604,7 +618,7 @@ class C[T]:
// let index = SemanticIndex::from_ast(ast);
// let table = &index.symbol_table;
// let x_sym = table
// .root_symbol_id_by_name("x")
// .module_global_symbol_id_by_name("x")
// .expect("x symbol should exist");
// let ast::Stmt::Expr(ast::StmtExpr { value: x_use, .. }) = &ast.body[1] else {
// panic!("should be an expr")
Expand Down Expand Up @@ -642,7 +656,7 @@ class C[T]:
let x = &x_stmt.targets[0];

assert_eq!(index.expression_scope(x).kind(), ScopeKind::Module);
assert_eq!(index.expression_scope_id(x), FileScopeId::root());
assert_eq!(index.expression_scope_id(x), FileScopeId::module_global());

let def = ast.body[1].as_function_def_stmt().unwrap();
let y_stmt = def.body[0].as_assign_stmt().unwrap();
Expand Down Expand Up @@ -679,24 +693,28 @@ def x():

let index = semantic_index(&db, file);

let descendents = index.descendent_scopes(FileScopeId::root());
let descendents = index.descendent_scopes(FileScopeId::module_global());
assert_eq!(
scope_names(descendents, &db, file),
vec!["Test", "foo", "bar", "baz", "x"]
);

let children = index.child_scopes(FileScopeId::root());
let children = index.child_scopes(FileScopeId::module_global());
assert_eq!(scope_names(children, &db, file), vec!["Test", "x"]);

let test_class = index.child_scopes(FileScopeId::root()).next().unwrap().0;
let test_class = index
.child_scopes(FileScopeId::module_global())
.next()
.unwrap()
.0;
let test_child_scopes = index.child_scopes(test_class);
assert_eq!(
scope_names(test_child_scopes, &db, file),
vec!["foo", "baz"]
);

let bar_scope = index
.descendent_scopes(FileScopeId::root())
.descendent_scopes(FileScopeId::module_global())
.nth(2)
.unwrap()
.0;
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/semantic_index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ impl<'db> ScopeId<'db> {
pub struct FileScopeId;

impl FileScopeId {
/// Returns the scope id of the Root scope.
pub fn root() -> Self {
/// Returns the scope id of the module-global scope.
pub fn module_global() -> Self {
FileScopeId::from_u32(0)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot_python_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ruff_python_ast::{Expr, ExpressionRef, StmtClassDef};

use crate::semantic_index::ast_ids::HasScopedAstId;
use crate::semantic_index::semantic_index;
use crate::types::{definition_ty, infer_scope_types, root_symbol_ty_by_name, Type};
use crate::types::{definition_ty, infer_scope_types, module_global_symbol_ty_by_name, Type};
use crate::Db;

pub struct SemanticModel<'db> {
Expand All @@ -28,8 +28,8 @@ impl<'db> SemanticModel<'db> {
resolve_module(self.db.upcast(), module_name)
}

pub fn root_symbol_ty(&self, module: &Module, symbol_name: &str) -> Type<'db> {
root_symbol_ty_by_name(self.db, module.file(), symbol_name)
pub fn module_global_symbol_ty(&self, module: &Module, symbol_name: &str) -> Type<'db> {
module_global_symbol_ty_by_name(self.db, module.file(), symbol_name)
}
}

Expand Down
Loading

0 comments on commit 865b92a

Please sign in to comment.