Skip to content

Commit d8a5d39

Browse files
committed
Make relevant_crates return a Set
1 parent f632727 commit d8a5d39

File tree

11 files changed

+36
-24
lines changed

11 files changed

+36
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_db/src/input.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ use std::{
1515

1616
use ra_cfg::CfgOptions;
1717
use ra_syntax::SmolStr;
18-
use rustc_hash::FxHashMap;
19-
use rustc_hash::FxHashSet;
18+
use ra_tt::TokenExpander;
19+
use rustc_hash::{FxHashMap, FxHashSet};
2020

2121
use crate::{RelativePath, RelativePathBuf};
22-
use fmt::Display;
23-
use ra_tt::TokenExpander;
2422

2523
/// `FileId` is an integer which uniquely identifies a file. File paths are
2624
/// messy and system-dependent, so most of the code should work directly with
@@ -111,7 +109,7 @@ impl CrateName {
111109
}
112110
}
113111

114-
impl Display for CrateName {
112+
impl fmt::Display for CrateName {
115113
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116114
write!(f, "{}", self.0)
117115
}

crates/ra_db/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{panic, sync::Arc};
77

88
use ra_prof::profile;
99
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize};
10+
use rustc_hash::FxHashSet;
1011

1112
pub use crate::{
1213
cancellation::Canceled,
@@ -95,7 +96,7 @@ pub trait FileLoader {
9596
/// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we
9697
/// get by with a `&str` for the time being.
9798
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
98-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
99+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
99100
}
100101

101102
/// Database which stores all significant input facts: source code and project
@@ -133,16 +134,21 @@ pub trait SourceDatabaseExt: SourceDatabase {
133134
#[salsa::input]
134135
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
135136

136-
fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
137+
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
137138
}
138139

139140
fn source_root_crates(
140141
db: &(impl SourceDatabaseExt + SourceDatabase),
141142
id: SourceRootId,
142-
) -> Arc<Vec<CrateId>> {
143-
let root = db.source_root(id);
143+
) -> Arc<FxHashSet<CrateId>> {
144144
let graph = db.crate_graph();
145-
let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
145+
let res = graph
146+
.iter()
147+
.filter(|&krate| {
148+
let root_file = graph[krate].root_file_id;
149+
db.file_source_root(root_file) == id
150+
})
151+
.collect::<FxHashSet<_>>();
146152
Arc::new(res)
147153
}
148154

@@ -156,7 +162,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
156162
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
157163
// FIXME: this *somehow* should be platform agnostic...
158164
if std::path::Path::new(path).is_absolute() {
159-
let krate = *self.relevant_crates(anchor).get(0)?;
165+
let krate = *self.relevant_crates(anchor).iter().next()?;
160166
let (extern_source_id, relative_file) =
161167
self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?;
162168

@@ -175,7 +181,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
175181
}
176182
}
177183

178-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
184+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
179185
let source_root = self.0.file_source_root(file_id);
180186
self.0.source_root_crates(source_root)
181187
}

crates/ra_hir_def/src/test_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77

88
use hir_expand::db::AstDatabase;
99
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
10+
use rustc_hash::FxHashSet;
1011

1112
use crate::db::DefDatabase;
1213

@@ -59,7 +60,7 @@ impl FileLoader for TestDB {
5960
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
6061
FileLoaderDelegate(self).resolve_path(anchor, path)
6162
}
62-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
63+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
6364
FileLoaderDelegate(self).relevant_crates(file_id)
6465
}
6566
}

crates/ra_hir_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ doctest = false
1010
[dependencies]
1111
log = "0.4.8"
1212
either = "1.5.3"
13+
rustc-hash = "1.0.0"
1314

1415
ra_arena = { path = "../ra_arena" }
1516
ra_db = { path = "../ra_db" }

crates/ra_hir_expand/src/builtin_derive.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ra_syntax::{
99
};
1010

1111
use crate::db::AstDatabase;
12-
use crate::{name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
12+
use crate::{guess_crate, name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
1313

1414
macro_rules! register_builtin {
1515
( $($trait:ident => $expand:ident),* ) => {
@@ -160,8 +160,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
160160
let m: MacroCallId = id.into();
161161
let file_id = m.as_file().original_file(db);
162162
let cg = db.crate_graph();
163-
let krates = db.relevant_crates(file_id);
164-
let krate = match krates.get(0) {
163+
let krate = match guess_crate(db, file_id) {
165164
Some(krate) => krate,
166165
None => {
167166
let tt = quote! { core };
@@ -172,7 +171,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
172171
// XXX
173172
// All crates except core itself should have a dependency on core,
174173
// We detect `core` by seeing whether it doesn't have such a dependency.
175-
let tt = if cg[*krate].dependencies.iter().any(|dep| dep.name == "core") {
174+
let tt = if cg[krate].dependencies.iter().any(|dep| dep.name == "core") {
176175
quote! { core }
177176
} else {
178177
quote! { crate }

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
name, AstId, CrateId, MacroDefId, MacroDefKind, TextSize,
66
};
77

8-
use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
8+
use crate::{guess_crate, quote, EagerMacroId, LazyMacroId, MacroCallId};
99
use either::Either;
1010
use mbe::parse_to_token_tree;
1111
use ra_db::FileId;
@@ -335,8 +335,7 @@ fn include_expand(
335335
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
336336
let call_id: MacroCallId = arg_id.into();
337337
let original_file = call_id.as_file().original_file(db);
338-
339-
let krate = *db.relevant_crates(original_file).get(0)?;
338+
let krate = guess_crate(db, original_file)?;
340339
db.crate_graph()[krate].env.get(key)
341340
}
342341

crates/ra_hir_expand/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,8 @@ impl<N: AstNode> InFile<N> {
424424
self.with_value(self.value.syntax())
425425
}
426426
}
427+
428+
// FIXME: this is obviously wrong, there shouldn't be any guesing here
429+
fn guess_crate(db: &dyn db::AstDatabase, file_id: FileId) -> Option<CrateId> {
430+
db.relevant_crates(file_id).iter().next().copied()
431+
}

crates/ra_hir_expand/src/test_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
9+
use rustc_hash::FxHashSet;
910

1011
#[salsa::database(
1112
ra_db::SourceDatabaseExtStorage,
@@ -44,7 +45,7 @@ impl FileLoader for TestDB {
4445
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
4546
FileLoaderDelegate(self).resolve_path(anchor, path)
4647
}
47-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
48+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
4849
FileLoaderDelegate(self).relevant_crates(file_id)
4950
}
5051
}

crates/ra_hir_ty/src/test_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
99
use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
1010
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
11+
use rustc_hash::FxHashSet;
1112
use stdx::format_to;
1213

1314
use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator};
@@ -73,7 +74,7 @@ impl FileLoader for TestDB {
7374
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
7475
FileLoaderDelegate(self).resolve_path(anchor, path)
7576
}
76-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
77+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7778
FileLoaderDelegate(self).relevant_crates(file_id)
7879
}
7980
}

0 commit comments

Comments
 (0)