Skip to content

Commit 25e42ac

Browse files
committed
Auto merge of #31937 - petrochenkov:field, r=eddyb
The names are "0", "1", "2" etc, the same as used in field access. This generally make things simpler and potentially allows to reuse braced struct machinery (struct patterns, struct expressions) for tuple structs. I haven't touched the AST for stability reasons, but I intend to do it later. r? @eddyb
2 parents 0400d92 + bc93fbc commit 25e42ac

File tree

26 files changed

+144
-233
lines changed

26 files changed

+144
-233
lines changed

src/librustc/front/map/collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
150150
for field in v.node.data.fields() {
151151
self.create_def_with_parent(
152152
Some(variant_def_index),
153-
field.node.id,
154-
DefPathData::Field(field.node.kind));
153+
field.id,
154+
DefPathData::Field(field.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.id, DefPathData::Field(field.name));
170170
}
171171
}
172172
ItemTrait(_, _, ref bounds, _) => {

src/librustc/front/map/definitions.rs

+3-13
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(ast::Name),
8987
StructCtor, // implicit ctor for a tuple-like struct
9088
Initializer, // initializer for a const
9189
Binding(ast::Name), // pattern binding
@@ -186,19 +184,11 @@ impl DefPathData {
186184
LifetimeDef(name) |
187185
EnumVariant(name) |
188186
DetachedCrate(name) |
189-
Binding(name) => {
187+
Binding(name) |
188+
Field(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(_)) => {
199-
InternedString::new("{{field}}")
200-
}
201-
202192
// note that this does not show up in user printouts
203193
CrateRoot => {
204194
InternedString::new("{{root}}")

src/librustc/lint/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
811811
}
812812

813813
fn visit_struct_field(&mut self, s: &hir::StructField) {
814-
self.with_lint_attrs(&s.node.attrs, |cx| {
814+
self.with_lint_attrs(&s.attrs, |cx| {
815815
run_lints!(cx, check_struct_field, late_passes, s);
816816
hir_visit::walk_struct_field(cx, s);
817817
})

src/librustc/middle/dead.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,9 @@ 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.vis == hir::Public
228225
});
229-
self.live_symbols.extend(live_fields.map(|f| f.node.id));
226+
self.live_symbols.extend(live_fields.map(|f| f.id));
230227

231228
intravisit::walk_struct_def(self, def);
232229
}
@@ -431,17 +428,16 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
431428
should_warn && !self.symbol_is_live(item.id, ctor_id)
432429
}
433430

434-
fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
435-
let is_named = node.name().is_some();
436-
let field_type = self.tcx.node_id_to_type(node.id);
431+
fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
432+
let field_type = self.tcx.node_id_to_type(field.id);
437433
let is_marker_field = match field_type.ty_to_def_id() {
438434
Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
439435
_ => false
440436
};
441-
is_named
442-
&& !self.symbol_is_live(node.id, None)
437+
!field.is_positional()
438+
&& !self.symbol_is_live(field.id, None)
443439
&& !is_marker_field
444-
&& !has_allow_dead_code_or_lang_attr(&node.attrs)
440+
&& !has_allow_dead_code_or_lang_attr(&field.attrs)
445441
}
446442

447443
fn should_warn_about_variant(&mut self, variant: &hir::Variant_) -> bool {
@@ -547,9 +543,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
547543
}
548544

549545
fn visit_struct_field(&mut self, field: &hir::StructField) {
550-
if self.should_warn_about_field(&field.node) {
551-
self.warn_dead_code(field.node.id, field.span,
552-
field.node.name().unwrap(), "struct field");
546+
if self.should_warn_about_field(&field) {
547+
self.warn_dead_code(field.id, field.span,
548+
field.name, "struct field");
553549
}
554550

555551
intravisit::walk_struct_field(self, field);

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Annotator<'a, 'tcx> {
259259
}
260260

261261
fn visit_struct_field(&mut self, s: &StructField) {
262-
self.annotate(s.node.id, &s.node.attrs, s.span, AnnotationKind::Required, |v| {
262+
self.annotate(s.id, &s.attrs, s.span, AnnotationKind::Required, |v| {
263263
intravisit::walk_struct_field(v, s);
264264
});
265265
}

src/librustc/middle/ty/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,6 @@ pub struct FieldDefData<'tcx, 'container: 'tcx> {
13711371
/// The field's DefId. NOTE: the fields of tuple-like enum variants
13721372
/// are not real items, and don't have entries in tcache etc.
13731373
pub did: DefId,
1374-
/// special_idents::unnamed_field.name
1375-
/// if this is a tuple-like field
13761374
pub name: Name,
13771375
pub vis: hir::Visibility,
13781376
/// TyIVar is used here to allow for variance (see the doc at

src/librustc_front/fold.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -700,15 +700,13 @@ 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;
704-
Spanned {
705-
node: StructField_ {
706-
id: fld.new_id(id),
707-
kind: kind,
708-
ty: fld.fold_ty(ty),
709-
attrs: fold_attrs(attrs, fld),
710-
},
711-
span: fld.new_span(span),
703+
StructField {
704+
span: fld.new_span(f.span),
705+
id: fld.new_id(f.id),
706+
name: f.name,
707+
vis: f.vis,
708+
ty: fld.fold_ty(f.ty),
709+
attrs: fold_attrs(f.attrs, fld),
712710
}
713711
}
714712

src/librustc_front/hir.rs

+9-33
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::*;
@@ -1242,43 +1241,20 @@ impl Visibility {
12421241
}
12431242

12441243
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1245-
pub struct StructField_ {
1246-
pub kind: StructFieldKind,
1244+
pub struct StructField {
1245+
pub span: Span,
1246+
pub name: Name,
1247+
pub vis: Visibility,
12471248
pub id: NodeId,
12481249
pub ty: P<Ty>,
12491250
pub attrs: HirVec<Attribute>,
12501251
}
12511252

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-
}
1260-
1261-
pub type StructField = Spanned<StructField_>;
1262-
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-
}
1253+
impl StructField {
1254+
// Still necessary in couple of places
1255+
pub fn is_positional(&self) -> bool {
1256+
let first = self.name.as_str().as_bytes()[0];
1257+
first >= b'0' && first <= b'9'
12821258
}
12831259
}
12841260

src/librustc_front/intravisit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ 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());
673-
visitor.visit_ty(&struct_field.node.ty);
674-
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
672+
visitor.visit_name(struct_field.span, struct_field.name);
673+
visitor.visit_ty(&struct_field.ty);
674+
walk_list!(visitor, visit_attribute, &struct_field.attrs);
675675
}
676676

677677
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {

src/librustc_front/lowering.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,14 @@ pub fn lower_variant_data(lctx: &LoweringContext, vdata: &VariantData) -> hir::V
578578
match *vdata {
579579
VariantData::Struct(ref fields, id) => {
580580
hir::VariantData::Struct(fields.iter()
581+
.enumerate()
581582
.map(|f| lower_struct_field(lctx, f))
582583
.collect(),
583584
id)
584585
}
585586
VariantData::Tuple(ref fields, id) => {
586587
hir::VariantData::Tuple(fields.iter()
588+
.enumerate()
587589
.map(|f| lower_struct_field(lctx, f))
588590
.collect(),
589591
id)
@@ -607,15 +609,17 @@ pub fn lower_poly_trait_ref(lctx: &LoweringContext, p: &PolyTraitRef) -> hir::Po
607609
}
608610
}
609611

610-
pub fn lower_struct_field(lctx: &LoweringContext, f: &StructField) -> hir::StructField {
611-
Spanned {
612-
node: hir::StructField_ {
613-
id: f.node.id,
614-
kind: lower_struct_field_kind(lctx, &f.node.kind),
615-
ty: lower_ty(lctx, &f.node.ty),
616-
attrs: lower_attrs(lctx, &f.node.attrs),
617-
},
612+
pub fn lower_struct_field(lctx: &LoweringContext,
613+
(index, f): (usize, &StructField))
614+
-> hir::StructField {
615+
hir::StructField {
618616
span: f.span,
617+
id: f.node.id,
618+
name: f.node.ident().map(|ident| ident.name)
619+
.unwrap_or(token::intern(&index.to_string())),
620+
vis: lower_visibility(lctx, f.node.kind.visibility()),
621+
ty: lower_ty(lctx, &f.node.ty),
622+
attrs: lower_attrs(lctx, &f.node.attrs),
619623
}
620624
}
621625

@@ -1589,15 +1593,6 @@ pub fn lower_binding_mode(lctx: &LoweringContext, b: &BindingMode) -> hir::Bindi
15891593
}
15901594
}
15911595

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-
16011596
pub fn lower_unsafe_source(_lctx: &LoweringContext, u: UnsafeSource) -> hir::UnsafeSource {
16021597
match u {
16031598
CompilerGenerated => hir::CompilerGenerated,

src/librustc_front/print/pprust.rs

+11-21
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.vis));
919+
try!(s.maybe_print_comment(field.span.lo));
920+
s.print_type(&field.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.attrs));
940+
try!(self.print_visibility(field.vis));
941+
try!(self.print_name(field.name));
942+
try!(self.word_nbsp(":"));
943+
try!(self.print_type(&field.ty));
944+
try!(word(&mut self.s, ","));
955945
}
956946

957947
self.bclose(span)

src/librustc_front/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'a, 'v, O: ast_util::IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O>
271271
}
272272

273273
fn visit_struct_field(&mut self, struct_field: &StructField) {
274-
self.operation.visit_id(struct_field.node.id);
274+
self.operation.visit_id(struct_field.id);
275275
intravisit::walk_struct_field(self, struct_field)
276276
}
277277

src/librustc_lint/bad_style.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +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 {
287-
self.check_snake_case(cx, "structure field", &name.as_str(),
288-
Some(sf.span));
289-
}
286+
self.check_snake_case(cx, "structure field", &sf.name.as_str(), Some(sf.span));
290287
}
291288
}
292289
}

src/librustc_lint/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl LateLintPass for BoxPointers {
126126
hir::ItemStruct(ref struct_def, _) => {
127127
for struct_field in struct_def.fields() {
128128
self.check_heap_type(cx, struct_field.span,
129-
cx.tcx.node_id_to_type(struct_field.node.id));
129+
cx.tcx.node_id_to_type(struct_field.id));
130130
}
131131
}
132132
_ => ()
@@ -428,12 +428,12 @@ 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.is_positional() {
432+
if sf.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),
436-
&sf.node.attrs, sf.span,
436+
&sf.attrs, sf.span,
437437
"a struct field")
438438
}
439439
}

0 commit comments

Comments
 (0)