Skip to content

Commit d9d7203

Browse files
petrochenkovMark-Simulacrum
authored andcommitted
resolve: Cleanup some field processing code
1 parent 7463e29 commit d9d7203

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
@@ -301,6 +301,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
301301
}
302302
}
303303

304+
fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
305+
let field_names = vdata.fields().iter().map(|field| {
306+
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
307+
}).collect();
308+
self.insert_field_names(def_id, field_names);
309+
}
310+
304311
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Spanned<Name>>) {
305312
if !field_names.is_empty() {
306313
self.r.field_names.insert(def_id, field_names);
@@ -734,58 +741,50 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
734741
}
735742

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

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

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

779781
ItemKind::Union(ref vdata, _) => {
780-
let res = Res::Def(DefKind::Union, self.r.definitions.local_def_id(item.id));
782+
let def_id = self.r.definitions.local_def_id(item.id);
783+
let res = Res::Def(DefKind::Union, def_id);
781784
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
782785

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

791790
ItemKind::Trait(..) => {

0 commit comments

Comments
 (0)