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) => {

0 commit comments

Comments
 (0)