Skip to content

Commit 63e1707

Browse files
committed
Remove unnecessary item tree query calls
1 parent 4f1a07c commit 63e1707

File tree

1 file changed

+47
-49
lines changed
  • src/tools/rust-analyzer/crates/hir-def/src/nameres

1 file changed

+47
-49
lines changed

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
3737
item_tree::{
3838
self, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, Macro2, MacroCall,
39-
MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
39+
MacroRules, Mod, ModItemId, ModKind, TreeId,
4040
},
4141
macro_call_as_call_id,
4242
nameres::{
@@ -140,8 +140,6 @@ struct ImportSource {
140140
id: UseId,
141141
is_prelude: bool,
142142
kind: ImportKind,
143-
tree: TreeId,
144-
item: FileAstId<ast::Use>,
145143
}
146144

147145
#[derive(Debug, Eq, PartialEq)]
@@ -155,7 +153,6 @@ struct Import {
155153
impl Import {
156154
fn from_use(
157155
tree: &ItemTree,
158-
tree_id: TreeId,
159156
item: FileAstId<ast::Use>,
160157
id: UseId,
161158
is_prelude: bool,
@@ -168,7 +165,7 @@ impl Import {
168165
path,
169166
alias,
170167
visibility: visibility.clone(),
171-
source: ImportSource { use_tree: idx, id, is_prelude, kind, tree: tree_id, item },
168+
source: ImportSource { use_tree: idx, id, is_prelude, kind },
172169
});
173170
});
174171
}
@@ -183,15 +180,15 @@ struct ImportDirective {
183180
}
184181

185182
#[derive(Clone, Debug, Eq, PartialEq)]
186-
struct MacroDirective {
183+
struct MacroDirective<'db> {
187184
module_id: LocalModuleId,
188185
depth: usize,
189-
kind: MacroDirectiveKind,
186+
kind: MacroDirectiveKind<'db>,
190187
container: ItemContainerId,
191188
}
192189

193190
#[derive(Clone, Debug, Eq, PartialEq)]
194-
enum MacroDirectiveKind {
191+
enum MacroDirectiveKind<'db> {
195192
FnLike {
196193
ast_id: AstIdWithPath<ast::MacroCall>,
197194
expand_to: ExpandTo,
@@ -210,28 +207,29 @@ enum MacroDirectiveKind {
210207
attr: Attr,
211208
mod_item: ModItemId,
212209
/* is this needed? */ tree: TreeId,
210+
item_tree: &'db ItemTree,
213211
},
214212
}
215213

216214
/// Walks the tree of module recursively
217-
struct DefCollector<'a> {
218-
db: &'a dyn DefDatabase,
215+
struct DefCollector<'db> {
216+
db: &'db dyn DefDatabase,
219217
def_map: DefMap,
220218
local_def_map: LocalDefMap,
221219
/// Set only in case of blocks.
222-
crate_local_def_map: Option<&'a LocalDefMap>,
220+
crate_local_def_map: Option<&'db LocalDefMap>,
223221
// The dependencies of the current crate, including optional deps like `test`.
224222
deps: FxHashMap<Name, BuiltDependency>,
225223
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility, GlobId)>>,
226224
unresolved_imports: Vec<ImportDirective>,
227225
indeterminate_imports: Vec<(ImportDirective, PerNs)>,
228-
unresolved_macros: Vec<MacroDirective>,
226+
unresolved_macros: Vec<MacroDirective<'db>>,
229227
// We'd like to avoid emitting a diagnostics avalanche when some `extern crate` doesn't
230228
// resolve. When we emit diagnostics for unresolved imports, we only do so if the import
231229
// doesn't start with an unresolved crate's name.
232230
unresolved_extern_crates: FxHashSet<Name>,
233231
mod_dirs: FxHashMap<LocalModuleId, ModDir>,
234-
cfg_options: &'a CfgOptions,
232+
cfg_options: &'db CfgOptions,
235233
/// List of procedural macros defined by this crate. This is read from the dynamic library
236234
/// built by the build system, and is the list of proc-macros we can actually expand. It is
237235
/// empty when proc-macro support is disabled (in which case we still do name resolution for
@@ -249,7 +247,7 @@ struct DefCollector<'a> {
249247
skip_attrs: FxHashMap<InFile<FileAstId<ast::Item>>, AttrId>,
250248
}
251249

252-
impl DefCollector<'_> {
250+
impl<'db> DefCollector<'db> {
253251
fn seed_with_top_level(&mut self) {
254252
let _p = tracing::info_span!("seed_with_top_level").entered();
255253

@@ -461,7 +459,7 @@ impl DefCollector<'_> {
461459
self.unresolved_macros.iter().enumerate().find_map(|(idx, directive)| match &directive
462460
.kind
463461
{
464-
MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree } => {
462+
MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree, item_tree } => {
465463
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
466464
directive.module_id,
467465
MacroCallKind::Attr {
@@ -474,14 +472,20 @@ impl DefCollector<'_> {
474472

475473
self.skip_attrs.insert(ast_id.ast_id.with_value(mod_item.ast_id()), attr.id);
476474

477-
Some((idx, directive, *mod_item, *tree))
475+
Some((idx, directive, *mod_item, *tree, *item_tree))
478476
}
479477
_ => None,
480478
});
481479

482480
match unresolved_attr {
483-
Some((pos, &MacroDirective { module_id, depth, container, .. }, mod_item, tree_id)) => {
484-
let item_tree = &tree_id.item_tree(self.db);
481+
Some((
482+
pos,
483+
&MacroDirective { module_id, depth, container, .. },
484+
mod_item,
485+
tree_id,
486+
item_tree,
487+
)) => {
488+
// FIXME: Remove this clone
485489
let mod_dir = self.mod_dirs[&module_id].clone();
486490
ModCollector {
487491
def_collector: self,
@@ -862,8 +866,6 @@ impl DefCollector<'_> {
862866
kind: kind @ (ImportKind::Plain | ImportKind::TypeOnly),
863867
id,
864868
use_tree,
865-
tree,
866-
item,
867869
..
868870
} => {
869871
let name = match &import.alias {
@@ -896,13 +898,11 @@ impl DefCollector<'_> {
896898
let Some(ImportOrExternCrate::ExternCrate(_)) = def.import else {
897899
return false;
898900
};
899-
let item_tree = tree.item_tree(self.db);
900-
let use_kind = item_tree[item].use_tree.kind();
901-
let UseTreeKind::Single { path, .. } = use_kind else {
901+
if kind == ImportKind::Glob {
902902
return false;
903-
};
904-
matches!(path.kind, PathKind::Plain | PathKind::SELF)
905-
&& path.segments().len() < 2
903+
}
904+
matches!(import.path.kind, PathKind::Plain | PathKind::SELF)
905+
&& import.path.segments().len() < 2
906906
};
907907
if is_extern_crate_reimport_without_prefix() {
908908
def.vis = vis;
@@ -1256,7 +1256,7 @@ impl DefCollector<'_> {
12561256
fn resolve_macros(&mut self) -> ReachedFixedPoint {
12571257
let mut macros = mem::take(&mut self.unresolved_macros);
12581258
let mut resolved = Vec::new();
1259-
let mut push_resolved = |directive: &MacroDirective, call_id| {
1259+
let mut push_resolved = |directive: &MacroDirective<'_>, call_id| {
12601260
resolved.push((directive.module_id, directive.depth, directive.container, call_id));
12611261
};
12621262

@@ -1269,7 +1269,7 @@ impl DefCollector<'_> {
12691269
let mut eager_callback_buffer = vec![];
12701270
let mut res = ReachedFixedPoint::Yes;
12711271
// Retain unresolved macros after this round of resolution.
1272-
let mut retain = |directive: &MacroDirective| {
1272+
let mut retain = |directive: &MacroDirective<'db>| {
12731273
let subns = match &directive.kind {
12741274
MacroDirectiveKind::FnLike { .. } => MacroSubNs::Bang,
12751275
MacroDirectiveKind::Attr { .. } | MacroDirectiveKind::Derive { .. } => {
@@ -1364,7 +1364,13 @@ impl DefCollector<'_> {
13641364
return Resolved::Yes;
13651365
}
13661366
}
1367-
MacroDirectiveKind::Attr { ast_id: file_ast_id, mod_item, attr, tree } => {
1367+
MacroDirectiveKind::Attr {
1368+
ast_id: file_ast_id,
1369+
mod_item,
1370+
attr,
1371+
tree,
1372+
item_tree,
1373+
} => {
13681374
let &AstIdWithPath { ast_id, ref path } = file_ast_id;
13691375
let file_id = ast_id.file_id;
13701376

@@ -1375,7 +1381,6 @@ impl DefCollector<'_> {
13751381
.skip_attrs
13761382
.insert(InFile::new(file_id, mod_item.ast_id()), attr.id);
13771383

1378-
let item_tree = tree.item_tree(self.db);
13791384
ModCollector {
13801385
def_collector: collector,
13811386
macro_depth: directive.depth,
@@ -1646,8 +1651,7 @@ impl DefCollector<'_> {
16461651
import:
16471652
Import {
16481653
ref path,
1649-
source:
1650-
ImportSource { use_tree, id, is_prelude: _, kind: _, tree: _, item: _ },
1654+
source: ImportSource { use_tree, id, is_prelude: _, kind: _ },
16511655
..
16521656
},
16531657
..
@@ -1671,12 +1675,12 @@ impl DefCollector<'_> {
16711675
}
16721676

16731677
/// Walks a single module, populating defs, imports and macros
1674-
struct ModCollector<'a, 'b> {
1675-
def_collector: &'a mut DefCollector<'b>,
1678+
struct ModCollector<'a, 'db> {
1679+
def_collector: &'a mut DefCollector<'db>,
16761680
macro_depth: usize,
16771681
module_id: LocalModuleId,
16781682
tree_id: TreeId,
1679-
item_tree: &'a ItemTree,
1683+
item_tree: &'db ItemTree,
16801684
mod_dir: ModDir,
16811685
}
16821686

@@ -1753,20 +1757,13 @@ impl ModCollector<'_, '_> {
17531757
UseLoc { container: module, id: InFile::new(self.file_id(), item_tree_id) }
17541758
.intern(db);
17551759
let is_prelude = attrs.by_key(sym::prelude_import).exists();
1756-
Import::from_use(
1757-
self.item_tree,
1758-
self.tree_id,
1759-
item_tree_id,
1760-
id,
1761-
is_prelude,
1762-
|import| {
1763-
self.def_collector.unresolved_imports.push(ImportDirective {
1764-
module_id: self.module_id,
1765-
import,
1766-
status: PartialResolvedImport::Unresolved,
1767-
});
1768-
},
1769-
)
1760+
Import::from_use(self.item_tree, item_tree_id, id, is_prelude, |import| {
1761+
self.def_collector.unresolved_imports.push(ImportDirective {
1762+
module_id: self.module_id,
1763+
import,
1764+
status: PartialResolvedImport::Unresolved,
1765+
});
1766+
})
17701767
}
17711768
ModItemId::ExternCrate(item_tree_id) => {
17721769
let item_tree::ExternCrate { name, visibility, alias } =
@@ -2268,6 +2265,7 @@ impl ModCollector<'_, '_> {
22682265
attr: attr.clone(),
22692266
mod_item,
22702267
tree: self.tree_id,
2268+
item_tree: self.item_tree,
22712269
},
22722270
container,
22732271
});

0 commit comments

Comments
 (0)