Skip to content

Commit 1fe252c

Browse files
Remove some Arcs
Unfortunately, this causes `Semantics` to become invariant over its lifetime, which necessitates large changes, but the changes from `hir` downwards are mechanical and could use a skim review.
1 parent 0705955 commit 1fe252c

264 files changed

Lines changed: 1083 additions & 952 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/hir-def/src/attrs/docs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ struct DocExprSourceCtx<'db> {
333333
resolver: Resolver<'db>,
334334
file_id: HirFileId,
335335
ast_id_map: &'db AstIdMap,
336-
span_map: SpanMap,
336+
span_map: SpanMap<'db>,
337337
}
338338

339339
fn expand_doc_expr_via_macro_pipeline<'db>(
@@ -390,7 +390,7 @@ fn expand_doc_macro_call<'db>(
390390
.value?;
391391

392392
expander.recursion_depth += 1;
393-
let parse = expander.db.parse_macro_expansion(call_id).value.0;
393+
let parse = expander.db.parse_macro_expansion(call_id).value.0.clone();
394394
let expr = parse.cast::<ast::Expr>().map(|parse| parse.tree())?;
395395
expander.recursion_depth -= 1;
396396

crates/hir-def/src/expr_store/expander.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ use std::mem;
55
use base_db::Crate;
66
use cfg::CfgOptions;
77
use drop_bomb::DropBomb;
8-
use hir_expand::AstId;
9-
use hir_expand::span_map::SpanMapRef;
108
use hir_expand::{
11-
ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
9+
AstId, ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
1210
eager::EagerCallBackFn, mod_path::ModPath, span_map::SpanMap,
1311
};
1412
use span::{AstIdMap, SyntaxContext};
@@ -23,7 +21,7 @@ use crate::{
2321

2422
#[derive(Debug)]
2523
pub(super) struct Expander<'db> {
26-
span_map: SpanMap,
24+
span_map: SpanMap<'db>,
2725
current_file_id: HirFileId,
2826
ast_id_map: &'db AstIdMap,
2927
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
@@ -58,11 +56,11 @@ impl<'db> Expander<'db> {
5856
}
5957

6058
pub(super) fn hygiene_for_range(&self, db: &dyn DefDatabase, range: TextRange) -> HygieneId {
61-
match self.span_map.as_ref() {
62-
hir_expand::span_map::SpanMapRef::ExpansionSpanMap(span_map) => {
59+
match self.span_map {
60+
SpanMap::ExpansionSpanMap(span_map) => {
6361
HygieneId::new(span_map.span_at(range.start()).ctx.opaque_and_semiopaque(db))
6462
}
65-
hir_expand::span_map::SpanMapRef::RealSpanMap(_) => HygieneId::ROOT,
63+
SpanMap::RealSpanMap(_) => HygieneId::ROOT,
6664
}
6765
}
6866

@@ -193,15 +191,15 @@ impl<'db> Expander<'db> {
193191

194192
let res = db.parse_macro_expansion(call_id);
195193

196-
let err = err.or(res.err);
194+
let err = err.or_else(|| res.err.clone());
197195
ExpandResult {
198196
value: {
199-
let parse = res.value.0.cast::<T>();
197+
let parse = res.value.0.clone().cast::<T>();
200198

201199
self.recursion_depth += 1;
202200
let old_file_id = std::mem::replace(&mut self.current_file_id, call_id.into());
203201
let old_span_map =
204-
std::mem::replace(&mut self.span_map, db.span_map(self.current_file_id));
202+
std::mem::replace(&mut self.span_map, SpanMap::ExpansionSpanMap(&res.value.1));
205203
let prev_ast_id_map =
206204
mem::replace(&mut self.ast_id_map, db.ast_id_map(self.current_file_id));
207205
let mark = Mark {
@@ -222,15 +220,15 @@ impl<'db> Expander<'db> {
222220
}
223221

224222
#[inline]
225-
pub(super) fn span_map(&self) -> SpanMapRef<'_> {
226-
self.span_map.as_ref()
223+
pub(super) fn span_map(&self) -> SpanMap<'_> {
224+
self.span_map
227225
}
228226
}
229227

230228
#[derive(Debug)]
231229
pub(super) struct Mark<'db> {
232230
file_id: HirFileId,
233-
span_map: SpanMap,
231+
span_map: SpanMap<'db>,
234232
ast_id_map: &'db AstIdMap,
235233
bomb: DropBomb,
236234
}

crates/hir-def/src/expr_store/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use either::Either;
1414
use hir_expand::{
1515
HirFileId, InFile, MacroDefId,
1616
name::{AsName, Name},
17-
span_map::SpanMapRef,
17+
span_map::SpanMap,
1818
};
1919
use intern::{Symbol, sym};
2020
use rustc_hash::FxHashMap;
@@ -585,7 +585,7 @@ impl<'db> ExprCollector<'db> {
585585
}
586586

587587
#[inline]
588-
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
588+
pub(crate) fn span_map(&self) -> SpanMap<'_> {
589589
self.expander.span_map()
590590
}
591591

crates/hir-def/src/find_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ mod tests {
684684
let ast_path =
685685
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
686686
let mod_path = ModPath::from_src(&db, ast_path, &mut |range| {
687-
db.span_map(pos.file_id.into()).as_ref().span_for_range(range).ctx
687+
db.span_map(pos.file_id.into()).span_for_range(range).ctx
688688
})
689689
.unwrap();
690690

crates/hir-def/src/item_tree.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use std::{
4040
fmt::{self, Debug},
4141
hash::Hash,
4242
ops::Index,
43-
sync::OnceLock,
4443
};
4544

4645
use ast::{AstNode, StructKind};
@@ -61,7 +60,6 @@ use span::{
6160
use stdx::never;
6261
use syntax::{SourceFile, SyntaxKind, ast, match_ast};
6362
use thin_vec::ThinVec;
64-
use triomphe::Arc;
6563
use tt::TextRange;
6664

6765
use crate::{BlockId, Lookup, attrs::parse_extra_crate_attrs, db::DefDatabase};
@@ -121,14 +119,13 @@ fn lower_extra_crate_attrs<'a>(
121119
AttrsOrCfg::lower(db, &crate_attrs_as_src, cfg_options, span_map)
122120
}
123121

124-
#[salsa_macros::tracked(returns(deref))]
122+
#[salsa_macros::tracked(returns(ref))]
125123
pub(crate) fn file_item_tree_query(
126124
db: &dyn DefDatabase,
127125
file_id: HirFileId,
128126
krate: Crate,
129-
) -> Arc<ItemTree> {
127+
) -> ItemTree {
130128
let _p = tracing::info_span!("file_item_tree_query", ?file_id).entered();
131-
static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new();
132129

133130
let ctx = lower::Ctx::new(db, file_id, krate);
134131
let syntax = db.parse_or_expand(file_id);
@@ -174,21 +171,17 @@ pub(crate) fn file_item_tree_query(
174171
&& top_attrs.is_empty()
175172
&& vis.arena.is_empty()
176173
{
177-
EMPTY
178-
.get_or_init(|| {
179-
Arc::new(ItemTree {
180-
top_level: Box::new([]),
181-
attrs: FxHashMap::default(),
182-
small_data: FxHashMap::default(),
183-
big_data: FxHashMap::default(),
184-
top_attrs: AttrsOrCfg::empty(),
185-
vis: ItemVisibilities { arena: ThinVec::new() },
186-
})
187-
})
188-
.clone()
174+
ItemTree {
175+
top_level: Box::new([]),
176+
attrs: FxHashMap::default(),
177+
small_data: FxHashMap::default(),
178+
big_data: FxHashMap::default(),
179+
top_attrs: AttrsOrCfg::empty(),
180+
vis: ItemVisibilities { arena: ThinVec::new() },
181+
}
189182
} else {
190183
item_tree.shrink_to_fit();
191-
Arc::new(item_tree)
184+
item_tree
192185
}
193186
}
194187

crates/hir-def/src/item_tree/lower.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ use std::cell::OnceCell;
44

55
use base_db::{Crate, FxIndexSet};
66
use cfg::CfgOptions;
7-
use hir_expand::{
8-
HirFileId,
9-
mod_path::PathKind,
10-
name::AsName,
11-
span_map::{SpanMap, SpanMapRef},
12-
};
7+
use hir_expand::{HirFileId, mod_path::PathKind, name::AsName, span_map::SpanMap};
138
use la_arena::Arena;
149
use span::{AstIdMap, FileAstId, SyntaxContext};
1510
use syntax::{
@@ -32,7 +27,7 @@ pub(super) struct Ctx<'db> {
3227
pub(super) db: &'db dyn DefDatabase,
3328
tree: ItemTree,
3429
source_ast_id_map: &'db AstIdMap,
35-
span_map: OnceCell<SpanMap>,
30+
span_map: OnceCell<SpanMap<'db>>,
3631
file: HirFileId,
3732
cfg_options: OnceCell<&'db CfgOptions>,
3833
krate: Crate,
@@ -60,8 +55,8 @@ impl<'db> Ctx<'db> {
6055
self.cfg_options.get_or_init(|| self.krate.cfg_options(self.db))
6156
}
6257

63-
pub(super) fn span_map(&self) -> SpanMapRef<'_> {
64-
self.span_map.get_or_init(|| self.db.span_map(self.file)).as_ref()
58+
pub(super) fn span_map(&self) -> SpanMap<'_> {
59+
*self.span_map.get_or_init(|| self.db.span_map(self.file))
6560
}
6661

6762
pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use hir_expand::{
2222
builtin::quote::quote,
2323
db::ExpandDatabase,
2424
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
25-
span_map::SpanMapRef,
25+
span_map::SpanMap,
2626
};
2727
use intern::{Symbol, sym};
2828
use itertools::Itertools;
@@ -142,10 +142,10 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
142142
}
143143

144144
let mut expn_text = String::new();
145-
if let Some(err) = exp.err {
145+
if let Some(err) = &exp.err {
146146
format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).message);
147147
}
148-
let (parse, token_map) = exp.value;
148+
let (parse, token_map) = &exp.value;
149149
if expect_errors {
150150
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
151151
for e in parse.errors() {
@@ -161,7 +161,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
161161
}
162162
let pp = pretty_print_macro_expansion(
163163
parse.syntax_node(),
164-
SpanMapRef::ExpansionSpanMap(&token_map),
164+
SpanMap::ExpansionSpanMap(token_map),
165165
show_spans,
166166
show_ctxt,
167167
);
@@ -215,7 +215,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
215215
}
216216
let pp = pretty_print_macro_expansion(
217217
src.value,
218-
db.span_map(src.file_id).as_ref(),
218+
db.span_map(src.file_id),
219219
show_spans,
220220
show_ctxt,
221221
);
@@ -230,7 +230,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
230230
if let Some(macro_file) = src.file_id.macro_file() {
231231
let pp = pretty_print_macro_expansion(
232232
src.value.syntax().clone(),
233-
db.span_map(macro_file.into()).as_ref(),
233+
db.span_map(macro_file.into()),
234234
false,
235235
false,
236236
);
@@ -245,7 +245,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
245245
{
246246
let pp = pretty_print_macro_expansion(
247247
src.value.syntax().clone(),
248-
db.span_map(macro_file.into()).as_ref(),
248+
db.span_map(macro_file.into()),
249249
false,
250250
false,
251251
);
@@ -309,7 +309,7 @@ fn reindent(indent: IndentLevel, pp: String) -> String {
309309

310310
fn pretty_print_macro_expansion(
311311
expn: SyntaxNode,
312-
map: SpanMapRef<'_>,
312+
map: SpanMap<'_>,
313313
show_spans: bool,
314314
show_ctxt: bool,
315315
) -> String {

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct AssocItemCollector<'db> {
138138
def_map: &'db DefMap,
139139
local_def_map: &'db LocalDefMap,
140140
ast_id_map: &'db AstIdMap,
141-
span_map: SpanMap,
141+
span_map: SpanMap<'db>,
142142
cfg_options: &'db CfgOptions,
143143
file_id: HirFileId,
144144
diagnostics: Vec<DefDiagnostic>,
@@ -191,19 +191,18 @@ impl<'db> AssocItemCollector<'db> {
191191

192192
fn collect_item(&mut self, item: ast::AssocItem) {
193193
let ast_id = self.ast_id_map.ast_id(&item);
194-
let attrs =
195-
match AttrsOrCfg::lower(self.db, &item, &|| self.cfg_options, self.span_map.as_ref()) {
196-
AttrsOrCfg::Enabled { attrs } => attrs,
197-
AttrsOrCfg::CfgDisabled(cfg) => {
198-
self.diagnostics.push(DefDiagnostic::unconfigured_code(
199-
self.module_id,
200-
InFile::new(self.file_id, ast_id.erase()),
201-
cfg.0,
202-
self.cfg_options.clone(),
203-
));
204-
return;
205-
}
206-
};
194+
let attrs = match AttrsOrCfg::lower(self.db, &item, &|| self.cfg_options, self.span_map) {
195+
AttrsOrCfg::Enabled { attrs } => attrs,
196+
AttrsOrCfg::CfgDisabled(cfg) => {
197+
self.diagnostics.push(DefDiagnostic::unconfigured_code(
198+
self.module_id,
199+
InFile::new(self.file_id, ast_id.erase()),
200+
cfg.0,
201+
self.cfg_options.clone(),
202+
));
203+
return;
204+
}
205+
};
207206
let ast_id = InFile::new(self.file_id, ast_id.upcast());
208207

209208
'attrs: for (attr_id, attr) in attrs.as_ref().iter() {
@@ -357,7 +356,7 @@ impl<'db> AssocItemCollector<'db> {
357356
return;
358357
}
359358

360-
let (syntax, span_map) = self.db.parse_macro_expansion(macro_call_id).value;
359+
let (syntax, span_map) = &self.db.parse_macro_expansion(macro_call_id).value;
361360
let old_file_id = mem::replace(&mut self.file_id, macro_call_id.into());
362361
let old_ast_id_map = mem::replace(&mut self.ast_id_map, self.db.ast_id_map(self.file_id));
363362
let old_span_map = mem::replace(&mut self.span_map, SpanMap::ExpansionSpanMap(span_map));

0 commit comments

Comments
 (0)