Skip to content

Commit 40aa09e

Browse files
committed
Merge struct fields and struct kind
1 parent 30af54d commit 40aa09e

File tree

36 files changed

+222
-148
lines changed

36 files changed

+222
-148
lines changed

src/librustc/front/map/collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
138138
NodeVariant(&**v),
139139
DefPathData::EnumVariant(v.node.name));
140140

141-
for field in &v.node.data.fields {
141+
for field in v.node.data.fields() {
142142
self.create_def_with_parent(
143143
Some(variant_def_index),
144144
field.node.id,
@@ -150,13 +150,13 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
150150
}
151151
ItemStruct(ref struct_def, _) => {
152152
// If this is a tuple-like struct, register the constructor.
153-
if struct_def.kind != VariantKind::Struct {
153+
if !struct_def.is_struct() {
154154
self.insert_def(struct_def.id,
155155
NodeStructCtor(&**struct_def),
156156
DefPathData::StructCtor);
157157
}
158158

159-
for field in &struct_def.fields {
159+
for field in struct_def.fields() {
160160
self.create_def(field.node.id, DefPathData::Field(field.node.kind));
161161
}
162162
}

src/librustc/front/map/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,10 @@ impl<'ast> Map<'ast> {
480480
}
481481
}
482482
Some(NodeVariant(variant)) => {
483-
match variant.node.data.kind {
484-
VariantKind::Struct => &variant.node.data,
485-
_ => panic!("struct ID bound to enum variant that isn't struct-like"),
483+
if variant.node.data.is_struct() {
484+
&variant.node.data
485+
} else {
486+
panic!("struct ID bound to enum variant that isn't struct-like")
486487
}
487488
}
488489
_ => panic!(format!("expected struct, found {}", self.node_to_string(id))),

src/librustc/metadata/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn each_auxiliary_node_id<F>(item: &hir::Item, callback: F) -> bool where
381381
match item.node {
382382
hir::ItemStruct(ref struct_def, _) => {
383383
// If this is a newtype struct, return the constructor.
384-
if struct_def.kind == hir::VariantKind::Tuple {
384+
if struct_def.is_tuple() {
385385
continue_ = callback(struct_def.id);
386386
}
387387
}
@@ -1068,7 +1068,7 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
10681068
// Encode inherent implementations for this structure.
10691069
encode_inherent_implementations(ecx, rbml_w, def_id);
10701070

1071-
if struct_def.kind != hir::VariantKind::Struct {
1071+
if !struct_def.is_struct() {
10721072
let ctor_did = ecx.tcx.map.local_def_id(struct_def.id);
10731073
rbml_w.wr_tagged_u64(tag_items_data_item_struct_ctor,
10741074
def_to_u64(ctor_did));
@@ -1081,7 +1081,7 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
10811081
}
10821082

10831083
// If this is a tuple-like struct, encode the type of the constructor.
1084-
if struct_def.kind != hir::VariantKind::Struct {
1084+
if !struct_def.is_struct() {
10851085
encode_info_for_struct_ctor(ecx, rbml_w, item.name, struct_def.id, index, item.id);
10861086
}
10871087
}

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) {
13201320
}
13211321
}
13221322
hir::ItemStruct(ref def, _) => {
1323-
if def.kind != hir::VariantKind::Struct {
1323+
if !def.is_struct() {
13241324
let ctor_did = dcx.tcx.lookup_adt_def(orig_did)
13251325
.struct_variant().did;
13261326
debug!("astencode: copying ctor {:?} => {:?}", ctor_did,

src/librustc/middle/dead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
219219
_: &hir::Generics, _: ast::NodeId, _: codemap::Span) {
220220
let has_extern_repr = self.struct_has_extern_repr;
221221
let inherited_pub_visibility = self.inherited_pub_visibility;
222-
let live_fields = def.fields.iter().filter(|f| {
222+
let live_fields = def.fields().filter(|f| {
223223
has_extern_repr || inherited_pub_visibility || match f.node.kind {
224224
hir::NamedField(_, hir::Public) => true,
225225
_ => false
@@ -426,7 +426,7 @@ fn find_live(tcx: &ty::ctxt,
426426

427427
fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
428428
match item.node {
429-
hir::ItemStruct(ref struct_def, _) if struct_def.kind != hir::VariantKind::Struct => {
429+
hir::ItemStruct(ref struct_def, _) if !struct_def.is_struct() => {
430430
Some(struct_def.id)
431431
}
432432
_ => None

src/librustc/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Annotator<'a, 'tcx> {
185185
|v| visit::walk_item(v, i), required);
186186

187187
if let hir::ItemStruct(ref sd, _) = i.node {
188-
if sd.kind != hir::VariantKind::Struct {
188+
if !sd.is_struct() {
189189
self.annotate(sd.id, true, &i.attrs, i.span, |_| {}, true)
190190
}
191191
}

src/librustc_front/fold.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,18 @@ pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -
694694
}
695695

696696
pub fn noop_fold_struct_def<T: Folder>(struct_def: P<VariantData>, fld: &mut T) -> P<VariantData> {
697-
struct_def.map(|VariantData { fields, id, kind }| {
697+
struct_def.map(|VariantData { data_, id }| {
698698
VariantData {
699-
fields: fields.move_map(|f| fld.fold_struct_field(f)),
699+
data_: match data_ {
700+
VariantData_::Struct(fields) => {
701+
VariantData_::Struct(fields.move_map(|f| fld.fold_struct_field(f)))
702+
}
703+
VariantData_::Tuple(fields) => {
704+
VariantData_::Tuple(fields.move_map(|f| fld.fold_struct_field(f)))
705+
}
706+
VariantData_::Unit => VariantData_::Unit
707+
},
700708
id: fld.new_id(id),
701-
kind: kind,
702709
}
703710
})
704711
}

src/librustc_front/hir.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use print::pprust;
4949
use util;
5050

5151
use std::fmt;
52+
use std::{iter, option, slice};
5253
use serialize::{Encodable, Encoder, Decoder};
5354

5455
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
@@ -1160,21 +1161,42 @@ impl StructFieldKind {
11601161
}
11611162
}
11621163

1163-
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1164-
pub enum VariantKind {
1165-
Struct,
1166-
Tuple,
1164+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1165+
pub enum VariantData_ {
1166+
Struct(Vec<StructField>),
1167+
Tuple(Vec<StructField>),
11671168
Unit,
11681169
}
11691170

11701171
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
11711172
pub struct VariantData {
1172-
/// Fields, not including ctor
1173-
pub fields: Vec<StructField>,
1173+
pub data_: VariantData_,
11741174
/// ID of the constructor. This is only used for tuple- or enum-like
11751175
/// structs.
11761176
pub id: NodeId,
1177-
pub kind: VariantKind,
1177+
}
1178+
1179+
pub type FieldIter<'a> = iter::FlatMap<option::IntoIter<&'a Vec<StructField>>,
1180+
slice::Iter<'a, StructField>,
1181+
fn(&Vec<StructField>) -> slice::Iter<StructField>>;
1182+
1183+
impl VariantData {
1184+
pub fn fields(&self) -> FieldIter {
1185+
fn vec_iter<T>(v: &Vec<T>) -> slice::Iter<T> { v.iter() }
1186+
match self.data_ {
1187+
VariantData_::Struct(ref fields) | VariantData_::Tuple(ref fields) => Some(fields),
1188+
_ => None,
1189+
}.into_iter().flat_map(vec_iter)
1190+
}
1191+
pub fn is_struct(&self) -> bool {
1192+
if let VariantData_::Struct(..) = self.data_ { true } else { false }
1193+
}
1194+
pub fn is_tuple(&self) -> bool {
1195+
if let VariantData_::Tuple(..) = self.data_ { true } else { false }
1196+
}
1197+
pub fn is_unit(&self) -> bool {
1198+
if let VariantData_::Unit = self.data_ { true } else { false }
1199+
}
11781200
}
11791201

11801202
/*

src/librustc_front/lowering.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,15 @@ pub fn lower_where_predicate(_lctx: &LoweringContext,
500500

501501
pub fn lower_struct_def(sd: &VariantData) -> P<hir::VariantData> {
502502
P(hir::VariantData {
503-
fields: sd.fields.iter().map(|f| lower_struct_field(_lctx, f)).collect(),
504503
id: sd.id,
505-
kind: match sd.kind {
506-
VariantKind::Struct => hir::VariantKind::Struct,
507-
VariantKind::Tuple => hir::VariantKind::Tuple,
508-
VariantKind::Unit => hir::VariantKind::Unit,
504+
data_: match sd.data_ {
505+
VariantData_::Struct(ref fields) => {
506+
hir::VariantData_::Struct(fields.iter().map(|f| lower_struct_field(_lctx, f)).collect())
507+
}
508+
VariantData_::Tuple(ref fields) => {
509+
hir::VariantData_::Tuple(fields.iter().map(|f| lower_struct_field(_lctx, f)).collect())
510+
}
511+
VariantData_::Unit => hir::VariantData_::Unit
509512
}
510513
})
511514
}

src/librustc_front/print/pprust.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,11 @@ impl<'a> State<'a> {
896896
-> io::Result<()> {
897897
try!(self.print_name(name));
898898
try!(self.print_generics(generics));
899-
if struct_def.kind != hir::VariantKind::Struct {
900-
if struct_def.kind == hir::VariantKind::Tuple {
899+
if !struct_def.is_struct() {
900+
if struct_def.is_tuple() {
901901
try!(self.popen());
902-
try!(self.commasep(Inconsistent,
903-
&struct_def.fields,
902+
try!(self.commasep_iter(Inconsistent,
903+
struct_def.fields(),
904904
|s, field| {
905905
match field.node.kind {
906906
hir::NamedField(..) => panic!("unexpected named field"),
@@ -925,7 +925,7 @@ impl<'a> State<'a> {
925925
try!(self.bopen());
926926
try!(self.hardbreak_if_not_bol());
927927

928-
for field in &struct_def.fields {
928+
for field in struct_def.fields() {
929929
match field.node.kind {
930930
hir::UnnamedField(..) => panic!("unexpected unnamed field"),
931931
hir::NamedField(name, visibility) => {

src/librustc_front/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
629629
}
630630

631631
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
632-
walk_list!(visitor, visit_struct_field, &struct_definition.fields);
632+
walk_list!(visitor, visit_struct_field, struct_definition.fields());
633633
}
634634

635635
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) {

src/librustc_lint/bad_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl LateLintPass for NonSnakeCase {
282282

283283
fn check_struct_def(&mut self, cx: &LateContext, s: &hir::VariantData,
284284
_: ast::Name, _: &hir::Generics, _: ast::NodeId) {
285-
for sf in &s.fields {
285+
for sf in s.fields() {
286286
if let hir::StructField_ { kind: hir::NamedField(name, _), .. } = sf.node {
287287
self.check_snake_case(cx, "structure field", &name.as_str(),
288288
Some(sf.span));

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl LateLintPass for BoxPointers {
123123
// If it's a struct, we also have to check the fields' types
124124
match it.node {
125125
hir::ItemStruct(ref struct_def, _) => {
126-
for struct_field in &struct_def.fields {
126+
for struct_field in struct_def.fields() {
127127
self.check_heap_type(cx, struct_field.span,
128128
cx.tcx.node_id_to_type(struct_field.node.id));
129129
}

src/librustc_privacy/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ impl<'v> Visitor<'v> for ParentVisitor {
132132
_: &'v hir::Generics, item_id: ast::NodeId, _: Span) {
133133
// Struct constructors are parented to their struct definitions because
134134
// they essentially are the struct definitions.
135-
if s.kind != hir::VariantKind::Struct {
135+
if !s.is_struct() {
136136
self.parents.insert(s.id, item_id);
137137
}
138138

139139
// While we have the id of the struct definition, go ahead and parent
140140
// all the fields.
141-
for field in &s.fields {
141+
for field in s.fields() {
142142
self.parents.insert(field.node.id, self.curparent);
143143
}
144144
visit::walk_struct_def(self, s)
@@ -319,11 +319,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EmbargoVisitor<'a, 'tcx> {
319319

320320
// Struct constructors are public if the struct is all public.
321321
hir::ItemStruct(ref def, _) if public_first => {
322-
if def.kind != hir::VariantKind::Struct {
322+
if !def.is_struct() {
323323
self.exported_items.insert(def.id);
324324
}
325325
// fields can be public or private, so lets check
326-
for field in &def.fields {
326+
for field in def.fields() {
327327
let vis = match field.node.kind {
328328
hir::NamedField(_, vis) | hir::UnnamedField(vis) => vis
329329
};
@@ -1089,7 +1089,7 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
10891089
}
10901090
}
10911091
let check_struct = |def: &hir::VariantData| {
1092-
for f in &def.fields {
1092+
for f in def.fields() {
10931093
match f.node.kind {
10941094
hir::NamedField(_, p) => check_inherited(tcx, f.span, p),
10951095
hir::UnnamedField(..) => {}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
492492
// These items live in both the type and value namespaces.
493493
ItemStruct(ref struct_def, _) => {
494494
// Adding to both Type and Value namespaces or just Type?
495-
let (forbid, ctor_id) = match struct_def.kind {
496-
hir::VariantKind::Struct => (ForbidDuplicateTypesAndModules, None),
497-
_ => (ForbidDuplicateTypesAndValues, Some(struct_def.id)),
495+
let (forbid, ctor_id) = if struct_def.is_struct() {
496+
(ForbidDuplicateTypesAndModules, None)
497+
} else {
498+
(ForbidDuplicateTypesAndValues, Some(struct_def.id))
498499
};
499500

500501
let name_bindings = self.add_child(name, parent, forbid, sp);
@@ -513,7 +514,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
513514
}
514515

515516
// Record the def ID and fields of this struct.
516-
let named_fields = struct_def.fields.iter().filter_map(|f| {
517+
let named_fields = struct_def.fields().filter_map(|f| {
517518
match f.node.kind {
518519
NamedField(name, _) => Some(name),
519520
UnnamedField(_) => None
@@ -587,14 +588,13 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
587588
item_id: DefId,
588589
parent: &Rc<Module>) {
589590
let name = variant.node.name;
590-
let is_exported = match variant.node.data.kind {
591-
hir::VariantKind::Struct => {
592-
// Not adding fields for variants as they are not accessed with a self receiver
593-
let variant_def_id = self.ast_map.local_def_id(variant.node.data.id);
594-
self.structs.insert(variant_def_id, Vec::new());
595-
true
596-
}
597-
_ => false,
591+
let is_exported = if variant.node.data.is_struct() {
592+
// Not adding fields for variants as they are not accessed with a self receiver
593+
let variant_def_id = self.ast_map.local_def_id(variant.node.data.id);
594+
self.structs.insert(variant_def_id, Vec::new());
595+
true
596+
} else {
597+
false
598598
};
599599

600600
let child = self.add_child(name, parent,

src/librustc_trans/save/dump_csv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
473473
&val);
474474

475475
// fields
476-
for field in &def.fields {
476+
for field in def.fields() {
477477
self.process_struct_field_def(field, item.id);
478478
self.visit_ty(&field.node.ty);
479479
}
@@ -510,7 +510,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
510510
&val,
511511
enum_data.id);
512512

513-
for field in &variant.node.data.fields {
513+
for field in variant.node.data.fields() {
514514
self.process_struct_field_def(field, variant.node.data.id);
515515
self.visit_ty(&*field.node.ty);
516516
}

src/librustc_trans/trans/base.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,12 +2428,12 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
24282428

24292429
hir_map::NodeVariant(ref v) => {
24302430
let llfn;
2431-
let fields = if v.node.data.kind == hir::VariantKind::Struct {
2431+
let fields = if v.node.data.is_struct() {
24322432
ccx.sess().bug("struct variant kind unexpected in get_item_val")
24332433
} else {
2434-
&v.node.data.fields
2434+
v.node.data.fields()
24352435
};
2436-
assert!(!fields.is_empty());
2436+
assert!(fields.count() != 0);
24372437
let ty = ccx.tcx().node_id_to_type(id);
24382438
let parent = ccx.tcx().map.get_parent(id);
24392439
let enm = ccx.tcx().map.expect_item(parent);
@@ -2454,12 +2454,11 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
24542454

24552455
hir_map::NodeStructCtor(struct_def) => {
24562456
// Only register the constructor if this is a tuple-like struct.
2457-
let ctor_id = match struct_def.kind {
2458-
hir::VariantKind::Struct => {
2459-
ccx.sess().bug("attempt to register a constructor of \
2460-
a non-tuple-like struct")
2461-
}
2462-
_ => struct_def.id,
2457+
let ctor_id = if struct_def.is_struct() {
2458+
ccx.sess().bug("attempt to register a constructor of \
2459+
a non-tuple-like struct")
2460+
} else {
2461+
struct_def.id
24632462
};
24642463
let parent = ccx.tcx().map.get_parent(id);
24652464
let struct_item = ccx.tcx().map.expect_item(parent);

src/librustc_trans/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub fn trans_fn_ref_with_substs<'a, 'tcx>(
418418

419419
match map_node {
420420
hir_map::NodeVariant(v) => {
421-
v.node.data.kind == hir::VariantKind::Tuple
421+
v.node.data.is_tuple()
422422
}
423423
hir_map::NodeStructCtor(_) => true,
424424
_ => false

0 commit comments

Comments
 (0)