Make relevant_crates return a Set

This commit is contained in:
Aleksey Kladov 2020-06-11 11:30:06 +02:00
parent f632727b2a
commit d8a5d39c2d
11 changed files with 36 additions and 24 deletions

1
Cargo.lock generated
View file

@ -1010,6 +1010,7 @@ dependencies = [
"ra_prof",
"ra_syntax",
"ra_tt",
"rustc-hash",
"test_utils",
]

View file

@ -15,12 +15,10 @@ use std::{
use ra_cfg::CfgOptions;
use ra_syntax::SmolStr;
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet;
use ra_tt::TokenExpander;
use rustc_hash::{FxHashMap, FxHashSet};
use crate::{RelativePath, RelativePathBuf};
use fmt::Display;
use ra_tt::TokenExpander;
/// `FileId` is an integer which uniquely identifies a file. File paths are
/// messy and system-dependent, so most of the code should work directly with
@ -111,7 +109,7 @@ impl CrateName {
}
}
impl Display for CrateName {
impl fmt::Display for CrateName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}

View file

@ -7,6 +7,7 @@ use std::{panic, sync::Arc};
use ra_prof::profile;
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize};
use rustc_hash::FxHashSet;
pub use crate::{
cancellation::Canceled,
@ -95,7 +96,7 @@ pub trait FileLoader {
/// `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 relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
}
/// Database which stores all significant input facts: source code and project
@ -133,16 +134,21 @@ pub trait SourceDatabaseExt: SourceDatabase {
#[salsa::input]
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
}
fn source_root_crates(
db: &(impl SourceDatabaseExt + SourceDatabase),
id: SourceRootId,
) -> Arc<Vec<CrateId>> {
let root = db.source_root(id);
) -> Arc<FxHashSet<CrateId>> {
let graph = db.crate_graph();
let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
let res = graph
.iter()
.filter(|&krate| {
let root_file = graph[krate].root_file_id;
db.file_source_root(root_file) == id
})
.collect::<FxHashSet<_>>();
Arc::new(res)
}
@ -156,7 +162,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
// FIXME: this *somehow* should be platform agnostic...
if std::path::Path::new(path).is_absolute() {
let krate = *self.relevant_crates(anchor).get(0)?;
let krate = *self.relevant_crates(anchor).iter().next()?;
let (extern_source_id, relative_file) =
self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?;
@ -175,7 +181,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
}
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root)
}

View file

@ -7,6 +7,7 @@ use std::{
use hir_expand::db::AstDatabase;
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
use rustc_hash::FxHashSet;
use crate::db::DefDatabase;
@ -59,7 +60,7 @@ impl FileLoader for TestDB {
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}

View file

@ -10,6 +10,7 @@ doctest = false
[dependencies]
log = "0.4.8"
either = "1.5.3"
rustc-hash = "1.0.0"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }

View file

@ -9,7 +9,7 @@ use ra_syntax::{
};
use crate::db::AstDatabase;
use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
use crate::{guess_crate, name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
macro_rules! register_builtin {
( $($trait:ident => $expand:ident),* ) => {
@ -160,8 +160,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
let m: MacroCallId = id.into();
let file_id = m.as_file().original_file(db);
let cg = db.crate_graph();
let krates = db.relevant_crates(file_id);
let krate = match krates.get(0) {
let krate = match guess_crate(db, file_id) {
Some(krate) => krate,
None => {
let tt = quote! { core };
@ -172,7 +171,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
// XXX
// All crates except core itself should have a dependency on core,
// We detect `core` by seeing whether it doesn't have such a dependency.
let tt = if cg[*krate].dependencies.iter().any(|dep| dep.name == "core") {
let tt = if cg[krate].dependencies.iter().any(|dep| dep.name == "core") {
quote! { core }
} else {
quote! { crate }

View file

@ -5,7 +5,7 @@ use crate::{
name, AstId, CrateId, MacroDefId, MacroDefKind, TextSize,
};
use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
use crate::{guess_crate, quote, EagerMacroId, LazyMacroId, MacroCallId};
use either::Either;
use mbe::parse_to_token_tree;
use ra_db::FileId;
@ -335,8 +335,7 @@ fn include_expand(
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
let call_id: MacroCallId = arg_id.into();
let original_file = call_id.as_file().original_file(db);
let krate = *db.relevant_crates(original_file).get(0)?;
let krate = guess_crate(db, original_file)?;
db.crate_graph()[krate].env.get(key)
}

View file

@ -424,3 +424,8 @@ impl<N: AstNode> InFile<N> {
self.with_value(self.value.syntax())
}
}
// FIXME: this is obviously wrong, there shouldn't be any guesing here
fn guess_crate(db: &dyn db::AstDatabase, file_id: FileId) -> Option<CrateId> {
db.relevant_crates(file_id).iter().next().copied()
}

View file

@ -6,6 +6,7 @@ use std::{
};
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
use rustc_hash::FxHashSet;
#[salsa::database(
ra_db::SourceDatabaseExtStorage,
@ -44,7 +45,7 @@ impl FileLoader for TestDB {
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}

View file

@ -8,6 +8,7 @@ use std::{
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
use rustc_hash::FxHashSet;
use stdx::format_to;
use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator};
@ -73,7 +74,7 @@ impl FileLoader for TestDB {
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}

View file

@ -19,7 +19,7 @@ use ra_db::{
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
SourceRootId, Upcast,
};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
@ -60,7 +60,7 @@ impl FileLoader for RootDatabase {
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}