Skip to content

Commit c681cf7

Browse files
committed
def_collector: Factor out common field handling code
1 parent fc9f13e commit c681cf7

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

src/librustc/hir/map/def_collector.rs

+21-26
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'a> DefCollector<'a> {
3131
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
3232
}
3333

34-
pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
34+
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
3535
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
3636
f(self);
3737
self.parent_def = orig_parent_def;
@@ -74,6 +74,22 @@ impl<'a> DefCollector<'a> {
7474
})
7575
}
7676

77+
fn collect_field(&mut self, field: &'a StructField, index: Option<usize>) {
78+
if field.is_placeholder {
79+
self.visit_macro_invoc(field.id);
80+
} else {
81+
let name = field.ident.map(|ident| ident.name)
82+
.or_else(|| index.map(sym::integer))
83+
.unwrap_or_else(|| {
84+
let node_id = NodeId::placeholder_from_expn_id(self.expansion);
85+
sym::integer(self.definitions.placeholder_field_indices[&node_id])
86+
})
87+
.as_interned_str();
88+
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
89+
self.with_parent(def, |this| visit::walk_struct_field(this, field));
90+
}
91+
}
92+
7793
pub fn visit_macro_invoc(&mut self, id: NodeId) {
7894
self.definitions.set_invocation_parent(id.placeholder_to_expn_id(), self.parent_def);
7995
}
@@ -174,17 +190,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
174190
// It currently holds because only inert attributes are accepted on fields,
175191
// and every such attribute expands into a single field after it's resolved.
176192
for (index, field) in data.fields().iter().enumerate() {
177-
if field.is_placeholder {
178-
self.visit_macro_invoc(field.id);
193+
self.collect_field(field, Some(index));
194+
if field.is_placeholder && field.ident.is_none() {
179195
self.definitions.placeholder_field_indices.insert(field.id, index);
180-
continue;
181196
}
182-
let name = field.ident.map(|ident| ident.name)
183-
.unwrap_or_else(|| sym::integer(index));
184-
let def = self.create_def(field.id,
185-
DefPathData::ValueNs(name.as_interned_str()),
186-
field.span);
187-
self.with_parent(def, |this| visit::walk_struct_field(this, field));
188197
}
189198
}
190199

@@ -344,21 +353,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
344353

345354
// This method is called only when we are visiting an individual field
346355
// after expanding an attribute on it.
347-
fn visit_struct_field(&mut self, sf: &'a StructField) {
348-
if sf.is_placeholder {
349-
self.visit_macro_invoc(sf.id)
350-
} else {
351-
let name = sf.ident.map_or_else(
352-
|| {
353-
let expn_id = NodeId::placeholder_from_expn_id(self.expansion);
354-
sym::integer(self.definitions.placeholder_field_indices[&expn_id])
355-
},
356-
|ident| ident.name,
357-
);
358-
let def = self.create_def(sf.id,
359-
DefPathData::ValueNs(name.as_interned_str()),
360-
sf.span);
361-
self.with_parent(def, |this| visit::walk_struct_field(this, sf));
362-
}
356+
fn visit_struct_field(&mut self, field: &'a StructField) {
357+
self.collect_field(field, None);
363358
}
364359
}

0 commit comments

Comments
 (0)