Skip to content

Commit 8b026a6

Browse files
committed
Use numeric field Names ("0", "1" etc) for positional fields
1 parent 8b60b94 commit 8b026a6

File tree

18 files changed

+62
-109
lines changed

18 files changed

+62
-109
lines changed

src/librustc/front/map/definitions.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub enum DefPathData {
8383
TypeParam(ast::Name),
8484
LifetimeDef(ast::Name),
8585
EnumVariant(ast::Name),
86-
Field(Option<ast::Name>),
86+
Field(ast::Name),
8787
StructCtor, // implicit ctor for a tuple-like struct
8888
Initializer, // initializer for a const
8989
Binding(ast::Name), // pattern binding
@@ -185,14 +185,10 @@ impl DefPathData {
185185
EnumVariant(name) |
186186
DetachedCrate(name) |
187187
Binding(name) |
188-
Field(Some(name)) => {
188+
Field(name) => {
189189
name.as_str()
190190
}
191191

192-
Field(None) => {
193-
InternedString::new("{{field}}")
194-
}
195-
196192
// note that this does not show up in user printouts
197193
CrateRoot => {
198194
InternedString::new("{{root}}")

src/librustc/middle/dead.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,12 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
429429
}
430430

431431
fn should_warn_about_field(&mut self, node: &hir::StructField_) -> bool {
432-
let is_named = node.name.is_some();
433432
let field_type = self.tcx.node_id_to_type(node.id);
434433
let is_marker_field = match field_type.ty_to_def_id() {
435434
Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
436435
_ => false
437436
};
438-
is_named
437+
!node.is_positional()
439438
&& !self.symbol_is_live(node.id, None)
440439
&& !is_marker_field
441440
&& !has_allow_dead_code_or_lang_attr(&node.attrs)
@@ -546,7 +545,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
546545
fn visit_struct_field(&mut self, field: &hir::StructField) {
547546
if self.should_warn_about_field(&field.node) {
548547
self.warn_dead_code(field.node.id, field.span,
549-
field.node.name.unwrap(), "struct field");
548+
field.node.name, "struct field");
550549
}
551550

552551
intravisit::walk_struct_field(self, field);

src/librustc/middle/ty/mod.rs

Lines changed: 0 additions & 2 deletions
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/hir.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,45 +1242,22 @@ impl Visibility {
12421242

12431243
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12441244
pub struct StructField_ {
1245-
pub name: Option<Name>,
1245+
pub name: Name,
12461246
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-
// }
1260-
12611252
pub type StructField = Spanned<StructField_>;
12621253

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-
// }
1254+
impl StructField_ {
1255+
// Still necessary in couple of places
1256+
pub fn is_positional(&self) -> bool {
1257+
let first = self.name.as_str().as_bytes()[0];
1258+
first >= b'0' && first <= b'9'
1259+
}
1260+
}
12841261

12851262
/// Fields and Ids of enum variants and structs
12861263
///

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+
visitor.visit_name(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: 7 additions & 2 deletions
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,11 +609,14 @@ 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 {
612+
pub fn lower_struct_field(lctx: &LoweringContext,
613+
(index, f): (usize, &StructField))
614+
-> hir::StructField {
611615
Spanned {
612616
node: hir::StructField_ {
613617
id: f.node.id,
614-
name: f.node.ident().map(|ident| ident.name),
618+
name: f.node.ident().map(|ident| ident.name)
619+
.unwrap_or(token::intern(&index.to_string())),
615620
vis: lower_visibility(lctx, f.node.kind.visibility()),
616621
ty: lower_ty(lctx, &f.node.ty),
617622
attrs: lower_attrs(lctx, &f.node.attrs),

src/librustc_front/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ impl<'a> State<'a> {
938938
try!(self.maybe_print_comment(field.span.lo));
939939
try!(self.print_outer_attributes(&field.node.attrs));
940940
try!(self.print_visibility(field.node.vis));
941-
try!(self.print_name(field.node.name.unwrap()));
941+
try!(self.print_name(field.node.name));
942942
try!(self.word_nbsp(":"));
943943
try!(self.print_type(&field.node.ty));
944944
try!(word(&mut self.s, ","));

src/librustc_lint/bad_style.rs

Lines changed: 1 addition & 4 deletions
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 Some(name) = sf.node.name {
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.node.name.as_str(), Some(sf.span));
290287
}
291288
}
292289
}

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl LateLintPass for MissingDoc {
428428
}
429429

430430
fn check_struct_field(&mut self, cx: &LateContext, sf: &hir::StructField) {
431-
if sf.node.name.is_some() {
431+
if !sf.node.is_positional() {
432432
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");

src/librustc_metadata/decoder.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ use rbml::reader;
4848
use rbml;
4949
use serialize::Decodable;
5050
use syntax::attr;
51-
use syntax::parse::token::{IdentInterner, special_idents};
52-
use syntax::parse::token;
51+
use syntax::parse::token::{self, IdentInterner};
5352
use syntax::ast;
5453
use syntax::abi::Abi;
5554
use syntax::codemap::{self, Span, BytePos, NO_EXPANSION};
@@ -406,6 +405,7 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
406405
cdata: Cmd,
407406
doc: rbml::Doc,
408407
tcx: &ty::ctxt<'tcx>) -> Vec<ty::FieldDefData<'tcx, 'tcx>> {
408+
let mut index = 0;
409409
reader::tagged_docs(doc, tag_item_field).map(|f| {
410410
let ff = item_family(f);
411411
match ff {
@@ -417,8 +417,9 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
417417
struct_field_family_to_visibility(ff))
418418
}).chain(reader::tagged_docs(doc, tag_item_unnamed_field).map(|f| {
419419
let ff = item_family(f);
420-
ty::FieldDefData::new(item_def_id(f, cdata),
421-
special_idents::unnamed_field.name,
420+
let name = intr.intern(&index.to_string());
421+
index += 1;
422+
ty::FieldDefData::new(item_def_id(f, cdata), name,
422423
struct_field_family_to_visibility(ff))
423424
})).collect()
424425
}
@@ -1153,10 +1154,13 @@ fn struct_field_family_to_visibility(family: Family) -> hir::Visibility {
11531154
pub fn get_struct_field_names(intr: &IdentInterner, cdata: Cmd, id: DefIndex)
11541155
-> Vec<ast::Name> {
11551156
let item = cdata.lookup_item(id);
1157+
let mut index = 0;
11561158
reader::tagged_docs(item, tag_item_field).map(|an_item| {
11571159
item_name(intr, an_item)
11581160
}).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| {
1159-
special_idents::unnamed_field.name
1161+
let name = intr.intern(&index.to_string());
1162+
index += 1;
1163+
name
11601164
})).collect()
11611165
}
11621166

src/librustc_metadata/encoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use syntax::codemap::BytePos;
4646
use syntax::attr;
4747
use syntax::attr::AttrMetaMethods;
4848
use syntax::errors::Handler;
49-
use syntax::parse::token::special_idents;
5049
use syntax;
5150
use rbml::writer::Encoder;
5251

@@ -249,7 +248,7 @@ fn encode_parent_item(rbml_w: &mut Encoder, id: DefId) {
249248
fn encode_struct_fields(rbml_w: &mut Encoder,
250249
variant: ty::VariantDef) {
251250
for f in &variant.fields {
252-
if f.name == special_idents::unnamed_field.name {
251+
if variant.is_tuple_struct() {
253252
rbml_w.start_tag(tag_item_unnamed_field);
254253
} else {
255254
rbml_w.start_tag(tag_item_field);

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,12 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
380380
}
381381

382382
// Record the def ID and fields of this struct.
383-
let named_fields = struct_def.fields()
384-
.iter()
385-
.filter_map(|f| f.node.name)
386-
.collect();
383+
let field_names = struct_def.fields()
384+
.iter()
385+
.map(|f| f.node.name)
386+
.collect();
387387
let item_def_id = self.ast_map.local_def_id(item.id);
388-
self.structs.insert(item_def_id, named_fields);
388+
self.structs.insert(item_def_id, field_names);
389389

390390
parent
391391
}

src/librustc_typeck/check/dropck.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use util::nodemap::FnvHashSet;
2121

2222
use syntax::ast;
2323
use syntax::codemap::{self, Span};
24-
use syntax::parse::token::special_idents;
2524

2625
/// check_drop_impl confirms that the Drop implementation identfied by
2726
/// `drop_impl_did` is not any more specialized than the type it is
@@ -299,7 +298,7 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
299298
// no need for an additional note if the overflow
300299
// was somehow on the root.
301300
}
302-
TypeContext::ADT { def_id, variant, field, field_index } => {
301+
TypeContext::ADT { def_id, variant, field } => {
303302
let adt = tcx.lookup_adt_def(def_id);
304303
let variant_name = match adt.adt_kind() {
305304
ty::AdtKind::Enum => format!("enum {} variant {}",
@@ -308,17 +307,12 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
308307
ty::AdtKind::Struct => format!("struct {}",
309308
tcx.item_path_str(def_id))
310309
};
311-
let field_name = if field == special_idents::unnamed_field.name {
312-
format!("#{}", field_index)
313-
} else {
314-
format!("`{}`", field)
315-
};
316310
span_note!(
317311
&mut err,
318312
span,
319313
"overflowed on {} field {} type: {}",
320314
variant_name,
321-
field_name,
315+
field,
322316
detected_on_typ);
323317
}
324318
}
@@ -338,7 +332,6 @@ enum TypeContext {
338332
def_id: DefId,
339333
variant: ast::Name,
340334
field: ast::Name,
341-
field_index: usize
342335
}
343336
}
344337

@@ -452,7 +445,7 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
452445
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
453446
let did = def.did;
454447
for variant in &def.variants {
455-
for (i, field) in variant.fields.iter().enumerate() {
448+
for field in variant.fields.iter() {
456449
let fty = field.ty(tcx, substs);
457450
let fty = cx.rcx.fcx.resolve_type_vars_if_possible(
458451
cx.rcx.fcx.normalize_associated_types_in(cx.span, &fty));
@@ -462,7 +455,6 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
462455
def_id: did,
463456
field: field.name,
464457
variant: variant.name,
465-
field_index: i
466458
},
467459
fty,
468460
depth+1))

src/librustc_typeck/coherence/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use middle::infer::{self, InferCtxt, TypeOrigin, new_infer_ctxt};
3636
use std::cell::RefCell;
3737
use std::rc::Rc;
3838
use syntax::codemap::Span;
39-
use syntax::parse::token;
4039
use util::nodemap::{DefIdMap, FnvHashMap};
4140
use rustc::dep_graph::DepNode;
4241
use rustc::front::map as hir_map;
@@ -449,13 +448,7 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
449448
for a coercion between structures with one field \
450449
being coerced, but {} fields need coercions: {}",
451450
diff_fields.len(), diff_fields.iter().map(|&(i, a, b)| {
452-
let name = fields[i].name;
453-
format!("{} ({} to {})",
454-
if name == token::special_names::unnamed_field {
455-
i.to_string()
456-
} else {
457-
name.to_string()
458-
}, a, b)
451+
format!("{} ({} to {})", fields[i].name, a, b)
459452
}).collect::<Vec<_>>().join(", "));
460453
return;
461454
}

src/librustc_typeck/collect.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -978,22 +978,18 @@ 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-
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);
991-
}
992-
993-
ty::FieldDefData::new(fid, name, f.node.vis)
981+
let dup_span = seen_fields.get(&f.node.name).cloned();
982+
if let Some(prev_span) = dup_span {
983+
let mut err = struct_span_err!(tcx.sess, f.span, E0124,
984+
"field `{}` is already declared",
985+
f.node.name);
986+
span_note!(&mut err, prev_span, "previously declared here");
987+
err.emit();
994988
} else {
995-
ty::FieldDefData::new(fid, special_idents::unnamed_field.name, f.node.vis)
989+
seen_fields.insert(f.node.name, f.span);
996990
}
991+
992+
ty::FieldDefData::new(fid, f.node.name, f.node.vis)
997993
}).collect();
998994
ty::VariantDefData {
999995
did: did,

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,15 @@ fn build_external_function(cx: &DocContext, tcx: &ty::ctxt, did: DefId) -> clean
188188
}
189189

190190
fn build_struct(cx: &DocContext, tcx: &ty::ctxt, did: DefId) -> clean::Struct {
191-
use syntax::parse::token::special_idents::unnamed_field;
192-
193191
let t = tcx.lookup_item_type(did);
194192
let predicates = tcx.lookup_predicates(did);
195193
let variant = tcx.lookup_adt_def(did).struct_variant();
196194

197195
clean::Struct {
198196
struct_type: match &*variant.fields {
199197
[] => doctree::Unit,
200-
[ref f] if f.name == unnamed_field.name => doctree::Newtype,
201-
[ref f, ..] if f.name == unnamed_field.name => doctree::Tuple,
198+
[_] if variant.kind == ty::VariantKind::Tuple => doctree::Newtype,
199+
[..] if variant.kind == ty::VariantKind::Tuple => doctree::Tuple,
202200
_ => doctree::Plain,
203201
},
204202
generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),

0 commit comments

Comments
 (0)