Skip to content

Commit 1526d13

Browse files
committed
move DefDatabased::expand_proc_attr_macros out of the query group
This required several non-trivial changes: - Rename the existing `ProcMacros::get` to `ProcMacros::get_for_crate` (feel free to suggest a better name), to avoid clashing with the new getter created by salsa. - Change `ProcMacroBuilder::build` to take `db` and `durability`, as it will need to build `ProcMacros` inside the `db`. Also rename it to `build_in`, to reflect the change in functionality. - `ProcMacros` is now stored in the database directly, instead of being wrapped in an Arc. This should be fine though, since its `by_crate` field is marked with `#[returns(ref)]`.
1 parent cdab17b commit 1526d13

13 files changed

Lines changed: 133 additions & 124 deletions

File tree

crates/hir-def/src/db.rs

Lines changed: 18 additions & 4 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::{
@@ -16,10 +17,6 @@ use crate::{
1617

1718
#[query_group::query_group]
1819
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-
2320
/// Computes an [`ItemTree`] for the given file or macro expansion.
2421
#[salsa::invoke(file_item_tree)]
2522
#[salsa::transparent]
@@ -47,6 +44,23 @@ pub trait DefDatabase: ExpandDatabase + SourceDatabase {
4744
fn include_macro_invoc(&self, crate_id: Crate) -> Arc<[(MacroCallId, EditionedFileId)]>;
4845
}
4946

47+
/// Whether to expand procedural macros during name resolution.
48+
#[salsa::input(singleton, debug)]
49+
pub(crate) struct ExpandProcAttrMacros {
50+
#[returns(copy)]
51+
pub(crate) enabled: bool,
52+
}
53+
54+
pub fn set_expand_proc_attr_macros(db: &mut dyn DefDatabase, enabled: bool) {
55+
if let Some(expand_proc_attr_macros) = ExpandProcAttrMacros::try_get(db) {
56+
if expand_proc_attr_macros.enabled(db) != enabled {
57+
expand_proc_attr_macros.set_enabled(db).with_durability(Durability::HIGH).to(enabled);
58+
}
59+
} else {
60+
_ = ExpandProcAttrMacros::builder(enabled).durability(Durability::HIGH).new(db);
61+
}
62+
}
63+
5064
// return: macro call id and include file id
5165
fn include_macro_invoc(
5266
db: &dyn DefDatabase,

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(3403),
193+
[salsa id]: Id(3003),
194194
krate: Crate(
195-
Id(2000),
195+
Id(1c00),
196196
),
197197
block: Some(
198198
BlockId(
199-
4401,
199+
4001,
200200
),
201201
),
202202
containing_module_inside_def_map: None,

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#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#
38+
struct#0:MacroRules[BE8F, 0]@58..64#16384# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#16384#
39+
map#0:MacroRules[BE8F, 0]@86..89#16384#:#0:MacroRules[BE8F, 0]@89..90#16384# #0:MacroRules[BE8F, 0]@89..90#16384#::#0:MacroRules[BE8F, 0]@91..93#16384#std#0:MacroRules[BE8F, 0]@93..96#16384#::#0:MacroRules[BE8F, 0]@96..98#16384#collections#0:MacroRules[BE8F, 0]@98..109#16384#::#0:MacroRules[BE8F, 0]@109..111#16384#HashSet#0:MacroRules[BE8F, 0]@111..118#16384#<#0:MacroRules[BE8F, 0]@118..119#16384#(#0:MacroRules[BE8F, 0]@119..120#16384#)#0:MacroRules[BE8F, 0]@120..121#16384#>#0:MacroRules[BE8F, 0]@121..122#16384#,#0:MacroRules[BE8F, 0]@122..123#16384#
40+
}#0:MacroRules[BE8F, 0]@132..133#16384#
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#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#
200+
struct#1:MacroRules[DB0C, 0]@59..65#16384# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#16384#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#16384#;#1:MacroRules[DB0C, 0]@75..76#16384#
201201
"#]],
202202
);
203203
}
@@ -423,10 +423,10 @@ m! { foo, bar }
423423
macro_rules! m {
424424
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
425425
}
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#
426+
impl#\16384# Bar#\16384# {#\16384#
427+
fn#\16384# foo#\ROOT2024#(#\16384#)#\16384# {#\16384#}#\16384#
428+
fn#\16384# bar#\ROOT2024#(#\16384#)#\16384# {#\16384#}#\16384#
429+
}#\16384#
430430
"#]],
431431
);
432432
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
&& !crate::db::ExpandProcAttrMacros::get(self.db).enabled(self.db) =>
620620
{
621621
(CustomProcMacroExpander::disabled_proc_attr(), kind)
622622
}

crates/hir-def/src/test_db.rs

Lines changed: 1 addition & 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,7 @@ 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+
crate::db::set_expand_proc_attr_macros(&mut this, true);
5251
// This needs to be here otherwise `CrateGraphBuilder` panics.
5352
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
5453
_ = base_db::LibraryRoots::builder(Default::default())

crates/hir-ty/src/test_db.rs

Lines changed: 2 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,7 @@ 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::set_expand_proc_attr_macros(&mut this, true);
4747
// This needs to be here otherwise `CrateGraphBuilder` panics.
4848
set_all_crates_with_durability(&mut this, std::iter::empty(), Durability::HIGH);
4949
_ = 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, set_expand_proc_attr_macros};
77
pub use hir_expand::db::ExpandDatabase;
88
pub use hir_ty::db::HirDatabase;

crates/ide-db/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,13 @@ 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::set_expand_proc_attr_macros(&mut db, false);
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::set_expand_proc_attr_macros(self, true);
220220
}
221221

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

crates/ide-db/src/test_data/test_doc_alias.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(
33
Module {
44
id: ModuleIdLt {
5-
[salsa id]: Id(3000),
5+
[salsa id]: Id(2c00),
66
},
77
},
88
[
@@ -12,15 +12,15 @@
1212
Struct(
1313
Struct {
1414
id: StructId(
15-
3801,
15+
3401,
1616
),
1717
},
1818
),
1919
),
2020
loc: DeclarationLocation {
2121
hir_file_id: FileId(
2222
EditionedFileId(
23-
Id(2c00),
23+
Id(2800),
2424
),
2525
),
2626
ptr: SyntaxNodePtr {
@@ -49,15 +49,15 @@
4949
Struct(
5050
Struct {
5151
id: StructId(
52-
3800,
52+
3400,
5353
),
5454
},
5555
),
5656
),
5757
loc: DeclarationLocation {
5858
hir_file_id: FileId(
5959
EditionedFileId(
60-
Id(2c00),
60+
Id(2800),
6161
),
6262
),
6363
ptr: SyntaxNodePtr {
@@ -86,15 +86,15 @@
8686
Struct(
8787
Struct {
8888
id: StructId(
89-
3800,
89+
3400,
9090
),
9191
},
9292
),
9393
),
9494
loc: DeclarationLocation {
9595
hir_file_id: FileId(
9696
EditionedFileId(
97-
Id(2c00),
97+
Id(2800),
9898
),
9999
),
100100
ptr: SyntaxNodePtr {
@@ -123,15 +123,15 @@
123123
Struct(
124124
Struct {
125125
id: StructId(
126-
3800,
126+
3400,
127127
),
128128
},
129129
),
130130
),
131131
loc: DeclarationLocation {
132132
hir_file_id: FileId(
133133
EditionedFileId(
134-
Id(2c00),
134+
Id(2800),
135135
),
136136
),
137137
ptr: SyntaxNodePtr {
@@ -160,15 +160,15 @@
160160
Struct(
161161
Struct {
162162
id: StructId(
163-
3800,
163+
3400,
164164
),
165165
},
166166
),
167167
),
168168
loc: DeclarationLocation {
169169
hir_file_id: FileId(
170170
EditionedFileId(
171-
Id(2c00),
171+
Id(2800),
172172
),
173173
),
174174
ptr: SyntaxNodePtr {
@@ -197,15 +197,15 @@
197197
Struct(
198198
Struct {
199199
id: StructId(
200-
3801,
200+
3401,
201201
),
202202
},
203203
),
204204
),
205205
loc: DeclarationLocation {
206206
hir_file_id: FileId(
207207
EditionedFileId(
208-
Id(2c00),
208+
Id(2800),
209209
),
210210
),
211211
ptr: SyntaxNodePtr {
@@ -234,15 +234,15 @@
234234
Struct(
235235
Struct {
236236
id: StructId(
237-
3800,
237+
3400,
238238
),
239239
},
240240
),
241241
),
242242
loc: DeclarationLocation {
243243
hir_file_id: FileId(
244244
EditionedFileId(
245-
Id(2c00),
245+
Id(2800),
246246
),
247247
),
248248
ptr: SyntaxNodePtr {

0 commit comments

Comments
 (0)