Skip to content

Commit e644310

Browse files
committed
internal: move ExpandDatabase::proc_macros out of the query group
1 parent 5f7510d commit e644310

11 files changed

Lines changed: 130 additions & 120 deletions

File tree

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

Lines changed: 2 additions & 2 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(2c03),
194194
krate: Crate(
195195
Id(1c00),
196196
),
197197
block: Some(
198198
BlockId(
199-
4001,
199+
3c01,
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#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#
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#
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#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#
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#
201201
"#]],
202202
);
203203
}
@@ -423,10 +423,10 @@ m! { foo, bar }
423423
macro_rules! m {
424424
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
425425
}
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#
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#
430430
"#]],
431431
);
432432
}

crates/hir-def/src/nameres/tests/incremental.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ fn f() { foo }
305305
"parse_macro_expansion",
306306
"expand_proc_macro",
307307
"macro_arg",
308+
"create_data_ExpandDatabase",
308309
"proc_macro_span_shim",
309310
]
310311
"#]],
@@ -441,6 +442,7 @@ pub struct S {}
441442
"parse_macro_expansion",
442443
"expand_proc_macro",
443444
"macro_arg",
445+
"create_data_ExpandDatabase",
444446
"proc_macro_span_shim",
445447
]
446448
"#]],

crates/hir-expand/src/change.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! state. Changes are transactional.
33
use base_db::{CrateGraphBuilder, FileChange, SourceRoot, salsa::Durability};
44
use span::FileId;
5-
use triomphe::Arc;
65

76
use crate::{db::ExpandDatabase, proc_macro::ProcMacrosBuilder};
87

@@ -16,12 +15,13 @@ impl ChangeWithProcMacros {
1615
pub fn apply(self, db: &mut impl ExpandDatabase) {
1716
let crates_id_map = self.source_change.apply(db);
1817
if let Some(proc_macros) = self.proc_macros {
19-
let proc_macros = proc_macros.build(
18+
proc_macros.build_in(
19+
db,
20+
Durability::HIGH,
2021
crates_id_map
2122
.as_ref()
2223
.expect("cannot set proc macros without setting the crate graph too"),
2324
);
24-
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
2525
}
2626
}
2727

crates/hir-expand/src/db.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
declarative::DeclarativeMacroExpander,
1818
fixup::{self, SyntaxFixupUndoInfo},
1919
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt},
20-
proc_macro::{CrateProcMacros, CustomProcMacroExpander, ProcMacros},
20+
proc_macro::{CrateProcMacros, CustomProcMacroExpander},
2121
span_map::{ExpansionSpanMap, RealSpanMap, SpanMap},
2222
tt,
2323
};
@@ -50,10 +50,6 @@ pub enum TokenExpander<'db> {
5050

5151
#[query_group::query_group]
5252
pub trait ExpandDatabase: SourceDatabase {
53-
/// The proc macros. Do not use this! Use `proc_macros_for_crate()` instead.
54-
#[salsa::input]
55-
fn proc_macros(&self) -> Arc<ProcMacros>;
56-
5753
/// Incrementality query to prevent queries from directly depending on `ExpandDatabase::proc_macros`.
5854
#[salsa::invoke(crate::proc_macro::proc_macros_for_crate)]
5955
fn proc_macros_for_crate(&self, krate: Crate) -> Option<Arc<CrateProcMacros>>;

crates/hir-expand/src/proc_macro.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{panic::RefUnwindSafe, sync};
77
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError};
88
use intern::Symbol;
99
use rustc_hash::FxHashMap;
10+
use salsa::{Durability, Setter};
1011
use span::Span;
1112
use triomphe::Arc;
1213

@@ -83,14 +84,24 @@ impl ProcMacrosBuilder {
8384
);
8485
}
8586

86-
pub(crate) fn build(self, crates_id_map: &CratesIdMap) -> ProcMacros {
87+
/// Builds [`ProcMacros`] and adds id to `db` with the provided `durability`.
88+
pub(crate) fn build_in(
89+
self,
90+
db: &mut dyn ExpandDatabase,
91+
durability: Durability,
92+
crates_id_map: &CratesIdMap,
93+
) {
8794
let mut map = self
8895
.0
8996
.into_iter()
9097
.map(|(krate, proc_macro)| (crates_id_map[&krate], proc_macro))
9198
.collect::<FxHashMap<_, _>>();
9299
map.shrink_to_fit();
93-
ProcMacros(map)
100+
ProcMacros::try_get(db)
101+
.unwrap_or_else(|| ProcMacros::new(db, Default::default()))
102+
.set_by_crate(db)
103+
.with_durability(durability)
104+
.to(map);
94105
}
95106
}
96107

@@ -107,11 +118,15 @@ impl FromIterator<(CrateBuilderId, ProcMacroLoadResult)> for ProcMacrosBuilder {
107118
#[derive(Debug, PartialEq, Eq)]
108119
pub struct CrateProcMacros(StoredProcMacroLoadResult);
109120

110-
#[derive(Default, Debug)]
111-
pub struct ProcMacros(FxHashMap<Crate, Arc<CrateProcMacros>>);
121+
#[salsa::input(singleton, debug)]
122+
pub struct ProcMacros {
123+
#[returns(ref)]
124+
pub by_crate: FxHashMap<Crate, Arc<CrateProcMacros>>,
125+
}
126+
112127
impl ProcMacros {
113-
fn get(&self, krate: Crate) -> Option<Arc<CrateProcMacros>> {
114-
self.0.get(&krate).cloned()
128+
fn get_for_crate(&self, db: &dyn ExpandDatabase, krate: Crate) -> Option<Arc<CrateProcMacros>> {
129+
self.by_crate(db).get(&krate).cloned()
115130
}
116131
}
117132

@@ -352,5 +367,5 @@ pub(crate) fn proc_macros_for_crate(
352367
db: &dyn ExpandDatabase,
353368
krate: Crate,
354369
) -> Option<Arc<CrateProcMacros>> {
355-
db.proc_macros().get(krate)
370+
ProcMacros::get(db).get_for_crate(db, krate)
356371
}

crates/ide-db/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ use base_db::{
6363
CrateGraphBuilder, CratesMap, FileSourceRootInput, FileText, Files, Nonce, SourceDatabase,
6464
SourceRoot, SourceRootId, SourceRootInput, set_all_crates_with_durability,
6565
};
66-
use hir::{
67-
FilePositionWrapper, FileRangeWrapper,
68-
db::{ExpandDatabase, HirDatabase},
69-
};
66+
use hir::{FilePositionWrapper, FileRangeWrapper, db::HirDatabase};
7067
use triomphe::Arc;
7168

7269
use crate::line_index::LineIndex;
@@ -203,7 +200,7 @@ impl RootDatabase {
203200
// This needs to be here otherwise `CrateGraphBuilder` will panic.
204201
set_all_crates_with_durability(&mut db, std::iter::empty(), Durability::HIGH);
205202
CrateGraphBuilder::default().set_in_db(&mut db);
206-
db.set_proc_macros_with_durability(Default::default(), Durability::MEDIUM);
203+
_ = hir::ProcMacros::builder(Default::default()).durability(Durability::MEDIUM).new(&db);
207204
_ = base_db::LibraryRoots::builder(Default::default())
208205
.durability(Durability::MEDIUM)
209206
.new(&db);

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

0 commit comments

Comments
 (0)