Skip to content

Commit c215de0

Browse files
authored
Rollup merge of #66673 - petrochenkov:toresolve, r=Centril
Move def collector from `rustc` to `rustc_resolve` It's used only from `rustc_resolve`, so we can move it there, thus reducing the size of `rustc` (#65031). It's quite possible that we can merge the def collector pass into the "build reduced graph" pass (they are always run together and do similar things), but it's some larger work. r? @eddyb
2 parents 0bb06cb + bbbdbb0 commit c215de0

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

src/librustc/hir/map/definitions.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub struct Definitions {
105105
/// we know what parent node that fragment should be attached to thanks to this table.
106106
invocation_parents: FxHashMap<ExpnId, DefIndex>,
107107
/// Indices of unnamed struct or variant fields with unresolved attributes.
108-
pub(super) placeholder_field_indices: NodeMap<usize>,
108+
placeholder_field_indices: NodeMap<usize>,
109109
}
110110

111111
/// A unique identifier that we can use to lookup a definition
@@ -535,6 +535,15 @@ impl Definitions {
535535
let old_parent = self.invocation_parents.insert(invoc_id, parent);
536536
assert!(old_parent.is_none(), "parent `DefIndex` is reset for an invocation");
537537
}
538+
539+
pub fn placeholder_field_index(&self, node_id: ast::NodeId) -> usize {
540+
self.placeholder_field_indices[&node_id]
541+
}
542+
543+
pub fn set_placeholder_field_index(&mut self, node_id: ast::NodeId, index: usize) {
544+
let old_index = self.placeholder_field_indices.insert(node_id, index);
545+
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
546+
}
538547
}
539548

540549
impl DefPathData {

src/librustc/hir/map/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use self::collector::NodeCollector;
2-
pub use self::def_collector::DefCollector;
32
pub use self::definitions::{
43
Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData, DefPathHash
54
};
@@ -25,7 +24,6 @@ use syntax_pos::{Span, DUMMY_SP};
2524

2625
pub mod blocks;
2726
mod collector;
28-
mod def_collector;
2927
pub mod definitions;
3028
mod hir_id_validator;
3129

src/librustc_resolve/build_reduced_graph.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! unexpanded macros in the fragment are visited and registered.
66
//! Imports are also considered items and placed into modules here, but not resolved yet.
77
8+
use crate::def_collector::collect_definitions;
89
use crate::macros::{LegacyBinding, LegacyScope};
910
use crate::resolve_imports::ImportDirective;
1011
use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
@@ -16,7 +17,6 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
1617
use rustc::bug;
1718
use rustc::hir::def::{self, *};
1819
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
19-
use rustc::hir::map::DefCollector;
2020
use rustc::ty;
2121
use rustc::middle::cstore::CrateStore;
2222
use rustc_metadata::cstore::LoadedMacro;
@@ -167,8 +167,7 @@ impl<'a> Resolver<'a> {
167167
fragment: &AstFragment,
168168
parent_scope: ParentScope<'a>,
169169
) -> LegacyScope<'a> {
170-
let mut def_collector = DefCollector::new(&mut self.definitions, parent_scope.expansion);
171-
fragment.visit_with(&mut def_collector);
170+
collect_definitions(&mut self.definitions, fragment, parent_scope.expansion);
172171
let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope };
173172
fragment.visit_with(&mut visitor);
174173
visitor.parent_scope.legacy

src/librustc/hir/map/def_collector.rs src/librustc_resolve/def_collector.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
use crate::hir::map::definitions::*;
2-
use crate::hir::def_id::DefIndex;
3-
1+
use log::debug;
2+
use rustc::hir::map::definitions::*;
3+
use rustc::hir::def_id::DefIndex;
44
use syntax::ast::*;
55
use syntax::visit;
66
use syntax::symbol::{kw, sym};
77
use syntax::token::{self, Token};
8+
use syntax_expand::expand::AstFragment;
89
use syntax_pos::hygiene::ExpnId;
910
use syntax_pos::Span;
1011

12+
crate fn collect_definitions(
13+
definitions: &mut Definitions,
14+
fragment: &AstFragment,
15+
expansion: ExpnId,
16+
) {
17+
let parent_def = definitions.invocation_parent(expansion);
18+
fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion });
19+
}
20+
1121
/// Creates `DefId`s for nodes in the AST.
12-
pub struct DefCollector<'a> {
22+
struct DefCollector<'a> {
1323
definitions: &'a mut Definitions,
1424
parent_def: DefIndex,
1525
expansion: ExpnId,
1626
}
1727

1828
impl<'a> DefCollector<'a> {
19-
pub fn new(definitions: &'a mut Definitions, expansion: ExpnId) -> Self {
20-
let parent_def = definitions.invocation_parent(expansion);
21-
DefCollector { definitions, parent_def, expansion }
22-
}
23-
2429
fn create_def(&mut self,
2530
node_id: NodeId,
2631
data: DefPathData,
@@ -82,7 +87,7 @@ impl<'a> DefCollector<'a> {
8287
.or_else(|| index.map(sym::integer))
8388
.unwrap_or_else(|| {
8489
let node_id = NodeId::placeholder_from_expn_id(self.expansion);
85-
sym::integer(self.definitions.placeholder_field_indices[&node_id])
90+
sym::integer(self.definitions.placeholder_field_index(node_id))
8691
});
8792
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
8893
self.with_parent(def, |this| visit::walk_struct_field(this, field));
@@ -186,7 +191,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
186191
for (index, field) in data.fields().iter().enumerate() {
187192
self.collect_field(field, Some(index));
188193
if field.is_placeholder && field.ident.is_none() {
189-
self.definitions.placeholder_field_indices.insert(field.id, index);
194+
self.definitions.set_placeholder_field_index(field.id, index);
190195
}
191196
}
192197
}

src/librustc_resolve/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use rustc_error_codes::*;
6868

6969
type Res = def::Res<NodeId>;
7070

71+
mod def_collector;
7172
mod diagnostics;
7273
mod late;
7374
mod macros;

0 commit comments

Comments
 (0)