Skip to content

Commit bbd40f8

Browse files
authored
Merge pull request #21341 from Veykril/veykril/push-wsrtvrkzqyvr
internal: Move library and local root inputs to base-db
2 parents e08a786 + 1c67944 commit bbd40f8

17 files changed

Lines changed: 101 additions & 68 deletions

File tree

crates/base-db/src/change.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
44
use std::fmt;
55

6-
use salsa::Durability;
6+
use rustc_hash::FxHashSet;
7+
use salsa::{Durability, Setter as _};
78
use triomphe::Arc;
89
use vfs::FileId;
910

10-
use crate::{CrateGraphBuilder, CratesIdMap, RootQueryDb, SourceRoot, SourceRootId};
11+
use crate::{
12+
CrateGraphBuilder, CratesIdMap, LibraryRoots, LocalRoots, RootQueryDb, SourceRoot, SourceRootId,
13+
};
1114

1215
/// Encapsulate a bunch of raw `.set` calls on the database.
1316
#[derive(Default)]
@@ -49,15 +52,24 @@ impl FileChange {
4952
pub fn apply(self, db: &mut dyn RootQueryDb) -> Option<CratesIdMap> {
5053
let _p = tracing::info_span!("FileChange::apply").entered();
5154
if let Some(roots) = self.roots {
55+
let mut local_roots = FxHashSet::default();
56+
let mut library_roots = FxHashSet::default();
5257
for (idx, root) in roots.into_iter().enumerate() {
5358
let root_id = SourceRootId(idx as u32);
59+
if root.is_library {
60+
library_roots.insert(root_id);
61+
} else {
62+
local_roots.insert(root_id);
63+
}
5464
let durability = source_root_durability(&root);
5565
for file_id in root.iter() {
5666
db.set_file_source_root_with_durability(file_id, root_id, durability);
5767
}
5868

5969
db.set_source_root_with_durability(root_id, Arc::new(root), durability);
6070
}
71+
LocalRoots::get(db).set_roots(db).to(local_roots);
72+
LibraryRoots::get(db).set_roots(db).to(library_roots);
6173
}
6274

6375
for (file_id, text) in self.files_changed {

crates/base-db/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use crate::{
3333
};
3434
use dashmap::{DashMap, mapref::entry::Entry};
3535
pub use query_group::{self};
36-
use rustc_hash::FxHasher;
36+
use rustc_hash::{FxHashSet, FxHasher};
3737
use salsa::{Durability, Setter};
3838
pub use semver::{BuildMetadata, Prerelease, Version, VersionReq};
3939
use syntax::{Parse, SyntaxError, ast};
@@ -203,6 +203,22 @@ impl Files {
203203
}
204204
}
205205

206+
/// The set of roots for crates.io libraries.
207+
/// Files in libraries are assumed to never change.
208+
#[salsa::input(singleton, debug)]
209+
pub struct LibraryRoots {
210+
#[returns(ref)]
211+
pub roots: FxHashSet<SourceRootId>,
212+
}
213+
214+
/// The set of "local" (that is, from the current workspace) roots.
215+
/// Files in local roots are assumed to change frequently.
216+
#[salsa::input(singleton, debug)]
217+
pub struct LocalRoots {
218+
#[returns(ref)]
219+
pub roots: FxHashSet<SourceRootId>,
220+
}
221+
206222
#[salsa_macros::input(debug)]
207223
pub struct FileText {
208224
#[returns(ref)]

crates/hir-def/src/expr_store/tests/body/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ fn f() {
190190
"#,
191191
expect![[r#"
192192
ModuleIdLt {
193-
[salsa id]: Id(3003),
193+
[salsa id]: Id(3803),
194194
krate: Crate(
195-
Id(1c00),
195+
Id(2400),
196196
),
197197
block: Some(
198198
BlockId(
199-
3c01,
199+
4401,
200200
),
201201
),
202202
}"#]],

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ macro_rules! f {
3535
};
3636
}
3737
38-
struct#0:MacroRules[BE8F, 0]@58..64#15360# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#15360#
39-
map#0:MacroRules[BE8F, 0]@86..89#15360#:#0:MacroRules[BE8F, 0]@89..90#15360# #0:MacroRules[BE8F, 0]@89..90#15360#::#0:MacroRules[BE8F, 0]@91..93#15360#std#0:MacroRules[BE8F, 0]@93..96#15360#::#0:MacroRules[BE8F, 0]@96..98#15360#collections#0:MacroRules[BE8F, 0]@98..109#15360#::#0:MacroRules[BE8F, 0]@109..111#15360#HashSet#0:MacroRules[BE8F, 0]@111..118#15360#<#0:MacroRules[BE8F, 0]@118..119#15360#(#0:MacroRules[BE8F, 0]@119..120#15360#)#0:MacroRules[BE8F, 0]@120..121#15360#>#0:MacroRules[BE8F, 0]@121..122#15360#,#0:MacroRules[BE8F, 0]@122..123#15360#
40-
}#0:MacroRules[BE8F, 0]@132..133#15360#
38+
struct#0:MacroRules[BE8F, 0]@58..64#17408# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#17408#
39+
map#0:MacroRules[BE8F, 0]@86..89#17408#:#0:MacroRules[BE8F, 0]@89..90#17408# #0:MacroRules[BE8F, 0]@89..90#17408#::#0:MacroRules[BE8F, 0]@91..93#17408#std#0:MacroRules[BE8F, 0]@93..96#17408#::#0:MacroRules[BE8F, 0]@96..98#17408#collections#0:MacroRules[BE8F, 0]@98..109#17408#::#0:MacroRules[BE8F, 0]@109..111#17408#HashSet#0:MacroRules[BE8F, 0]@111..118#17408#<#0:MacroRules[BE8F, 0]@118..119#17408#(#0:MacroRules[BE8F, 0]@119..120#17408#)#0:MacroRules[BE8F, 0]@120..121#17408#>#0:MacroRules[BE8F, 0]@121..122#17408#,#0:MacroRules[BE8F, 0]@122..123#17408#
40+
}#0:MacroRules[BE8F, 0]@132..133#17408#
4141
"#]],
4242
);
4343
}
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
struct#1:MacroRules[DB0C, 0]@59..65#15360# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#15360#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#15360#;#1:MacroRules[DB0C, 0]@75..76#15360#
200+
struct#1:MacroRules[DB0C, 0]@59..65#17408# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#17408#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#17408#;#1:MacroRules[DB0C, 0]@75..76#17408#
201201
"#]],
202202
);
203203
}
@@ -423,10 +423,10 @@ m! { foo, bar }
423423
macro_rules! m {
424424
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
425425
}
426-
impl#\15360# Bar#\15360# {#\15360#
427-
fn#\15360# foo#\ROOT2024#(#\15360#)#\15360# {#\15360#}#\15360#
428-
fn#\15360# bar#\ROOT2024#(#\15360#)#\15360# {#\15360#}#\15360#
429-
}#\15360#
426+
impl#\17408# Bar#\17408# {#\17408#
427+
fn#\17408# foo#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408#
428+
fn#\17408# bar#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408#
429+
}#\17408#
430430
"#]],
431431
);
432432
}

crates/hir-def/src/nameres.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,23 +502,41 @@ impl DefMap {
502502
}
503503

504504
impl DefMap {
505+
/// Returns all modules in the crate that are associated with the given file.
505506
pub fn modules_for_file<'a>(
506507
&'a self,
507508
db: &'a dyn DefDatabase,
508509
file_id: FileId,
509510
) -> impl Iterator<Item = ModuleId> + 'a {
510511
self.modules
511512
.iter()
512-
.filter(move |(_id, data)| {
513+
.filter(move |(_, data)| {
513514
data.origin.file_id().map(|file_id| file_id.file_id(db)) == Some(file_id)
514515
})
515-
.map(|(id, _data)| id)
516+
.map(|(id, _)| id)
516517
}
517518

518519
pub fn modules(&self) -> impl Iterator<Item = (ModuleId, &ModuleData)> + '_ {
519520
self.modules.iter()
520521
}
521522

523+
/// Returns all inline modules (mod name { ... }) in the crate that are associated with the given macro expansion.
524+
pub fn inline_modules_for_macro_file(
525+
&self,
526+
file_id: MacroCallId,
527+
) -> impl Iterator<Item = ModuleId> + '_ {
528+
self.modules
529+
.iter()
530+
.filter(move |(_, data)| {
531+
matches!(
532+
data.origin,
533+
ModuleOrigin::Inline { definition_tree_id, .. }
534+
if definition_tree_id.file_id().macro_file() == Some(file_id)
535+
)
536+
})
537+
.map(|(id, _)| id)
538+
}
539+
522540
pub fn derive_helpers_in_scope(
523541
&self,
524542
id: AstId<ast::Adt>,

crates/hir-def/src/test_db.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ impl Default for TestDB {
4949
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
5050
// This needs to be here otherwise `CrateGraphBuilder` panics.
5151
this.set_all_crates(Arc::new(Box::new([])));
52+
_ = base_db::LibraryRoots::builder(Default::default())
53+
.durability(Durability::MEDIUM)
54+
.new(&this);
55+
_ = base_db::LocalRoots::builder(Default::default())
56+
.durability(Durability::MEDIUM)
57+
.new(&this);
5258
CrateGraphBuilder::default().set_in_db(&mut this);
5359
this
5460
}

crates/hir-ty/src/test_db.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ impl Default for TestDB {
4646
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
4747
// This needs to be here otherwise `CrateGraphBuilder` panics.
4848
this.set_all_crates(Arc::new(Box::new([])));
49+
_ = base_db::LibraryRoots::builder(Default::default())
50+
.durability(Durability::MEDIUM)
51+
.new(&this);
52+
_ = base_db::LocalRoots::builder(Default::default())
53+
.durability(Durability::MEDIUM)
54+
.new(&this);
4955
CrateGraphBuilder::default().set_in_db(&mut this);
5056
this
5157
}

crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ impl<'db> SemanticsImpl<'db> {
836836
// FIXME: Type the return type
837837
/// Returns the range (pre-expansion) in the string literal corresponding to the resolution,
838838
/// absolute file range (post-expansion)
839-
/// of the part in the format string, the corresponding string token and the resolution if it
839+
/// of the part in the format string (post-expansion), the corresponding string token and the resolution if it
840840
/// exists.
841841
// FIXME: Remove this in favor of `check_for_format_args_template_with_file`
842842
pub fn check_for_format_args_template(

crates/ide-db/src/apply_change.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
//! Applies changes to the IDE state transactionally.
22
3-
use base_db::SourceRootId;
43
use profile::Bytes;
5-
use rustc_hash::FxHashSet;
6-
use salsa::{Database as _, Durability, Setter as _};
4+
use salsa::{Database as _, Durability};
75

8-
use crate::{
9-
ChangeWithProcMacros, RootDatabase,
10-
symbol_index::{LibraryRoots, LocalRoots},
11-
};
6+
use crate::{ChangeWithProcMacros, RootDatabase};
127

138
impl RootDatabase {
149
pub fn request_cancellation(&mut self) {
@@ -20,20 +15,6 @@ impl RootDatabase {
2015
let _p = tracing::info_span!("RootDatabase::apply_change").entered();
2116
self.request_cancellation();
2217
tracing::trace!("apply_change {:?}", change);
23-
if let Some(roots) = &change.source_change.roots {
24-
let mut local_roots = FxHashSet::default();
25-
let mut library_roots = FxHashSet::default();
26-
for (idx, root) in roots.iter().enumerate() {
27-
let root_id = SourceRootId(idx as u32);
28-
if root.is_library {
29-
library_roots.insert(root_id);
30-
} else {
31-
local_roots.insert(root_id);
32-
}
33-
}
34-
LocalRoots::get(self).set_roots(self).to(local_roots);
35-
LibraryRoots::get(self).set_roots(self).to(library_roots);
36-
}
3718
change.apply(self);
3819
}
3920

crates/ide-db/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
7575
pub use ::line_index;
7676

7777
/// `base_db` is normally also needed in places where `ide_db` is used, so this re-export is for convenience.
78-
pub use base_db::{self, FxIndexMap, FxIndexSet};
78+
pub use base_db::{self, FxIndexMap, FxIndexSet, LibraryRoots, LocalRoots};
7979
pub use span::{self, FileId};
8080

8181
pub type FilePosition = FilePositionWrapper<FileId>;
@@ -200,10 +200,10 @@ impl RootDatabase {
200200
db.set_all_crates(Arc::new(Box::new([])));
201201
CrateGraphBuilder::default().set_in_db(&mut db);
202202
db.set_proc_macros_with_durability(Default::default(), Durability::MEDIUM);
203-
_ = crate::symbol_index::LibraryRoots::builder(Default::default())
203+
_ = base_db::LibraryRoots::builder(Default::default())
204204
.durability(Durability::MEDIUM)
205205
.new(&db);
206-
_ = crate::symbol_index::LocalRoots::builder(Default::default())
206+
_ = base_db::LocalRoots::builder(Default::default())
207207
.durability(Durability::MEDIUM)
208208
.new(&db);
209209
db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH);

0 commit comments

Comments
 (0)