Skip to content

Commit d3796ad

Browse files
committed
Auto merge of rust-lang#12772 - Veykril:nameres, r=Veykril
internal: Remove allocation in DefCollector::reseed_with_unresolved_attribute
2 parents 2e9d5b5 + 25090f0 commit d3796ad

File tree

2 files changed

+60
-65
lines changed

2 files changed

+60
-65
lines changed

crates/hir-def/src/item_scope.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -270,35 +270,33 @@ impl ItemScope {
270270
$glob_imports:ident [ $lookup:ident ],
271271
$def_import_type:ident
272272
) => {{
273-
let existing = $this.$field.entry($lookup.1.clone());
274-
match (existing, $def.$field) {
275-
(Entry::Vacant(entry), Some(_)) => {
276-
match $def_import_type {
277-
ImportType::Glob => {
278-
$glob_imports.$field.insert($lookup.clone());
273+
if let Some(fld) = $def.$field {
274+
let existing = $this.$field.entry($lookup.1.clone());
275+
match existing {
276+
Entry::Vacant(entry) => {
277+
match $def_import_type {
278+
ImportType::Glob => {
279+
$glob_imports.$field.insert($lookup.clone());
280+
}
281+
ImportType::Named => {
282+
$glob_imports.$field.remove(&$lookup);
283+
}
279284
}
280-
ImportType::Named => {
281-
$glob_imports.$field.remove(&$lookup);
282-
}
283-
}
284285

285-
if let Some(fld) = $def.$field {
286286
entry.insert(fld);
287+
$changed = true;
287288
}
288-
$changed = true;
289-
}
290-
(Entry::Occupied(mut entry), Some(_))
291-
if $glob_imports.$field.contains(&$lookup)
292-
&& matches!($def_import_type, ImportType::Named) =>
293-
{
294-
cov_mark::hit!(import_shadowed);
295-
$glob_imports.$field.remove(&$lookup);
296-
if let Some(fld) = $def.$field {
289+
Entry::Occupied(mut entry)
290+
if $glob_imports.$field.contains(&$lookup)
291+
&& matches!($def_import_type, ImportType::Named) =>
292+
{
293+
cov_mark::hit!(import_shadowed);
294+
$glob_imports.$field.remove(&$lookup);
297295
entry.insert(fld);
296+
$changed = true;
298297
}
299-
$changed = true;
298+
_ => {}
300299
}
301-
_ => {}
302300
}
303301
}};
304302
}

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

+40-43
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,8 @@ impl DefCollector<'_> {
395395
// As some of the macros will expand newly import shadowing partial resolved imports
396396
// FIXME: We maybe could skip this, if we handle the indeterminate imports in `resolve_imports`
397397
// correctly
398-
let partial_resolved = self.indeterminate_imports.drain(..).filter_map(|mut directive| {
399-
directive.status = PartialResolvedImport::Unresolved;
400-
Some(directive)
398+
let partial_resolved = self.indeterminate_imports.drain(..).map(|directive| {
399+
ImportDirective { status: PartialResolvedImport::Unresolved, ..directive }
401400
});
402401
self.unresolved_imports.extend(partial_resolved);
403402
self.resolve_imports();
@@ -434,50 +433,48 @@ impl DefCollector<'_> {
434433
fn reseed_with_unresolved_attribute(&mut self) -> ReachedFixedPoint {
435434
cov_mark::hit!(unresolved_attribute_fallback);
436435

437-
let mut unresolved_macros = mem::take(&mut self.unresolved_macros);
438-
let pos = unresolved_macros.iter().position(|directive| {
439-
if let MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree } = &directive.kind {
440-
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
441-
directive.module_id,
442-
MacroCallKind::Attr {
443-
ast_id: ast_id.ast_id,
444-
attr_args: Default::default(),
445-
invoc_attr_index: attr.id.ast_index,
446-
is_derive: false,
447-
},
448-
attr.path().clone(),
449-
));
436+
let unresolved_attr =
437+
self.unresolved_macros.iter().enumerate().find_map(|(idx, directive)| match &directive
438+
.kind
439+
{
440+
MacroDirectiveKind::Attr { ast_id, mod_item, attr, tree } => {
441+
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
442+
directive.module_id,
443+
MacroCallKind::Attr {
444+
ast_id: ast_id.ast_id,
445+
attr_args: Default::default(),
446+
invoc_attr_index: attr.id.ast_index,
447+
is_derive: false,
448+
},
449+
attr.path().clone(),
450+
));
450451

451-
self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
452+
self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
452453

453-
let item_tree = tree.item_tree(self.db);
454-
let mod_dir = self.mod_dirs[&directive.module_id].clone();
454+
Some((idx, directive, *mod_item, *tree))
455+
}
456+
_ => None,
457+
});
458+
459+
match unresolved_attr {
460+
Some((pos, &MacroDirective { module_id, depth, container, .. }, mod_item, tree_id)) => {
461+
let item_tree = &tree_id.item_tree(self.db);
462+
let mod_dir = self.mod_dirs[&module_id].clone();
455463
ModCollector {
456464
def_collector: self,
457-
macro_depth: directive.depth,
458-
module_id: directive.module_id,
459-
tree_id: *tree,
460-
item_tree: &item_tree,
465+
macro_depth: depth,
466+
module_id,
467+
tree_id,
468+
item_tree,
461469
mod_dir,
462470
}
463-
.collect(&[*mod_item], directive.container);
464-
true
465-
} else {
466-
false
467-
}
468-
});
469-
470-
if let Some(pos) = pos {
471-
unresolved_macros.swap_remove(pos);
472-
}
473-
474-
self.unresolved_macros.extend(unresolved_macros);
471+
.collect(&[mod_item], container);
475472

476-
if pos.is_some() {
477-
// Continue name resolution with the new data.
478-
ReachedFixedPoint::No
479-
} else {
480-
ReachedFixedPoint::Yes
473+
self.unresolved_macros.swap_remove(pos);
474+
// Continue name resolution with the new data.
475+
ReachedFixedPoint::No
476+
}
477+
None => ReachedFixedPoint::Yes,
481478
}
482479
}
483480

@@ -722,7 +719,8 @@ impl DefCollector<'_> {
722719
fn resolve_imports(&mut self) -> ReachedFixedPoint {
723720
let mut res = ReachedFixedPoint::Yes;
724721
let imports = mem::take(&mut self.unresolved_imports);
725-
let imports = imports
722+
723+
self.unresolved_imports = imports
726724
.into_iter()
727725
.filter_map(|mut directive| {
728726
directive.status = self.resolve_import(directive.module_id, &directive.import);
@@ -742,7 +740,6 @@ impl DefCollector<'_> {
742740
}
743741
})
744742
.collect();
745-
self.unresolved_imports = imports;
746743
res
747744
}
748745

@@ -1034,7 +1031,7 @@ impl DefCollector<'_> {
10341031
.glob_imports
10351032
.get(&module_id)
10361033
.into_iter()
1037-
.flat_map(|v| v.iter())
1034+
.flatten()
10381035
.filter(|(glob_importing_module, _)| {
10391036
// we know all resolutions have the same visibility (`vis`), so we
10401037
// just need to check that once

0 commit comments

Comments
 (0)