Skip to content

Commit c0dfbc4

Browse files
committed
expose getters and setters on ExpandProcAttrMacros instead of the struct directly
1 parent 5f7510d commit c0dfbc4

7 files changed

Lines changed: 36 additions & 29 deletions

File tree

crates/hir-def/src/db.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use hir_expand::{
44
EditionedFileId, HirFileId, InFile, Lookup, MacroCallId, MacroDefId, MacroDefKind,
55
db::ExpandDatabase,
66
};
7+
use salsa::{Durability, Setter};
78
use triomphe::Arc;
89

910
use crate::{
@@ -45,9 +46,25 @@ pub trait DefDatabase: ExpandDatabase + SourceDatabase {
4546

4647
/// Whether to expand procedural macros during name resolution.
4748
#[salsa::input(singleton, debug)]
48-
pub struct ExpandProcAttrMacros {
49+
struct ExpandProcAttrMacros {
4950
#[returns(copy)]
50-
pub enabled: bool,
51+
enabled: bool,
52+
}
53+
54+
pub fn get_expand_proc_attr_macros(db: &dyn DefDatabase) -> bool {
55+
ExpandProcAttrMacros::get(db).enabled(db)
56+
}
57+
58+
pub fn set_expand_proc_attr_macros_with_durability(
59+
db: &mut dyn salsa::Database,
60+
enabled: bool,
61+
durability: Durability,
62+
) {
63+
ExpandProcAttrMacros::try_get(db)
64+
.unwrap_or_else(|| ExpandProcAttrMacros::new(db, Default::default()))
65+
.set_enabled(db)
66+
.with_durability(durability)
67+
.to(enabled);
5168
}
5269

5370
// return: macro call id and include file id

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, ExpandProcAttrMacros},
37+
db::DefDatabase,
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-
&& !ExpandProcAttrMacros::get(self.db).enabled(self.db) =>
619+
&& !crate::db::get_expand_proc_attr_macros(self.db) =>
620620
{
621621
(CustomProcMacroExpander::disabled_proc_attr(), kind)
622622
}

crates/hir-def/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Default for TestDB {
4747
crates_map: Default::default(),
4848
nonce: Nonce::new(),
4949
};
50-
_ = crate::db::ExpandProcAttrMacros::builder(true).durability(Durability::HIGH).new(&this);
50+
crate::db::set_expand_proc_attr_macros_with_durability(&mut this, true, Durability::HIGH);
5151
// This needs to be here otherwise `CrateGraphBuilder` panics.
5252
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
5353
_ = base_db::LibraryRoots::builder(Default::default())

crates/hir-ty/src/test_db.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ impl Default for TestDB {
4343
crates_map: Default::default(),
4444
nonce: Nonce::new(),
4545
};
46-
_ = hir_def::db::ExpandProcAttrMacros::builder(true)
47-
.durability(Durability::HIGH)
48-
.new(&this);
46+
hir_def::db::set_expand_proc_attr_macros_with_durability(&mut this, true, Durability::HIGH);
4947
// This needs to be here otherwise `CrateGraphBuilder` panics.
5048
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
5149
_ = base_db::LibraryRoots::builder(Default::default())

crates/hir/src/db.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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, ExpandProcAttrMacros};
6+
pub use hir_def::db::{
7+
DefDatabase, get_expand_proc_attr_macros, set_expand_proc_attr_macros_with_durability,
8+
};
79
pub use hir_expand::db::ExpandDatabase;
810
pub use hir_ty::db::HirDatabase;

crates/ide-db/src/lib.rs

Lines changed: 3 additions & 6 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, Setter};
58+
use salsa::Durability;
5959

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

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

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

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

crates/rust-analyzer/src/reload.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ use std::{iter, mem, sync::atomic::AtomicUsize, time::Duration};
1818
use hir::{ChangeWithProcMacros, ProcMacrosBuilder};
1919
use ide_db::{
2020
FxHashMap,
21-
base_db::{
22-
CrateGraphBuilder, ProcMacroLoadingError, ProcMacroPaths,
23-
salsa::{Durability, Setter},
24-
},
21+
base_db::{CrateGraphBuilder, ProcMacroLoadingError, ProcMacroPaths, salsa::Durability},
2522
};
2623
use itertools::Itertools;
2724
use load_cargo::{ProjectFolders, load_proc_macro};
@@ -112,20 +109,16 @@ impl GlobalState {
112109
self.reload_flycheck();
113110
}
114111

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?
119112
// TODO: we don't use `raw_db` mutably until we go into the `if` -- is it a problem that I
120113
// still get it mutably in an eager manner? I did this to avoid needing two separate calls, to
121114
// `raw_database` and `raw_database_mut`. Or maybe I should just inline `raw_db`?
122115
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());
116+
if hir::db::get_expand_proc_attr_macros(raw_db) != self.config.expand_proc_attr_macros() {
117+
hir::db::set_expand_proc_attr_macros_with_durability(
118+
raw_db,
119+
self.config.expand_proc_attr_macros(),
120+
Durability::HIGH,
121+
);
129122
}
130123

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

0 commit comments

Comments
 (0)