Skip to content

Commit 8b60b94

Browse files
committed
Get rid of hir::StructFieldKind
1 parent 0400d92 commit 8b60b94

File tree

13 files changed

+80
-119
lines changed

13 files changed

+80
-119
lines changed

src/librustc/front/map/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
151151
self.create_def_with_parent(
152152
Some(variant_def_index),
153153
field.node.id,
154-
DefPathData::Field(field.node.kind));
154+
DefPathData::Field(field.node.name));
155155
}
156156
}
157157
}
@@ -166,7 +166,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
166166
}
167167

168168
for field in struct_def.fields() {
169-
self.create_def(field.node.id, DefPathData::Field(field.node.kind));
169+
self.create_def(field.node.id, DefPathData::Field(field.node.name));
170170
}
171171
}
172172
ItemTrait(_, _, ref bounds, _) => {

src/librustc/front/map/definitions.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use middle::cstore::LOCAL_CRATE;
1212
use middle::def_id::{DefId, DefIndex};
1313
use rustc_data_structures::fnv::FnvHashMap;
14-
use rustc_front::hir;
1514
use syntax::ast;
1615
use syntax::parse::token::InternedString;
1716
use util::nodemap::NodeMap;
@@ -84,8 +83,7 @@ pub enum DefPathData {
8483
TypeParam(ast::Name),
8584
LifetimeDef(ast::Name),
8685
EnumVariant(ast::Name),
87-
PositionalField,
88-
Field(hir::StructFieldKind),
86+
Field(Option<ast::Name>),
8987
StructCtor, // implicit ctor for a tuple-like struct
9088
Initializer, // initializer for a const
9189
Binding(ast::Name), // pattern binding
@@ -186,16 +184,12 @@ impl DefPathData {
186184
LifetimeDef(name) |
187185
EnumVariant(name) |
188186
DetachedCrate(name) |
189-
Binding(name) => {
187+
Binding(name) |
188+
Field(Some(name)) => {
190189
name.as_str()
191190
}
192191

193-
Field(hir::StructFieldKind::NamedField(name, _)) => {
194-
name.as_str()
195-
}
196-
197-
PositionalField |
198-
Field(hir::StructFieldKind::UnnamedField(_)) => {
192+
Field(None) => {
199193
InternedString::new("{{field}}")
200194
}
201195

src/librustc/middle/dead.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
221221
let has_extern_repr = self.struct_has_extern_repr;
222222
let inherited_pub_visibility = self.inherited_pub_visibility;
223223
let live_fields = def.fields().iter().filter(|f| {
224-
has_extern_repr || inherited_pub_visibility || match f.node.kind {
225-
hir::NamedField(_, hir::Public) => true,
226-
_ => false
227-
}
224+
has_extern_repr || inherited_pub_visibility || f.node.vis == hir::Public
228225
});
229226
self.live_symbols.extend(live_fields.map(|f| f.node.id));
230227

@@ -432,7 +429,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
432429
}
433430

434431
fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
435-
let is_named = node.name().is_some();
432+
let is_named = node.name.is_some();
436433
let field_type = self.tcx.node_id_to_type(node.id);
437434
let is_marker_field = match field_type.ty_to_def_id() {
438435
Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
@@ -549,7 +546,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
549546
fn visit_struct_field(&mut self, field: &hir::StructField) {
550547
if self.should_warn_about_field(&field.node) {
551548
self.warn_dead_code(field.node.id, field.span,
552-
field.node.name().unwrap(), "struct field");
549+
field.node.name.unwrap(), "struct field");
553550
}
554551

555552
intravisit::walk_struct_field(self, field);

src/librustc_front/fold.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,12 @@ pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> Poly
700700
}
701701

702702
pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
703-
let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
703+
let StructField {node: StructField_ {id, name, vis, ty, attrs}, span} = f;
704704
Spanned {
705705
node: StructField_ {
706706
id: fld.new_id(id),
707-
kind: kind,
707+
name: name,
708+
vis: vis,
708709
ty: fld.fold_ty(ty),
709710
attrs: fold_attrs(attrs, fld),
710711
},

src/librustc_front/hir.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub use self::Mutability::*;
2424
pub use self::PathListItem_::*;
2525
pub use self::PrimTy::*;
2626
pub use self::Stmt_::*;
27-
pub use self::StructFieldKind::*;
2827
pub use self::TraitItem_::*;
2928
pub use self::Ty_::*;
3029
pub use self::TyParamBound::*;
@@ -1243,44 +1242,45 @@ impl Visibility {
12431242

12441243
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12451244
pub struct StructField_ {
1246-
pub kind: StructFieldKind,
1245+
pub name: Option<Name>,
1246+
pub vis: Visibility,
12471247
pub id: NodeId,
12481248
pub ty: P<Ty>,
12491249
pub attrs: HirVec<Attribute>,
12501250
}
12511251

1252-
impl StructField_ {
1253-
pub fn name(&self) -> Option<Name> {
1254-
match self.kind {
1255-
NamedField(name, _) => Some(name),
1256-
UnnamedField(_) => None,
1257-
}
1258-
}
1259-
}
1252+
// impl StructField_ {
1253+
// pub fn name(&self) -> Option<Name> {
1254+
// match self.kind {
1255+
// NamedField(name, _) => Some(name),
1256+
// UnnamedField(_) => None,
1257+
// }
1258+
// }
1259+
// }
12601260

12611261
pub type StructField = Spanned<StructField_>;
12621262

1263-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1264-
pub enum StructFieldKind {
1265-
NamedField(Name, Visibility),
1266-
/// Element of a tuple-like struct
1267-
UnnamedField(Visibility),
1268-
}
1269-
1270-
impl StructFieldKind {
1271-
pub fn is_unnamed(&self) -> bool {
1272-
match *self {
1273-
UnnamedField(..) => true,
1274-
NamedField(..) => false,
1275-
}
1276-
}
1277-
1278-
pub fn visibility(&self) -> Visibility {
1279-
match *self {
1280-
NamedField(_, vis) | UnnamedField(vis) => vis,
1281-
}
1282-
}
1283-
}
1263+
// #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1264+
// pub enum StructFieldKind {
1265+
// NamedField(Name, Visibility),
1266+
// /// Element of a tuple-like struct
1267+
// UnnamedField(Visibility),
1268+
// }
1269+
1270+
// impl StructFieldKind {
1271+
// pub fn is_unnamed(&self) -> bool {
1272+
// match *self {
1273+
// UnnamedField(..) => true,
1274+
// NamedField(..) => false,
1275+
// }
1276+
// }
1277+
1278+
// pub fn visibility(&self) -> Visibility {
1279+
// match *self {
1280+
// NamedField(_, vis) | UnnamedField(vis) => vis,
1281+
// }
1282+
// }
1283+
// }
12841284

12851285
/// Fields and Ids of enum variants and structs
12861286
///

src/librustc_front/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &
669669
}
670670

671671
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {
672-
walk_opt_name(visitor, struct_field.span, struct_field.node.name());
672+
walk_opt_name(visitor, struct_field.span, struct_field.node.name);
673673
visitor.visit_ty(&struct_field.node.ty);
674674
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
675675
}

src/librustc_front/lowering.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ pub fn lower_struct_field(lctx: &LoweringContext, f: &StructField) -> hir::Struc
611611
Spanned {
612612
node: hir::StructField_ {
613613
id: f.node.id,
614-
kind: lower_struct_field_kind(lctx, &f.node.kind),
614+
name: f.node.ident().map(|ident| ident.name),
615+
vis: lower_visibility(lctx, f.node.kind.visibility()),
615616
ty: lower_ty(lctx, &f.node.ty),
616617
attrs: lower_attrs(lctx, &f.node.attrs),
617618
},
@@ -1589,15 +1590,6 @@ pub fn lower_binding_mode(lctx: &LoweringContext, b: &BindingMode) -> hir::Bindi
15891590
}
15901591
}
15911592

1592-
pub fn lower_struct_field_kind(lctx: &LoweringContext,
1593-
s: &StructFieldKind)
1594-
-> hir::StructFieldKind {
1595-
match *s {
1596-
NamedField(ident, vis) => hir::NamedField(ident.name, lower_visibility(lctx, vis)),
1597-
UnnamedField(vis) => hir::UnnamedField(lower_visibility(lctx, vis)),
1598-
}
1599-
}
1600-
16011593
pub fn lower_unsafe_source(_lctx: &LoweringContext, u: UnsafeSource) -> hir::UnsafeSource {
16021594
match u {
16031595
CompilerGenerated => hir::CompilerGenerated,

src/librustc_front/print/pprust.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -915,14 +915,9 @@ impl<'a> State<'a> {
915915
if struct_def.is_tuple() {
916916
try!(self.popen());
917917
try!(self.commasep(Inconsistent, struct_def.fields(), |s, field| {
918-
match field.node.kind {
919-
hir::NamedField(..) => panic!("unexpected named field"),
920-
hir::UnnamedField(vis) => {
921-
try!(s.print_visibility(vis));
922-
try!(s.maybe_print_comment(field.span.lo));
923-
s.print_type(&field.node.ty)
924-
}
925-
}
918+
try!(s.print_visibility(field.node.vis));
919+
try!(s.maybe_print_comment(field.span.lo));
920+
s.print_type(&field.node.ty)
926921
}));
927922
try!(self.pclose());
928923
}
@@ -939,19 +934,14 @@ impl<'a> State<'a> {
939934
try!(self.hardbreak_if_not_bol());
940935

941936
for field in struct_def.fields() {
942-
match field.node.kind {
943-
hir::UnnamedField(..) => panic!("unexpected unnamed field"),
944-
hir::NamedField(name, visibility) => {
945-
try!(self.hardbreak_if_not_bol());
946-
try!(self.maybe_print_comment(field.span.lo));
947-
try!(self.print_outer_attributes(&field.node.attrs));
948-
try!(self.print_visibility(visibility));
949-
try!(self.print_name(name));
950-
try!(self.word_nbsp(":"));
951-
try!(self.print_type(&field.node.ty));
952-
try!(word(&mut self.s, ","));
953-
}
954-
}
937+
try!(self.hardbreak_if_not_bol());
938+
try!(self.maybe_print_comment(field.span.lo));
939+
try!(self.print_outer_attributes(&field.node.attrs));
940+
try!(self.print_visibility(field.node.vis));
941+
try!(self.print_name(field.node.name.unwrap()));
942+
try!(self.word_nbsp(":"));
943+
try!(self.print_type(&field.node.ty));
944+
try!(word(&mut self.s, ","));
955945
}
956946

957947
self.bclose(span)

src/librustc_lint/bad_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl LateLintPass for NonSnakeCase {
283283
fn check_struct_def(&mut self, cx: &LateContext, s: &hir::VariantData,
284284
_: ast::Name, _: &hir::Generics, _: ast::NodeId) {
285285
for sf in s.fields() {
286-
if let hir::StructField_ { kind: hir::NamedField(name, _), .. } = sf.node {
286+
if let Some(name) = sf.node.name {
287287
self.check_snake_case(cx, "structure field", &name.as_str(),
288288
Some(sf.span));
289289
}

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ impl LateLintPass for MissingDoc {
428428
}
429429

430430
fn check_struct_field(&mut self, cx: &LateContext, sf: &hir::StructField) {
431-
if let hir::NamedField(_, vis) = sf.node.kind {
432-
if vis == hir::Public || self.in_variant {
431+
if sf.node.name.is_some() {
432+
if sf.node.vis == hir::Public || self.in_variant {
433433
let cur_struct_def = *self.struct_def_stack.last()
434434
.expect("empty struct_def_stack");
435435
self.check_missing_docs_attrs(cx, Some(cur_struct_def),

src/librustc_privacy/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
288288
self.update(def.id(), item_level);
289289
}
290290
for field in def.fields() {
291-
if field.node.kind.visibility() == hir::Public {
291+
if field.node.vis == hir::Public {
292292
self.update(field.node.id, item_level);
293293
}
294294
}
@@ -1178,7 +1178,7 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
11781178
hir::ItemEnum(ref def, _) => {
11791179
for variant in &def.variants {
11801180
for field in variant.node.data.fields() {
1181-
check_inherited(field.span, field.node.kind.visibility(),
1181+
check_inherited(field.span, field.vis,
11821182
"visibility qualifiers have no effect on variant fields");
11831183
}
11841184
}
@@ -1514,10 +1514,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx>
15141514
}
15151515

15161516
fn visit_struct_field(&mut self, s: &hir::StructField) {
1517-
let vis = match s.node.kind {
1518-
hir::NamedField(_, vis) | hir::UnnamedField(vis) => vis
1519-
};
1520-
if vis == hir::Public || self.in_variant {
1517+
if s.node.vis == hir::Public || self.in_variant {
15211518
intravisit::walk_struct_field(self, s);
15221519
}
15231520
}
@@ -1728,7 +1725,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivateItemsInPublicInterfacesVisitor<'a, 'tc
17281725
if item.vis == hir::Public {
17291726
check.visit_generics(generics);
17301727
for field in struct_def.fields() {
1731-
if field.node.kind.visibility() == hir::Public {
1728+
if field.node.vis == hir::Public {
17321729
check.visit_struct_field(field);
17331730
}
17341731
}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ use rustc_front::hir::{ForeignItem, ForeignItemFn, ForeignItemStatic};
4141
use rustc_front::hir::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
4242
use rustc_front::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
4343
use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
44-
use rustc_front::hir::{NamedField, PathListIdent, PathListMod};
45-
use rustc_front::hir::StmtDecl;
46-
use rustc_front::hir::UnnamedField;
44+
use rustc_front::hir::{PathListIdent, PathListMod, StmtDecl};
4745
use rustc_front::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
4846
use rustc_front::hir::Visibility;
4947
use rustc_front::intravisit::{self, Visitor};
@@ -384,12 +382,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
384382
// Record the def ID and fields of this struct.
385383
let named_fields = struct_def.fields()
386384
.iter()
387-
.filter_map(|f| {
388-
match f.node.kind {
389-
NamedField(name, _) => Some(name),
390-
UnnamedField(_) => None,
391-
}
392-
})
385+
.filter_map(|f| f.node.name)
393386
.collect();
394387
let item_def_id = self.ast_map.local_def_id(item.id);
395388
self.structs.insert(item_def_id, named_fields);

src/librustc_typeck/collect.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -978,24 +978,21 @@ fn convert_struct_variant<'tcx>(tcx: &ty::ctxt<'tcx>,
978978
let mut seen_fields: FnvHashMap<ast::Name, Span> = FnvHashMap();
979979
let fields = def.fields().iter().map(|f| {
980980
let fid = tcx.map.local_def_id(f.node.id);
981-
match f.node.kind {
982-
hir::NamedField(name, vis) => {
983-
let dup_span = seen_fields.get(&name).cloned();
984-
if let Some(prev_span) = dup_span {
985-
let mut err = struct_span_err!(tcx.sess, f.span, E0124,
986-
"field `{}` is already declared",
987-
name);
988-
span_note!(&mut err, prev_span, "previously declared here");
989-
err.emit();
990-
} else {
991-
seen_fields.insert(name, f.span);
992-
}
993-
994-
ty::FieldDefData::new(fid, name, vis)
995-
},
996-
hir::UnnamedField(vis) => {
997-
ty::FieldDefData::new(fid, special_idents::unnamed_field.name, vis)
981+
if let Some(name) = f.node.name {
982+
let dup_span = seen_fields.get(&name).cloned();
983+
if let Some(prev_span) = dup_span {
984+
let mut err = struct_span_err!(tcx.sess, f.span, E0124,
985+
"field `{}` is already declared",
986+
name);
987+
span_note!(&mut err, prev_span, "previously declared here");
988+
err.emit();
989+
} else {
990+
seen_fields.insert(name, f.span);
998991
}
992+
993+
ty::FieldDefData::new(fid, name, f.node.vis)
994+
} else {
995+
ty::FieldDefData::new(fid, special_idents::unnamed_field.name, f.node.vis)
999996
}
1000997
}).collect();
1001998
ty::VariantDefData {

0 commit comments

Comments
 (0)