[red-knot] Rename FileSystem to System (#12214)

This commit is contained in:
Micha Reiser 2024-07-09 09:20:51 +02:00 committed by GitHub
parent 16a63c88cf
commit ac04380f36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 1432 additions and 1291 deletions

View file

@ -1,8 +1,7 @@
use salsa::DbWithJar;
use ruff_db::{Db as SourceDb, Upcast};
use red_knot_module_resolver::Db as ResolverDb;
use ruff_db::{Db as SourceDb, Upcast};
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::{public_symbols_map, PublicSymbolId, ScopeId};
@ -45,9 +44,10 @@ pub(crate) mod tests {
use salsa::storage::HasIngredientsFor;
use salsa::DebugWithDb;
use red_knot_module_resolver::{Db as ResolverDb, Jar as ResolverJar};
use ruff_db::file_system::{FileSystem, MemoryFileSystem, OsFileSystem};
use ruff_db::vfs::Vfs;
use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb, Jar as ResolverJar};
use ruff_db::files::Files;
use ruff_db::system::{DbWithTestSystem, System, TestSystem};
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Jar as SourceJar, Upcast};
use super::{Db, Jar};
@ -55,8 +55,9 @@ pub(crate) mod tests {
#[salsa::db(Jar, ResolverJar, SourceJar)]
pub(crate) struct TestDb {
storage: salsa::Storage<Self>,
vfs: Vfs,
file_system: TestFileSystem,
files: Files,
system: TestSystem,
vendored: VendoredFileSystem,
events: std::sync::Arc<std::sync::Mutex<Vec<salsa::Event>>>,
}
@ -64,29 +65,13 @@ pub(crate) mod tests {
pub(crate) fn new() -> Self {
Self {
storage: salsa::Storage::default(),
file_system: TestFileSystem::Memory(MemoryFileSystem::default()),
system: TestSystem::default(),
vendored: vendored_typeshed_stubs().snapshot(),
events: std::sync::Arc::default(),
vfs: Vfs::with_stubbed_vendored(),
files: Files::default(),
}
}
/// Returns the memory file system.
///
/// ## Panics
/// If this test db isn't using a memory file system.
pub(crate) fn memory_file_system(&self) -> &MemoryFileSystem {
if let TestFileSystem::Memory(fs) = &self.file_system {
fs
} else {
panic!("The test db is not using a memory file system");
}
}
#[allow(unused)]
pub(crate) fn vfs_mut(&mut self) -> &mut Vfs {
&mut self.vfs
}
/// Takes the salsa events.
///
/// ## Panics
@ -107,16 +92,27 @@ pub(crate) mod tests {
}
}
impl SourceDb for TestDb {
fn file_system(&self) -> &dyn FileSystem {
match &self.file_system {
TestFileSystem::Memory(fs) => fs,
TestFileSystem::Os(fs) => fs,
}
impl DbWithTestSystem for TestDb {
fn test_system(&self) -> &TestSystem {
&self.system
}
fn vfs(&self) -> &Vfs {
&self.vfs
fn test_system_mut(&mut self) -> &mut TestSystem {
&mut self.system
}
}
impl SourceDb for TestDb {
fn vendored(&self) -> &VendoredFileSystem {
&self.vendored
}
fn system(&self) -> &dyn System {
&self.system
}
fn files(&self) -> &Files {
&self.files
}
}
@ -147,22 +143,14 @@ pub(crate) mod tests {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(Self {
storage: self.storage.snapshot(),
vfs: self.vfs.snapshot(),
file_system: match &self.file_system {
TestFileSystem::Memory(memory) => TestFileSystem::Memory(memory.snapshot()),
TestFileSystem::Os(fs) => TestFileSystem::Os(fs.snapshot()),
},
files: self.files.snapshot(),
system: self.system.snapshot(),
vendored: self.vendored.snapshot(),
events: self.events.clone(),
})
}
}
enum TestFileSystem {
Memory(MemoryFileSystem),
#[allow(dead_code)]
Os(OsFileSystem),
}
pub(crate) fn assert_will_run_function_query<'db, C, Db, Jar>(
db: &'db Db,
to_function: impl FnOnce(&C) -> &salsa::function::FunctionIngredient<C>,

View file

@ -3,8 +3,8 @@ use std::sync::Arc;
use rustc_hash::FxHashMap;
use ruff_db::files::File;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::VfsFile;
use ruff_index::{IndexSlice, IndexVec};
use crate::semantic_index::ast_ids::node_key::ExpressionNodeKey;
@ -28,7 +28,7 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), ()>;
///
/// Prefer using [`symbol_table`] when working with symbols from a single scope.
#[salsa::tracked(return_ref, no_eq)]
pub(crate) fn semantic_index(db: &dyn Db, file: VfsFile) -> SemanticIndex<'_> {
pub(crate) fn semantic_index(db: &dyn Db, file: File) -> SemanticIndex<'_> {
let _span = tracing::trace_span!("semantic_index", ?file).entered();
let parsed = parsed_module(db.upcast(), file);
@ -51,7 +51,7 @@ pub(crate) fn symbol_table<'db>(db: &'db dyn Db, scope: ScopeId<'db>) -> Arc<Sym
/// Returns the root scope of `file`.
#[salsa::tracked]
pub(crate) fn root_scope(db: &dyn Db, file: VfsFile) -> ScopeId<'_> {
pub(crate) fn root_scope(db: &dyn Db, file: File) -> ScopeId<'_> {
let _span = tracing::trace_span!("root_scope", ?file).entered();
FileScopeId::root().to_scope_id(db, file)
@ -61,7 +61,7 @@ pub(crate) fn root_scope(db: &dyn Db, file: VfsFile) -> ScopeId<'_> {
/// no symbol with the given name exists.
pub(crate) fn public_symbol<'db>(
db: &'db dyn Db,
file: VfsFile,
file: File,
name: &str,
) -> Option<PublicSymbolId<'db>> {
let root_scope = root_scope(db, file);
@ -272,8 +272,9 @@ impl FusedIterator for ChildrenIter<'_> {}
#[cfg(test)]
mod tests {
use ruff_db::files::{system_path_to_file, File};
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::{system_path_to_file, VfsFile};
use ruff_db::system::DbWithTestSystem;
use crate::db::tests::TestDb;
use crate::semantic_index::symbol::{FileScopeId, Scope, ScopeKind, SymbolTable};
@ -282,14 +283,12 @@ mod tests {
struct TestCase {
db: TestDb,
file: VfsFile,
file: File,
}
fn test_case(content: impl ToString) -> TestCase {
let db = TestDb::new();
db.memory_file_system()
.write_file("test.py", content)
.unwrap();
let mut db = TestDb::new();
db.write_file("test.py", content).unwrap();
let file = system_path_to_file(&db, "test.py").unwrap();
@ -631,7 +630,7 @@ class C[T]:
fn scope_names<'a>(
scopes: impl Iterator<Item = (FileScopeId, &'a Scope)>,
db: &'a dyn Db,
file: VfsFile,
file: File,
) -> Vec<&'a str> {
scopes
.into_iter()

View file

@ -2,8 +2,8 @@ use std::sync::Arc;
use rustc_hash::FxHashMap;
use ruff_db::files::File;
use ruff_db::parsed::ParsedModule;
use ruff_db::vfs::VfsFile;
use ruff_index::IndexVec;
use ruff_python_ast as ast;
use ruff_python_ast::name::Name;
@ -22,7 +22,7 @@ use crate::Db;
pub(super) struct SemanticIndexBuilder<'db, 'ast> {
// Builder state
db: &'db dyn Db,
file: VfsFile,
file: File,
module: &'db ParsedModule,
scope_stack: Vec<FileScopeId>,
/// the target we're currently inferring
@ -42,7 +42,7 @@ impl<'db, 'ast> SemanticIndexBuilder<'db, 'ast>
where
'db: 'ast,
{
pub(super) fn new(db: &'db dyn Db, file: VfsFile, parsed: &'db ParsedModule) -> Self {
pub(super) fn new(db: &'db dyn Db, file: File, parsed: &'db ParsedModule) -> Self {
let mut builder = Self {
db,
file,

View file

@ -1,5 +1,5 @@
use ruff_db::files::File;
use ruff_db::parsed::ParsedModule;
use ruff_db::vfs::VfsFile;
use ruff_python_ast as ast;
use crate::ast_node_ref::AstNodeRef;
@ -10,7 +10,7 @@ use crate::semantic_index::symbol::{FileScopeId, ScopedSymbolId};
pub struct Definition<'db> {
/// The file in which the definition is defined.
#[id]
pub(super) file: VfsFile,
pub(super) file: File,
/// The scope in which the definition is defined.
#[id]

View file

@ -3,8 +3,8 @@ use std::ops::Range;
use bitflags::bitflags;
use hashbrown::hash_map::RawEntryMut;
use ruff_db::files::File;
use ruff_db::parsed::ParsedModule;
use ruff_db::vfs::VfsFile;
use ruff_index::{newtype_index, IndexVec};
use ruff_python_ast::name::Name;
use ruff_python_ast::{self as ast};
@ -79,7 +79,7 @@ bitflags! {
#[salsa::tracked]
pub struct PublicSymbolId<'db> {
#[id]
pub(crate) file: VfsFile,
pub(crate) file: File,
#[id]
pub(crate) scoped_symbol_id: ScopedSymbolId,
}
@ -116,14 +116,14 @@ impl ScopedSymbolId {
///
/// # Panics
/// May panic if the symbol does not belong to `file` or is not a symbol of `file`'s root scope.
pub(crate) fn to_public_symbol(self, db: &dyn Db, file: VfsFile) -> PublicSymbolId {
pub(crate) fn to_public_symbol(self, db: &dyn Db, file: File) -> PublicSymbolId {
let symbols = public_symbols_map(db, file);
symbols.public(self)
}
}
#[salsa::tracked(return_ref)]
pub(crate) fn public_symbols_map(db: &dyn Db, file: VfsFile) -> PublicSymbolsMap<'_> {
pub(crate) fn public_symbols_map(db: &dyn Db, file: File) -> PublicSymbolsMap<'_> {
let _span = tracing::trace_span!("public_symbols_map", ?file).entered();
let module_scope = root_scope(db, file);
@ -156,7 +156,7 @@ impl<'db> PublicSymbolsMap<'db> {
#[salsa::tracked]
pub struct ScopeId<'db> {
#[id]
pub file: VfsFile,
pub file: File,
#[id]
pub file_scope_id: FileScopeId,
@ -190,7 +190,7 @@ impl FileScopeId {
FileScopeId::from_u32(0)
}
pub fn to_scope_id(self, db: &dyn Db, file: VfsFile) -> ScopeId<'_> {
pub fn to_scope_id(self, db: &dyn Db, file: File) -> ScopeId<'_> {
let index = semantic_index(db, file);
index.scope_ids_by_scope[self]
}

View file

@ -1,5 +1,5 @@
use red_knot_module_resolver::{resolve_module, Module, ModuleName};
use ruff_db::vfs::VfsFile;
use ruff_db::files::File;
use ruff_python_ast as ast;
use ruff_python_ast::{Expr, ExpressionRef, StmtClassDef};
@ -11,11 +11,11 @@ use crate::Db;
pub struct SemanticModel<'db> {
db: &'db dyn Db,
file: VfsFile,
file: File,
}
impl<'db> SemanticModel<'db> {
pub fn new(db: &'db dyn Db, file: VfsFile) -> Self {
pub fn new(db: &'db dyn Db, file: File) -> Self {
Self { db, file }
}
@ -182,9 +182,9 @@ mod tests {
use red_knot_module_resolver::{
set_module_resolution_settings, RawModuleResolutionSettings, TargetVersion,
};
use ruff_db::file_system::FileSystemPathBuf;
use ruff_db::files::system_path_to_file;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::system_path_to_file;
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};
use crate::db::tests::TestDb;
use crate::types::Type;
@ -196,7 +196,7 @@ mod tests {
&mut db,
RawModuleResolutionSettings {
extra_paths: vec![],
workspace_root: FileSystemPathBuf::from("/src"),
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
custom_typeshed: None,
target_version: TargetVersion::Py38,
@ -208,10 +208,9 @@ mod tests {
#[test]
fn function_ty() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("/src/foo.py", "def test(): pass")?;
db.write_file("/src/foo.py", "def test(): pass")?;
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
let ast = parsed_module(&db, foo);
@ -227,10 +226,9 @@ mod tests {
#[test]
fn class_ty() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("/src/foo.py", "class Test: pass")?;
db.write_file("/src/foo.py", "class Test: pass")?;
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
let ast = parsed_module(&db, foo);
@ -246,9 +244,9 @@ mod tests {
#[test]
fn alias_ty() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("/src/foo.py", "class Test: pass"),
("/src/bar.py", "from foo import Test"),
])?;

View file

@ -1,5 +1,5 @@
use ruff_db::files::File;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::VfsFile;
use ruff_python_ast::name::Name;
use crate::semantic_index::symbol::{NodeWithScopeKind, PublicSymbolId, ScopeId};
@ -49,7 +49,7 @@ pub(crate) fn public_symbol_ty<'db>(db: &'db dyn Db, symbol: PublicSymbolId<'db>
/// Shorthand for `public_symbol_ty` that takes a symbol name instead of a [`PublicSymbolId`].
pub(crate) fn public_symbol_ty_by_name<'db>(
db: &'db dyn Db,
file: VfsFile,
file: File,
name: &str,
) -> Option<Type<'db>> {
let symbol = public_symbol(db, file, name)?;
@ -105,7 +105,7 @@ pub enum Type<'db> {
/// a specific function object
Function(FunctionType<'db>),
/// a specific module object
Module(VfsFile),
Module(File),
/// a specific class object
Class(ClassType<'db>),
/// the set of Python objects with the given class in their __class__'s method resolution order
@ -274,9 +274,9 @@ mod tests {
use red_knot_module_resolver::{
set_module_resolution_settings, RawModuleResolutionSettings, TargetVersion,
};
use ruff_db::file_system::FileSystemPathBuf;
use ruff_db::files::system_path_to_file;
use ruff_db::parsed::parsed_module;
use ruff_db::vfs::system_path_to_file;
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};
use crate::db::tests::{
assert_will_not_run_function_query, assert_will_run_function_query, TestDb,
@ -292,7 +292,7 @@ mod tests {
RawModuleResolutionSettings {
target_version: TargetVersion::Py38,
extra_paths: vec![],
workspace_root: FileSystemPathBuf::from("/src"),
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
custom_typeshed: None,
},
@ -303,9 +303,9 @@ mod tests {
#[test]
fn local_inference() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file("/src/a.py", "x = 10")?;
db.write_file("/src/a.py", "x = 10")?;
let a = system_path_to_file(&db, "/src/a.py").unwrap();
let parsed = parsed_module(&db, a);
@ -324,7 +324,7 @@ mod tests {
fn dependency_public_symbol_type_change() -> anyhow::Result<()> {
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("/src/a.py", "from foo import x"),
("/src/foo.py", "x = 10\ndef foo(): ..."),
])?;
@ -335,11 +335,7 @@ mod tests {
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
// Change `x` to a different value
db.memory_file_system()
.write_file("/src/foo.py", "x = 20\ndef foo(): ...")?;
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
foo.touch(&mut db);
db.write_file("/src/foo.py", "x = 20\ndef foo(): ...")?;
let a = system_path_to_file(&db, "/src/a.py").unwrap();
@ -365,7 +361,7 @@ mod tests {
fn dependency_non_public_symbol_change() -> anyhow::Result<()> {
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("/src/a.py", "from foo import x"),
("/src/foo.py", "x = 10\ndef foo(): y = 1"),
])?;
@ -375,13 +371,9 @@ mod tests {
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
db.memory_file_system()
.write_file("/src/foo.py", "x = 10\ndef foo(): pass")?;
db.write_file("/src/foo.py", "x = 10\ndef foo(): pass")?;
let a = system_path_to_file(&db, "/src/a.py").unwrap();
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
foo.touch(&mut db);
db.clear_salsa_events();
@ -407,7 +399,7 @@ mod tests {
fn dependency_unrelated_public_symbol() -> anyhow::Result<()> {
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("/src/a.py", "from foo import x"),
("/src/foo.py", "x = 10\ny = 20"),
])?;
@ -417,13 +409,9 @@ mod tests {
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
db.memory_file_system()
.write_file("/src/foo.py", "x = 10\ny = 30")?;
db.write_file("/src/foo.py", "x = 10\ny = 30")?;
let a = system_path_to_file(&db, "/src/a.py").unwrap();
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
foo.touch(&mut db);
db.clear_salsa_events();

View file

@ -3,7 +3,7 @@ use std::borrow::Cow;
use std::sync::Arc;
use red_knot_module_resolver::{resolve_module, ModuleName};
use ruff_db::vfs::VfsFile;
use ruff_db::files::File;
use ruff_index::IndexVec;
use ruff_python_ast as ast;
use ruff_python_ast::{ExprContext, TypeParams};
@ -58,7 +58,7 @@ pub(super) struct TypeInferenceBuilder<'db> {
// Cached lookups
index: &'db SemanticIndex<'db>,
file_scope_id: FileScopeId,
file_id: VfsFile,
file_id: File,
symbol_table: Arc<SymbolTable<'db>>,
/// The type inference results
@ -601,8 +601,8 @@ mod tests {
use red_knot_module_resolver::{
set_module_resolution_settings, RawModuleResolutionSettings, TargetVersion,
};
use ruff_db::file_system::FileSystemPathBuf;
use ruff_db::vfs::system_path_to_file;
use ruff_db::files::system_path_to_file;
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};
use ruff_python_ast::name::Name;
use crate::db::tests::TestDb;
@ -616,7 +616,7 @@ mod tests {
RawModuleResolutionSettings {
target_version: TargetVersion::Py38,
extra_paths: Vec::new(),
workspace_root: FileSystemPathBuf::from("/src"),
workspace_root: SystemPathBuf::from("/src"),
site_packages: None,
custom_typeshed: None,
},
@ -634,9 +634,9 @@ mod tests {
#[test]
fn follow_import_to_class() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("src/a.py", "from b import C as D; E = D"),
("src/b.py", "class C: pass"),
])?;
@ -648,9 +648,9 @@ mod tests {
#[test]
fn resolve_base_class_by_name() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file(
db.write_file(
"src/mod.py",
r#"
class Base:
@ -680,9 +680,9 @@ class Sub(Base):
#[test]
fn resolve_method() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file(
db.write_file(
"src/mod.py",
"
class C:
@ -710,9 +710,9 @@ class C:
#[test]
fn resolve_module_member() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_files([
db.write_files([
("src/a.py", "import b; D = b.C"),
("src/b.py", "class C: pass"),
])?;
@ -724,9 +724,9 @@ class C:
#[test]
fn resolve_literal() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file("src/a.py", "x = 1")?;
db.write_file("src/a.py", "x = 1")?;
assert_public_ty(&db, "src/a.py", "x", "Literal[1]");
@ -735,9 +735,9 @@ class C:
#[test]
fn resolve_union() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file(
db.write_file(
"src/a.py",
"
if flag:
@ -754,9 +754,9 @@ else:
#[test]
fn literal_int_arithmetic() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file(
db.write_file(
"src/a.py",
"
a = 2 + 1
@ -778,10 +778,9 @@ e = 5 % 3
#[test]
fn walrus() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("src/a.py", "x = (y := 1) + 1")?;
db.write_file("src/a.py", "x = (y := 1) + 1")?;
assert_public_ty(&db, "src/a.py", "x", "Literal[2]");
assert_public_ty(&db, "src/a.py", "y", "Literal[1]");
@ -791,10 +790,9 @@ e = 5 % 3
#[test]
fn ifexpr() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("src/a.py", "x = 1 if flag else 2")?;
db.write_file("src/a.py", "x = 1 if flag else 2")?;
assert_public_ty(&db, "src/a.py", "x", "Literal[1, 2]");
@ -803,9 +801,9 @@ e = 5 % 3
#[test]
fn ifexpr_walrus() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system().write_file(
db.write_file(
"src/a.py",
"
y = z = 0
@ -824,10 +822,9 @@ b = z
#[test]
fn ifexpr_nested() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("src/a.py", "x = 1 if flag else 2 if flag2 else 3")?;
db.write_file("src/a.py", "x = 1 if flag else 2 if flag2 else 3")?;
assert_public_ty(&db, "src/a.py", "x", "Literal[1, 2, 3]");
@ -836,10 +833,9 @@ b = z
#[test]
fn none() -> anyhow::Result<()> {
let db = setup_db();
let mut db = setup_db();
db.memory_file_system()
.write_file("src/a.py", "x = 1 if flag else None")?;
db.write_file("src/a.py", "x = 1 if flag else None")?;
assert_public_ty(&db, "src/a.py", "x", "Literal[1] | None");
Ok(())