4760: Minimize FileLoader interface r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-05 15:23:22 +00:00 committed by GitHub
commit f133159ec0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 79 deletions

View file

@ -89,14 +89,13 @@ pub const DEFAULT_LRU_CAP: usize = 128;
pub trait FileLoader { pub trait FileLoader {
/// Text of the file. /// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<String>; fn file_text(&self, file_id: FileId) -> Arc<String>;
/// Note that we intentionally accept a `&str` and not a `&Path` here. This
/// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such,
/// so the input is guaranteed to be utf-8 string. We might introduce
/// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we
/// get by with a `&str` for the time being.
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId>;
} }
/// Database which stores all significant input facts: source code and project /// Database which stores all significant input facts: source code and project
@ -154,34 +153,30 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn file_text(&self, file_id: FileId) -> Arc<String> { fn file_text(&self, file_id: FileId) -> Arc<String> {
SourceDatabaseExt::file_text(self.0, file_id) SourceDatabaseExt::file_text(self.0, file_id)
} }
/// Note that we intentionally accept a `&str` and not a `&Path` here. This
/// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such,
/// so the input is guaranteed to be utf-8 string. We might introduce
/// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we
/// get by with a `&str` for the time being.
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
let rel_path = { // FIXME: this *somehow* should be platform agnostic...
let mut rel_path = self.0.file_relative_path(anchor); if std::path::Path::new(path).is_absolute() {
assert!(rel_path.pop()); let krate = *self.relevant_crates(anchor).get(0)?;
rel_path.push(path); let (extern_source_id, relative_file) =
rel_path.normalize() self.0.crate_graph()[krate].extern_source.extern_path(path)?;
};
let source_root = self.0.file_source_root(anchor); let source_root = self.0.source_root(SourceRootId(extern_source_id.0));
let source_root = self.0.source_root(source_root); source_root.file_by_relative_path(&relative_file)
source_root.file_by_relative_path(&rel_path) } else {
let rel_path = {
let mut rel_path = self.0.file_relative_path(anchor);
assert!(rel_path.pop());
rel_path.push(path);
rel_path.normalize()
};
let source_root = self.0.file_source_root(anchor);
let source_root = self.0.source_root(source_root);
source_root.file_by_relative_path(&rel_path)
}
} }
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
let source_root = self.0.file_source_root(file_id); let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root) self.0.source_root_crates(source_root)
} }
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
let source_root = self.0.source_root(SourceRootId(extern_id.0));
source_root.file_by_relative_path(&relative_path)
}
} }

View file

@ -6,9 +6,7 @@ use std::{
}; };
use hir_expand::db::AstDatabase; use hir_expand::db::AstDatabase;
use ra_db::{ use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath, Upcast,
};
use crate::db::DefDatabase; use crate::db::DefDatabase;
@ -64,14 +62,6 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl TestDB { impl TestDB {

View file

@ -295,19 +295,13 @@ fn concat_expand(
fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> { fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
let call_site = call_id.as_file().original_file(db); let call_site = call_id.as_file().original_file(db);
let res = db.resolve_path(call_site, path)?;
// Handle trivial case // Prevent include itself
if let Some(res) = db.resolve_path(call_site, path) { if res == call_site {
// Prevent include itself None
return if res == call_site { None } else { Some(res) }; } else {
Some(res)
} }
// Extern paths ?
let krate = *db.relevant_crates(call_site).get(0)?;
let (extern_source_id, relative_file) =
db.crate_graph()[krate].extern_source.extern_path(path)?;
db.resolve_extern_path(extern_source_id, &relative_file)
} }
fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> { fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {

View file

@ -5,7 +5,7 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
#[salsa::database( #[salsa::database(
ra_db::SourceDatabaseExtStorage, ra_db::SourceDatabaseExtStorage,
@ -47,11 +47,4 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
anchor: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(anchor, relative_path)
}
} }

View file

@ -7,9 +7,7 @@ use std::{
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId}; use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink}; use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
use ra_db::{ use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast,
};
use stdx::format_to; use stdx::format_to;
use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator}; use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator};
@ -78,13 +76,6 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl TestDB { impl TestDB {

View file

@ -16,8 +16,8 @@ use std::sync::Arc;
use hir::db::{AstDatabase, DefDatabase}; use hir::db::{AstDatabase, DefDatabase};
use ra_db::{ use ra_db::{
salsa::{self, Database, Durability}, salsa::{self, Database, Durability},
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
SourceDatabase, SourceRootId, Upcast, SourceRootId, Upcast,
}; };
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
@ -63,13 +63,6 @@ impl FileLoader for RootDatabase {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl salsa::Database for RootDatabase { impl salsa::Database for RootDatabase {