Skip to content

Commit cb7240b

Browse files
authored
Merge pull request #21897 from Veykril/push-qpksnpswtlkk
Remove `Arc` from `GenericParams` and `AstIdMap`
2 parents 8904be4 + 5820b1f commit cb7240b

36 files changed

Lines changed: 249 additions & 297 deletions

crates/hir-def/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ impl AttrFlags {
894894
def: GenericDefId,
895895
) -> &(ArenaMap<LocalLifetimeParamId, AttrFlags>, ArenaMap<LocalTypeOrConstParamId, AttrFlags>)
896896
{
897-
let generic_params = GenericParams::new(db, def);
897+
let generic_params = GenericParams::of(db, def);
898898
let params_count_excluding_self =
899899
generic_params.len() - usize::from(generic_params.trait_self_param().is_some());
900900
if params_count_excluding_self == 0 {

crates/hir-def/src/db.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ use triomphe::Arc;
1010
use crate::{
1111
AnonConstId, AnonConstLoc, AssocItemId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc,
1212
EnumId, EnumLoc, EnumVariantId, EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId,
13-
ExternCrateLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id,
14-
Macro2Loc, MacroExpander, MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags,
15-
ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc,
16-
TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
13+
ExternCrateLoc, FunctionId, FunctionLoc, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc,
14+
MacroExpander, MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId,
15+
ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId,
16+
TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
1717
attrs::AttrFlags,
18-
hir::generics::GenericParams,
19-
import_map::ImportMap,
2018
item_tree::{ItemTree, file_item_tree_query},
2119
nameres::crate_def_map,
2220
visibility::{self, Visibility},
@@ -98,13 +96,6 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
9896
#[salsa::invoke(macro_def)]
9997
fn macro_def(&self, m: MacroId) -> MacroDefId;
10098

101-
#[salsa::transparent]
102-
#[salsa::invoke(GenericParams::new)]
103-
fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>;
104-
105-
#[salsa::invoke(ImportMap::import_map_query)]
106-
fn import_map(&self, krate: Crate) -> Arc<ImportMap>;
107-
10899
// region:visibilities
109100

110101
#[salsa::invoke(visibility::field_visibilities_query)]

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use hir_expand::{
1414
use span::{AstIdMap, SyntaxContext};
1515
use syntax::ast::HasAttrs;
1616
use syntax::{AstNode, Parse, ast};
17-
use triomphe::Arc;
1817
use tt::TextRange;
1918

2019
use crate::{
@@ -23,21 +22,21 @@ use crate::{
2322
};
2423

2524
#[derive(Debug)]
26-
pub(super) struct Expander {
25+
pub(super) struct Expander<'db> {
2726
span_map: SpanMap,
2827
current_file_id: HirFileId,
29-
ast_id_map: Arc<AstIdMap>,
28+
ast_id_map: &'db AstIdMap,
3029
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
3130
recursion_depth: u32,
3231
recursion_limit: usize,
3332
}
3433

35-
impl Expander {
34+
impl<'db> Expander<'db> {
3635
pub(super) fn new(
37-
db: &dyn DefDatabase,
36+
db: &'db dyn DefDatabase,
3837
current_file_id: HirFileId,
39-
def_map: &DefMap,
40-
) -> Expander {
38+
def_map: &'db DefMap,
39+
) -> Expander<'db> {
4140
let recursion_limit = def_map.recursion_limit() as usize;
4241
let recursion_limit = if cfg!(test) {
4342
// Without this, `body::tests::your_stack_belongs_to_me` stack-overflows in debug
@@ -77,12 +76,12 @@ impl Expander {
7776

7877
pub(super) fn enter_expand<T: ast::AstNode>(
7978
&mut self,
80-
db: &dyn DefDatabase,
79+
db: &'db dyn DefDatabase,
8180
macro_call: ast::MacroCall,
8281
krate: Crate,
8382
resolver: impl Fn(&ModPath) -> Option<MacroId>,
8483
eager_callback: EagerCallBackFn<'_>,
85-
) -> Result<ExpandResult<Option<(Mark, Option<Parse<T>>)>>, UnresolvedMacro> {
84+
) -> Result<ExpandResult<Option<(Mark<'db>, Option<Parse<T>>)>>, UnresolvedMacro> {
8685
// FIXME: within_limit should support this, instead of us having to extract the error
8786
let mut unresolved_macro_err = None;
8887

@@ -130,13 +129,13 @@ impl Expander {
130129

131130
pub(super) fn enter_expand_id<T: ast::AstNode>(
132131
&mut self,
133-
db: &dyn DefDatabase,
132+
db: &'db dyn DefDatabase,
134133
call_id: MacroCallId,
135-
) -> ExpandResult<Option<(Mark, Option<Parse<T>>)>> {
134+
) -> ExpandResult<Option<(Mark<'db>, Option<Parse<T>>)>> {
136135
self.within_limit(db, |_this| ExpandResult::ok(Some(call_id)))
137136
}
138137

139-
pub(super) fn exit(&mut self, Mark { file_id, span_map, ast_id_map, mut bomb }: Mark) {
138+
pub(super) fn exit(&mut self, Mark { file_id, span_map, ast_id_map, mut bomb }: Mark<'db>) {
140139
self.span_map = span_map;
141140
self.current_file_id = file_id;
142141
self.ast_id_map = ast_id_map;
@@ -162,9 +161,9 @@ impl Expander {
162161

163162
fn within_limit<F, T: ast::AstNode>(
164163
&mut self,
165-
db: &dyn DefDatabase,
164+
db: &'db dyn DefDatabase,
166165
op: F,
167-
) -> ExpandResult<Option<(Mark, Option<Parse<T>>)>>
166+
) -> ExpandResult<Option<(Mark<'db>, Option<Parse<T>>)>>
168167
where
169168
F: FnOnce(&mut Self) -> ExpandResult<Option<MacroCallId>>,
170169
{
@@ -219,7 +218,7 @@ impl Expander {
219218

220219
#[inline]
221220
pub(super) fn ast_id_map(&self) -> &AstIdMap {
222-
&self.ast_id_map
221+
self.ast_id_map
223222
}
224223

225224
#[inline]
@@ -229,9 +228,9 @@ impl Expander {
229228
}
230229

231230
#[derive(Debug)]
232-
pub(super) struct Mark {
231+
pub(super) struct Mark<'db> {
233232
file_id: HirFileId,
234233
span_map: SpanMap,
235-
ast_id_map: Arc<AstIdMap>,
234+
ast_id_map: &'db AstIdMap,
236235
bomb: DropBomb,
237236
}

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use syntax::{
2828
},
2929
};
3030
use thin_vec::ThinVec;
31-
use triomphe::Arc;
3231
use tt::TextRange;
3332

3433
use crate::{
@@ -201,7 +200,7 @@ pub(crate) fn lower_generic_params(
201200
file_id: HirFileId,
202201
param_list: Option<ast::GenericParamList>,
203202
where_clause: Option<ast::WhereClause>,
204-
) -> (ExpressionStore, Arc<GenericParams>, ExpressionStoreSourceMap) {
203+
) -> (ExpressionStore, GenericParams, ExpressionStoreSourceMap) {
205204
let mut expr_collector = ExprCollector::signature(db, module, file_id);
206205
let mut collector = generics::GenericParamsCollector::new(def);
207206
collector.lower(&mut expr_collector, param_list, where_clause);
@@ -215,7 +214,7 @@ pub(crate) fn lower_impl(
215214
module: ModuleId,
216215
impl_syntax: InFile<ast::Impl>,
217216
impl_id: ImplId,
218-
) -> (ExpressionStore, ExpressionStoreSourceMap, TypeRefId, Option<TraitRef>, Arc<GenericParams>) {
217+
) -> (ExpressionStore, ExpressionStoreSourceMap, TypeRefId, Option<TraitRef>, GenericParams) {
219218
let mut expr_collector = ExprCollector::signature(db, module, impl_syntax.file_id);
220219
let self_ty =
221220
expr_collector.lower_type_ref_opt_disallow_impl_trait(impl_syntax.value.self_ty());
@@ -243,7 +242,7 @@ pub(crate) fn lower_trait(
243242
module: ModuleId,
244243
trait_syntax: InFile<ast::Trait>,
245244
trait_id: TraitId,
246-
) -> (ExpressionStore, ExpressionStoreSourceMap, Arc<GenericParams>) {
245+
) -> (ExpressionStore, ExpressionStoreSourceMap, GenericParams) {
247246
let mut expr_collector = ExprCollector::signature(db, module, trait_syntax.file_id);
248247
let mut collector = generics::GenericParamsCollector::with_self_param(
249248
&mut expr_collector,
@@ -265,13 +264,8 @@ pub(crate) fn lower_type_alias(
265264
module: ModuleId,
266265
alias: InFile<ast::TypeAlias>,
267266
type_alias_id: TypeAliasId,
268-
) -> (
269-
ExpressionStore,
270-
ExpressionStoreSourceMap,
271-
Arc<GenericParams>,
272-
Box<[TypeBound]>,
273-
Option<TypeRefId>,
274-
) {
267+
) -> (ExpressionStore, ExpressionStoreSourceMap, GenericParams, Box<[TypeBound]>, Option<TypeRefId>)
268+
{
275269
let mut expr_collector = ExprCollector::signature(db, module, alias.file_id);
276270
let bounds = alias
277271
.value
@@ -308,7 +302,7 @@ pub(crate) fn lower_function(
308302
) -> (
309303
ExpressionStore,
310304
ExpressionStoreSourceMap,
311-
Arc<GenericParams>,
305+
GenericParams,
312306
Box<[TypeRefId]>,
313307
Option<TypeRefId>,
314308
bool,
@@ -420,7 +414,7 @@ pub(crate) fn lower_function(
420414
pub struct ExprCollector<'db> {
421415
db: &'db dyn DefDatabase,
422416
cfg_options: &'db CfgOptions,
423-
expander: Expander,
417+
expander: Expander<'db>,
424418
def_map: &'db DefMap,
425419
local_def_map: &'db LocalDefMap,
426420
module: ModuleId,

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
//! generic parameters. See also the `Generics` type and the `generics_of` query
44
//! in rustc.
55
6-
use std::sync::LazyLock;
7-
86
use either::Either;
97
use hir_expand::name::{AsName, Name};
108
use intern::sym;
119
use la_arena::Arena;
1210
use syntax::ast::{self, HasName, HasTypeBounds};
1311
use thin_vec::ThinVec;
14-
use triomphe::Arc;
1512

1613
use crate::{
1714
GenericDefId, TypeOrConstParamId, TypeParamId,
@@ -84,28 +81,16 @@ impl GenericParamsCollector {
8481
)
8582
}
8683

87-
pub(crate) fn finish(self) -> Arc<GenericParams> {
88-
let Self { mut lifetimes, mut type_or_consts, mut where_predicates, parent: _ } = self;
89-
90-
if lifetimes.is_empty() && type_or_consts.is_empty() && where_predicates.is_empty() {
91-
static EMPTY: LazyLock<Arc<GenericParams>> = LazyLock::new(|| {
92-
Arc::new(GenericParams {
93-
lifetimes: Arena::new(),
94-
type_or_consts: Arena::new(),
95-
where_predicates: Box::default(),
96-
})
97-
});
98-
return Arc::clone(&EMPTY);
99-
}
84+
pub(crate) fn finish(self) -> GenericParams {
85+
let Self { mut lifetimes, mut type_or_consts, where_predicates, parent: _ } = self;
10086

10187
lifetimes.shrink_to_fit();
10288
type_or_consts.shrink_to_fit();
103-
where_predicates.shrink_to_fit();
104-
Arc::new(GenericParams {
89+
GenericParams {
10590
type_or_consts,
10691
lifetimes,
10792
where_predicates: where_predicates.into_boxed_slice(),
108-
})
93+
}
10994
}
11095

11196
fn lower_param_list(&mut self, ec: &mut ExprCollector<'_>, params: ast::GenericParamList) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ impl Printer<'_> {
12121212
}
12131213

12141214
pub(crate) fn print_type_param(&mut self, param: TypeParamId) {
1215-
let generic_params = self.db.generic_params(param.parent());
1215+
let generic_params = GenericParams::of(self.db, param.parent());
12161216

12171217
match generic_params[param.local_id()].name() {
12181218
Some(name) => w!(self, "{}", name.display(self.db, self.edition)),
@@ -1221,7 +1221,7 @@ impl Printer<'_> {
12211221
}
12221222

12231223
pub(crate) fn print_lifetime_param(&mut self, param: LifetimeParamId) {
1224-
let generic_params = self.db.generic_params(param.parent);
1224+
let generic_params = GenericParams::of(self.db, param.parent);
12251225
w!(self, "{}", generic_params[param.local_id].name.display(self.db, self.edition))
12261226
}
12271227

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl ExprScopes {
6464

6565
#[salsa::tracked(returns(ref))]
6666
pub fn sig_expr_scopes(db: &dyn DefDatabase, def: GenericDefId) -> ExprScopes {
67-
let (_, store) = GenericParams::of(db, def);
67+
let (_, store) = GenericParams::with_store(db, def);
6868
let roots = store.signature_const_expr_roots();
6969
let mut scopes = ExprScopes::new_store(store, roots);
7070
scopes.shrink_to_fit();

crates/hir-def/src/find_path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hash::FxHashSet;
1414
use crate::{
1515
FindPathConfig, ModuleDefId, ModuleId,
1616
db::DefDatabase,
17+
import_map::ImportMap,
1718
item_scope::ItemInNs,
1819
nameres::DefMap,
1920
visibility::{Visibility, VisibilityExplicitness},
@@ -426,7 +427,7 @@ fn find_in_dep(
426427
best_choice: &mut Option<Choice>,
427428
dep: Crate,
428429
) {
429-
let import_map = ctx.db.import_map(dep);
430+
let import_map = ImportMap::of(ctx.db, dep);
430431
let Some(import_info_for) = import_map.import_info_for(item) else {
431432
return;
432433
};

0 commit comments

Comments
 (0)