Skip to content

Commit caefec5

Browse files
committed
optimize some stuff
1 parent 51cdba5 commit caefec5

File tree

9 files changed

+35
-45
lines changed

9 files changed

+35
-45
lines changed

crates/hir-def/src/attr.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::{borrow::Cow, convert::identity, hash::Hash, ops};
44

5-
use base_db::Crate;
65
use cfg::{CfgExpr, CfgOptions};
76
use either::Either;
87
use hir_expand::{
@@ -56,12 +55,12 @@ impl Attrs {
5655
(**self).iter().find(|attr| attr.id == id)
5756
}
5857

59-
pub(crate) fn expand_cfg_attr(
60-
db: &dyn DefDatabase,
61-
krate: Crate,
58+
pub(crate) fn expand_cfg_attr<'db>(
59+
db: &'db dyn DefDatabase,
60+
cfg_options: impl FnOnce() -> &'db CfgOptions,
6261
raw_attrs: RawAttrs,
6362
) -> Attrs {
64-
Attrs(raw_attrs.expand_cfg_attr(db, krate))
63+
Attrs(raw_attrs.expand_cfg_attr(db, cfg_options))
6564
}
6665

6766
pub(crate) fn is_cfg_enabled_for(
@@ -105,35 +104,32 @@ impl Attrs {
105104
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
106105
let _p = tracing::info_span!("fields_attrs_query").entered();
107106
let mut res = ArenaMap::default();
108-
let (fields, file_id, krate) = match v {
107+
let (fields, file_id, module) = match v {
109108
VariantId::EnumVariantId(it) => {
110109
let loc = it.lookup(db);
111-
let krate = loc.parent.lookup(db).container.krate(db);
112110
let source = loc.source(db);
113-
(source.value.field_list(), source.file_id, krate)
111+
(source.value.field_list(), source.file_id, loc.parent.lookup(db).container)
114112
}
115113
VariantId::StructId(it) => {
116114
let loc = it.lookup(db);
117-
let krate = loc.container.krate(db);
118115
let source = loc.source(db);
119-
(source.value.field_list(), source.file_id, krate)
116+
(source.value.field_list(), source.file_id, loc.container)
120117
}
121118
VariantId::UnionId(it) => {
122119
let loc = it.lookup(db);
123-
let krate = loc.container.krate(db);
124120
let source = loc.source(db);
125121
(
126122
source.value.record_field_list().map(ast::FieldList::RecordFieldList),
127123
source.file_id,
128-
krate,
124+
loc.container,
129125
)
130126
}
131127
};
132128
let Some(fields) = fields else {
133129
return Arc::new(res);
134130
};
135131

136-
let cfg_options = krate.cfg_options(db);
132+
let cfg_options = module.krate(db).cfg_options(db);
137133
let span_map = db.span_map(file_id);
138134

139135
match fields {
@@ -544,7 +540,7 @@ impl AttrsWithOwner {
544540
tree.top_level_raw_attrs().clone()
545541
}
546542
};
547-
Attrs::expand_cfg_attr(db, module.krate(db), raw_attrs)
543+
Attrs::expand_cfg_attr(db, || module.krate(db).cfg_options(db), raw_attrs)
548544
}
549545
AttrDefId::FieldId(it) => db.fields_attrs(it.parent)[it.local_id].clone(),
550546
AttrDefId::EnumVariantId(it) => attrs_from_ast_id_loc(db, it),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,10 @@ impl ExprCollector<'_> {
546546
) -> ExprCollector<'_> {
547547
let (def_map, local_def_map) = module.local_def_map(db);
548548
let expander = Expander::new(db, current_file_id, def_map);
549+
let krate = module.krate(db);
549550
ExprCollector {
550551
db,
551-
cfg_options: module.krate(db).cfg_options(db),
552+
cfg_options: krate.cfg_options(db),
552553
module,
553554
def_map,
554555
local_def_map,
@@ -562,7 +563,7 @@ impl ExprCollector<'_> {
562563
awaitable_context: None,
563564
current_block_legacy_macro_defs_count: FxHashMap::default(),
564565
outer_impl_trait: false,
565-
krate: module.krate(db),
566+
krate,
566567
}
567568
}
568569

crates/hir-def/src/import_map.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,11 @@ impl ImportMap {
141141
if !visited.insert(module) {
142142
continue;
143143
}
144-
let ext_def_map;
145144
let mod_data = if module.krate(db) == krate {
146145
&def_map[module]
147146
} else {
148147
// The crate might reexport a module defined in another crate.
149-
ext_def_map = module.def_map(db);
150-
&ext_def_map[module]
148+
&module.def_map(db)[module]
151149
};
152150

153151
let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {

crates/hir-def/src/item_scope.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,12 @@ impl ItemScope {
261261
pub fn fully_resolve_import(&self, db: &dyn DefDatabase, mut import: ImportId) -> PerNs {
262262
let mut res = PerNs::none();
263263

264-
let mut def_map;
265264
let mut scope = self;
266265
while let Some(&m) = scope.use_imports_macros.get(&ImportOrExternCrate::Import(import)) {
267266
match m {
268267
ImportOrDef::Import(i) => {
269268
let module_id = i.use_.lookup(db).container;
270-
def_map = module_id.def_map(db);
271-
scope = &def_map[module_id].scope;
269+
scope = &module_id.def_map(db)[module_id].scope;
272270
import = i;
273271
}
274272
ImportOrDef::Def(ModuleDefId::MacroId(def)) => {
@@ -283,8 +281,7 @@ impl ItemScope {
283281
match m {
284282
ImportOrDef::Import(i) => {
285283
let module_id = i.use_.lookup(db).container;
286-
def_map = module_id.def_map(db);
287-
scope = &def_map[module_id].scope;
284+
scope = &module_id.def_map(db)[module_id].scope;
288285
import = i;
289286
}
290287
ImportOrDef::Def(def) => {
@@ -299,8 +296,7 @@ impl ItemScope {
299296
match m {
300297
ImportOrDef::Import(i) => {
301298
let module_id = i.use_.lookup(db).container;
302-
def_map = module_id.def_map(db);
303-
scope = &def_map[module_id].scope;
299+
scope = &module_id.def_map(db)[module_id].scope;
304300
import = i;
305301
}
306302
ImportOrDef::Def(def) => {
@@ -912,10 +908,7 @@ impl ItemInNs {
912908

913909
/// Returns the crate defining this item (or `None` if `self` is built-in).
914910
pub fn krate(&self, db: &dyn DefDatabase) -> Option<Crate> {
915-
match self {
916-
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db).map(|m| m.krate(db)),
917-
ItemInNs::Macros(id) => Some(id.module(db).krate(db)),
918-
}
911+
self.module(db).map(|module_id| module_id.krate(db))
919912
}
920913

921914
pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {

crates/hir-def/src/item_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl ItemTree {
203203

204204
/// Returns the inner attributes of the source file.
205205
pub(crate) fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
206-
Attrs::expand_cfg_attr(db, krate, self.top_attrs.clone())
206+
Attrs::expand_cfg_attr(db, || krate.cfg_options(db), self.top_attrs.clone())
207207
}
208208

209209
pub(crate) fn raw_attrs(&self, of: FileAstId<ast::Item>) -> &RawAttrs {
@@ -216,7 +216,7 @@ impl ItemTree {
216216
krate: Crate,
217217
of: FileAstId<ast::Item>,
218218
) -> Attrs {
219-
Attrs::expand_cfg_attr(db, krate, self.raw_attrs(of).clone())
219+
Attrs::expand_cfg_attr(db, || krate.cfg_options(db), self.raw_attrs(of).clone())
220220
}
221221

222222
/// Returns a count of a few, expensive items.

crates/hir-def/src/nameres.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,11 @@ pub fn block_def_map(db: &dyn DefDatabase, block_id: BlockId) -> DefMap {
420420
let module_data =
421421
ModuleData::new(ModuleOrigin::BlockExpr { block: ast_id, id: block_id }, visibility, None);
422422

423-
let local_def_map = crate_local_def_map(db, module.krate(db));
423+
let krate = module.krate(db);
424+
let local_def_map = crate_local_def_map(db, krate);
424425
let def_map = DefMap::empty(
425426
db,
426-
module.krate(db),
427+
krate,
427428
local_def_map.def_map(db).data.clone(),
428429
module_data,
429430
Some(BlockInfo { block: block_id, parent: module }),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,6 @@ impl<'db> DefCollector<'db> {
10741074
vis: Visibility,
10751075
import: Option<ImportOrExternCrate>,
10761076
) {
1077-
self.db.unwind_if_revision_cancelled();
10781077
self.update_recursive(module_id, resolutions, vis, import, 0)
10791078
}
10801079

@@ -1706,7 +1705,7 @@ impl ModCollector<'_, '_> {
17061705
// Prelude module is always considered to be `#[macro_use]`.
17071706
if let Some((prelude_module, _use)) = self.def_collector.def_map.prelude {
17081707
// Don't insert macros from the prelude into blocks, as they can be shadowed by other macros.
1709-
if prelude_module.krate(self.def_collector.db) != krate && is_crate_root {
1708+
if is_crate_root && prelude_module.krate(self.def_collector.db) != krate {
17101709
cov_mark::hit!(prelude_is_macro_use);
17111710
self.def_collector.import_macros_from_extern_crate(
17121711
prelude_module.krate(self.def_collector.db),

crates/hir-def/src/resolver.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,7 @@ impl HasResolver for ModuleId {
12821282
fn resolver(self, db: &dyn DefDatabase) -> Resolver<'_> {
12831283
let (mut def_map, local_def_map) = self.local_def_map(db);
12841284
let mut module_id = self;
1285-
1286-
if !self.is_block_module(db) {
1285+
if def_map.parent().is_none() || module_id != def_map.root_module_id() {
12871286
return Resolver {
12881287
scopes: vec![],
12891288
module_scope: ModuleItemMap { def_map, local_def_map, module_id },
@@ -1292,10 +1291,10 @@ impl HasResolver for ModuleId {
12921291

12931292
let mut modules: SmallVec<[_; 1]> = smallvec![];
12941293
while let Some(parent) = def_map.parent() {
1295-
let block_def_map = mem::replace(&mut def_map, parent.def_map(db));
1296-
modules.push(block_def_map);
1297-
if !parent.is_block_module(db) {
1298-
module_id = parent;
1294+
modules.push(def_map);
1295+
def_map = parent.def_map(db);
1296+
module_id = parent;
1297+
if module_id != def_map.root_module_id() {
12991298
break;
13001299
}
13011300
}

crates/hir-expand/src/attrs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::iter;
33
use std::{borrow::Cow, fmt, ops};
44

5-
use base_db::Crate;
65
use cfg::{CfgExpr, CfgOptions};
76
use either::Either;
87
use intern::{Interned, Symbol, sym};
@@ -141,14 +140,18 @@ impl RawAttrs {
141140
}
142141

143142
/// Processes `cfg_attr`s
144-
pub fn expand_cfg_attr(self, db: &dyn ExpandDatabase, krate: Crate) -> RawAttrs {
143+
pub fn expand_cfg_attr<'db>(
144+
self,
145+
db: &'db dyn ExpandDatabase,
146+
cfg_options: impl FnOnce() -> &'db CfgOptions,
147+
) -> RawAttrs {
145148
let has_cfg_attrs =
146149
self.iter().any(|attr| attr.path.as_ident().is_some_and(|name| *name == sym::cfg_attr));
147150
if !has_cfg_attrs {
148151
return self;
149152
}
150153

151-
let cfg_options = krate.cfg_options(db);
154+
let cfg_options = cfg_options();
152155
let new_attrs = self
153156
.iter()
154157
.cloned()

0 commit comments

Comments
 (0)