Skip to content

Commit e2c962d

Browse files
committed
resolve: Cleanup some field processing code
1 parent 9e6725d commit e2c962d

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/librustc_resolve/build_reduced_graph.rs

+31-32
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
299299
}
300300
}
301301

302+
fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
303+
let field_names = vdata.fields().iter().map(|field| {
304+
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
305+
}).collect();
306+
self.insert_field_names(def_id, field_names);
307+
}
308+
302309
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Spanned<Name>>) {
303310
if !field_names.is_empty() {
304311
self.r.field_names.insert(def_id, field_names);
@@ -736,58 +743,50 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
736743
}
737744

738745
// These items live in both the type and value namespaces.
739-
ItemKind::Struct(ref struct_def, _) => {
746+
ItemKind::Struct(ref vdata, _) => {
740747
// Define a name in the type namespace.
741748
let def_id = self.r.definitions.local_def_id(item.id);
742749
let res = Res::Def(DefKind::Struct, def_id);
743750
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
744751

745-
let mut ctor_vis = vis;
746-
747-
let has_non_exhaustive = attr::contains_name(&item.attrs, sym::non_exhaustive);
748-
749-
// If the structure is marked as non_exhaustive then lower the visibility
750-
// to within the crate.
751-
if has_non_exhaustive && vis == ty::Visibility::Public {
752-
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
753-
}
754-
755752
// Record field names for error reporting.
756-
let field_names = struct_def.fields().iter().map(|field| {
757-
// NOTE: The field may be an expansion placeholder, but expansion sets correct
758-
// visibilities for unnamed field placeholders specifically, so the constructor
759-
// visibility should still be determined correctly.
760-
let field_vis = self.resolve_visibility_speculative(&field.vis, true);
761-
if ctor_vis.is_at_least(field_vis, &*self.r) {
762-
ctor_vis = field_vis;
763-
}
764-
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
765-
}).collect();
766-
let item_def_id = self.r.definitions.local_def_id(item.id);
767-
self.insert_field_names(item_def_id, field_names);
753+
self.insert_field_names_local(def_id, vdata);
768754

769755
// If this is a tuple or unit struct, define a name
770756
// in the value namespace as well.
771-
if let Some(ctor_node_id) = struct_def.ctor_id() {
757+
if let Some(ctor_node_id) = vdata.ctor_id() {
758+
let mut ctor_vis = vis;
759+
// If the structure is marked as non_exhaustive then lower the visibility
760+
// to within the crate.
761+
if vis == ty::Visibility::Public &&
762+
attr::contains_name(&item.attrs, sym::non_exhaustive) {
763+
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
764+
}
765+
for field in vdata.fields() {
766+
// NOTE: The field may be an expansion placeholder, but expansion sets
767+
// correct visibilities for unnamed field placeholders specifically, so the
768+
// constructor visibility should still be determined correctly.
769+
let field_vis = self.resolve_visibility_speculative(&field.vis, true);
770+
if ctor_vis.is_at_least(field_vis, &*self.r) {
771+
ctor_vis = field_vis;
772+
}
773+
}
772774
let ctor_res = Res::Def(
773-
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(struct_def)),
775+
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(vdata)),
774776
self.r.definitions.local_def_id(ctor_node_id),
775777
);
776778
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
777-
self.r.struct_constructors.insert(res.def_id(), (ctor_res, ctor_vis));
779+
self.r.struct_constructors.insert(def_id, (ctor_res, ctor_vis));
778780
}
779781
}
780782

781783
ItemKind::Union(ref vdata, _) => {
782-
let res = Res::Def(DefKind::Union, self.r.definitions.local_def_id(item.id));
784+
let def_id = self.r.definitions.local_def_id(item.id);
785+
let res = Res::Def(DefKind::Union, def_id);
783786
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
784787

785788
// Record field names for error reporting.
786-
let field_names = vdata.fields().iter().map(|field| {
787-
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
788-
}).collect();
789-
let item_def_id = self.r.definitions.local_def_id(item.id);
790-
self.insert_field_names(item_def_id, field_names);
789+
self.insert_field_names_local(def_id, vdata);
791790
}
792791

793792
ItemKind::Impl(.., ref impl_items) => {

0 commit comments

Comments
 (0)