Skip to content

Commit 7fe164e

Browse files
committed
move DefDatabased::expand_proc_attr_macros out of the query group
1 parent cdab17b commit 7fe164e

7 files changed

Lines changed: 42 additions & 24 deletions

File tree

crates/hir-def/src/db.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ use crate::{
1616

1717
#[query_group::query_group]
1818
pub trait DefDatabase: ExpandDatabase + SourceDatabase {
19-
/// Whether to expand procedural macros during name resolution.
20-
#[salsa::input]
21-
fn expand_proc_attr_macros(&self) -> bool;
22-
2319
/// Computes an [`ItemTree`] for the given file or macro expansion.
2420
#[salsa::invoke(file_item_tree)]
2521
#[salsa::transparent]
@@ -47,6 +43,13 @@ pub trait DefDatabase: ExpandDatabase + SourceDatabase {
4743
fn include_macro_invoc(&self, crate_id: Crate) -> Arc<[(MacroCallId, EditionedFileId)]>;
4844
}
4945

46+
/// Whether to expand procedural macros during name resolution.
47+
#[salsa::input(singleton, debug)]
48+
pub struct ExpandProcAttrMacros {
49+
#[returns(copy)]
50+
pub enabled: bool,
51+
}
52+
5053
// return: macro call id and include file id
5154
fn include_macro_invoc(
5255
db: &dyn DefDatabase,

crates/hir-def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ModuleDefId, ModuleId, ProcMacroId,
3535
ProcMacroLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro, UseId,
3636
UseLoc,
37-
db::DefDatabase,
37+
db::{DefDatabase, ExpandProcAttrMacros},
3838
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
3939
item_tree::{
4040
self, Attrs, AttrsOrCfg, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
@@ -616,7 +616,7 @@ impl<'db> DefCollector<'db> {
616616
let (expander, kind) = match self.proc_macros.iter().find(|(n, _, _)| n == &def.name) {
617617
Some(_)
618618
if kind == hir_expand::proc_macro::ProcMacroKind::Attr
619-
&& !self.db.expand_proc_attr_macros() =>
619+
&& !ExpandProcAttrMacros::get(self.db).enabled(self.db) =>
620620
{
621621
(CustomProcMacroExpander::disabled_proc_attr(), kind)
622622
}

crates/hir-def/src/test_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use triomphe::Arc;
1515

1616
use crate::{
1717
Lookup, ModuleDefId, ModuleId,
18-
db::DefDatabase,
1918
expr_store::{Body, scope::ExprScopes},
2019
nameres::{DefMap, ModuleSource, block_def_map, crate_def_map},
2120
src::HasSource,
@@ -48,7 +47,8 @@ impl Default for TestDB {
4847
crates_map: Default::default(),
4948
nonce: Nonce::new(),
5049
};
51-
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
50+
_ =
51+
crate::db::ExpandProcAttrMacros::builder(true).durability(Durability::HIGH).new(&this);
5252
// This needs to be here otherwise `CrateGraphBuilder` panics.
5353
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
5454
_ = base_db::LibraryRoots::builder(Default::default())

crates/hir-ty/src/test_db.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use base_db::{
77
SourceRootId, SourceRootInput, all_crates, relevant_crates, set_all_crates_with_durability,
88
};
99

10-
use hir_def::{ModuleId, db::DefDatabase, nameres::crate_def_map};
10+
use hir_def::{ModuleId, nameres::crate_def_map};
1111
use hir_expand::EditionedFileId;
1212
use rustc_hash::FxHashMap;
1313
use salsa::Durability;
@@ -43,7 +43,9 @@ impl Default for TestDB {
4343
crates_map: Default::default(),
4444
nonce: Nonce::new(),
4545
};
46-
this.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
46+
_ = hir_def::db::ExpandProcAttrMacros::builder(true)
47+
.durability(Durability::HIGH)
48+
.new(&this);
4749
// This needs to be here otherwise `CrateGraphBuilder` panics.
4850
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
4951
_ = base_db::LibraryRoots::builder(Default::default())

crates/hir/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
//! we didn't do that.
44
//!
55
//! But we need this for at least LRU caching at the query level.
6-
pub use hir_def::db::DefDatabase;
6+
pub use hir_def::db::{DefDatabase, ExpandProcAttrMacros};
77
pub use hir_expand::db::ExpandDatabase;
88
pub use hir_ty::db::HirDatabase;

crates/ide-db/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub mod syntax_helpers {
5555
}
5656

5757
pub use hir::{ChangeWithProcMacros, EditionedFileId};
58-
use salsa::Durability;
58+
use salsa::{Durability, Setter};
5959

6060
use std::{fmt, mem::ManuallyDrop};
6161

@@ -65,7 +65,7 @@ use base_db::{
6565
};
6666
use hir::{
6767
FilePositionWrapper, FileRangeWrapper,
68-
db::{DefDatabase, ExpandDatabase, HirDatabase},
68+
db::{ExpandDatabase, HirDatabase},
6969
};
7070
use triomphe::Arc;
7171

@@ -210,13 +210,16 @@ impl RootDatabase {
210210
_ = base_db::LocalRoots::builder(Default::default())
211211
.durability(Durability::MEDIUM)
212212
.new(&db);
213-
db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH);
213+
_ = hir::db::ExpandProcAttrMacros::builder(false).durability(Durability::HIGH).new(&db);
214214
db.update_base_query_lru_capacities(lru_capacity);
215215
db
216216
}
217217

218218
pub fn enable_proc_attr_macros(&mut self) {
219-
self.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
219+
hir::db::ExpandProcAttrMacros::get(self)
220+
.set_enabled(self)
221+
.with_durability(Durability::HIGH)
222+
.to(true);
220223
}
221224

222225
pub fn update_base_query_lru_capacities(&mut self, _lru_capacity: Option<u16>) {

crates/rust-analyzer/src/reload.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
// FIXME: This is a mess that needs some untangling work
1616
use std::{iter, mem, sync::atomic::AtomicUsize, time::Duration};
1717

18-
use hir::{ChangeWithProcMacros, ProcMacrosBuilder, db::DefDatabase};
18+
use hir::{ChangeWithProcMacros, ProcMacrosBuilder};
1919
use ide_db::{
2020
FxHashMap,
21-
base_db::{CrateGraphBuilder, ProcMacroLoadingError, ProcMacroPaths, salsa::Durability},
21+
base_db::{
22+
CrateGraphBuilder, ProcMacroLoadingError, ProcMacroPaths,
23+
salsa::{Durability, Setter},
24+
},
2225
};
2326
use itertools::Itertools;
2427
use load_cargo::{ProjectFolders, load_proc_macro};
@@ -109,13 +112,20 @@ impl GlobalState {
109112
self.reload_flycheck();
110113
}
111114

112-
if self.analysis_host.raw_database().expand_proc_attr_macros()
113-
!= self.config.expand_proc_attr_macros()
114-
{
115-
self.analysis_host.raw_database_mut().set_expand_proc_attr_macros_with_durability(
116-
self.config.expand_proc_attr_macros(),
117-
Durability::HIGH,
118-
);
115+
// TODO: `ide`'s Cargo.toml says that it should avoid depending on `hir-*` crates directly,
116+
// and go through `hir` re-exports instead. `rust-analyzer` OTOH does seems to depend on `hir-*`
117+
// after all. So, should I import `ExpandProcAttrMacros` through `hir` or `hir_def`?
118+
// TODO: should I create `AnalysisHost::{update_,}expand_proc_attr_macros` to clean this up?
119+
// TODO: we don't use `raw_db` mutably until we go into the `if` -- is it a problem that I
120+
// still get it mutably in an eager manner? I did this to avoid needing two separate calls, to
121+
// `raw_database` and `raw_database_mut`. Or maybe I should just inline `raw_db`?
122+
let raw_db = self.analysis_host.raw_database_mut();
123+
let expand_proc_macros = hir::db::ExpandProcAttrMacros::get(raw_db);
124+
if expand_proc_macros.enabled(raw_db) != self.config.expand_proc_attr_macros() {
125+
expand_proc_macros
126+
.set_enabled(raw_db)
127+
.with_durability(Durability::HIGH)
128+
.to(self.config.expand_proc_attr_macros());
119129
}
120130

121131
if self.config.cargo(None) != old_config.cargo(None) {

0 commit comments

Comments
 (0)