Skip to content

Commit 7f3744f

Browse files
committed
Get rid of ast::StructFieldKind
1 parent 772c600 commit 7f3744f

File tree

10 files changed

+55
-136
lines changed

10 files changed

+55
-136
lines changed

src/librustc_front/lowering.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,8 @@ pub fn lower_struct_field(lctx: &LoweringContext,
622622
hir::StructField {
623623
span: f.span,
624624
id: f.node.id,
625-
name: f.node.ident().map(|ident| ident.name)
626-
.unwrap_or(token::intern(&index.to_string())),
627-
vis: lower_visibility(lctx, f.node.kind.visibility()),
625+
name: f.node.ident.map(|ident| ident.name).unwrap_or(token::intern(&index.to_string())),
626+
vis: lower_visibility(lctx, f.node.vis),
628627
ty: lower_ty(lctx, &f.node.ty),
629628
attrs: lower_attrs(lctx, &f.node.attrs),
630629
}

src/librustc_save_analysis/lib.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,22 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
246246

247247
pub fn get_field_data(&self, field: &ast::StructField,
248248
scope: NodeId) -> Option<VariableData> {
249-
match field.node.kind {
250-
ast::NamedField(ident, _) => {
251-
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
252-
let typ = self.tcx.node_types().get(&field.node.id).unwrap().to_string();
253-
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
254-
filter!(self.span_utils, sub_span, field.span, None);
255-
Some(VariableData {
256-
id: field.node.id,
257-
name: ident.to_string(),
258-
qualname: qualname,
259-
span: sub_span.unwrap(),
260-
scope: scope,
261-
value: "".to_owned(),
262-
type_value: typ,
263-
})
264-
}
265-
_ => None,
249+
if let Some(ident) = field.node.ident {
250+
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
251+
let typ = self.tcx.node_types().get(&field.node.id).unwrap().to_string();
252+
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
253+
filter!(self.span_utils, sub_span, field.span, None);
254+
Some(VariableData {
255+
id: field.node.id,
256+
name: ident.to_string(),
257+
qualname: qualname,
258+
span: sub_span.unwrap(),
259+
scope: scope,
260+
value: "".to_owned(),
261+
type_value: typ,
262+
})
263+
} else {
264+
None
266265
}
267266
}
268267

src/libsyntax/ast.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// The Rust abstract syntax tree.
1212

13-
pub use self::StructFieldKind::*;
1413
pub use self::TyParamBound::*;
1514
pub use self::UnsafeSource::*;
1615
pub use self::ViewPath_::*;
@@ -1878,45 +1877,15 @@ pub enum Visibility {
18781877

18791878
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18801879
pub struct StructField_ {
1881-
pub kind: StructFieldKind,
1880+
pub ident: Option<Ident>,
1881+
pub vis: Visibility,
18821882
pub id: NodeId,
18831883
pub ty: P<Ty>,
18841884
pub attrs: Vec<Attribute>,
18851885
}
18861886

1887-
impl StructField_ {
1888-
pub fn ident(&self) -> Option<Ident> {
1889-
match self.kind {
1890-
NamedField(ref ident, _) => Some(ident.clone()),
1891-
UnnamedField(_) => None
1892-
}
1893-
}
1894-
}
1895-
18961887
pub type StructField = Spanned<StructField_>;
18971888

1898-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1899-
pub enum StructFieldKind {
1900-
NamedField(Ident, Visibility),
1901-
/// Element of a tuple-like struct
1902-
UnnamedField(Visibility),
1903-
}
1904-
1905-
impl StructFieldKind {
1906-
pub fn is_unnamed(&self) -> bool {
1907-
match *self {
1908-
UnnamedField(..) => true,
1909-
NamedField(..) => false,
1910-
}
1911-
}
1912-
1913-
pub fn visibility(&self) -> &Visibility {
1914-
match *self {
1915-
NamedField(_, ref vis) | UnnamedField(ref vis) => vis
1916-
}
1917-
}
1918-
}
1919-
19201889
/// Fields and Ids of enum variants and structs
19211890
///
19221891
/// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all

src/libsyntax/ast_util.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident
9595
token::gensym_ident(&pretty[..])
9696
}
9797

98-
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
99-
match field.node.kind {
100-
ast::NamedField(_, v) | ast::UnnamedField(v) => v
101-
}
102-
}
103-
10498
// ______________________________________________________________________
10599
// Enumerating the IDs which appear in an AST
106100

src/libsyntax/ext/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
10091009
let fields: Vec<_> = tys.into_iter().map(|ty| {
10101010
Spanned { span: ty.span, node: ast::StructField_ {
10111011
ty: ty,
1012-
kind: ast::UnnamedField(ast::Visibility::Inherited),
1012+
ident: None,
1013+
vis: ast::Visibility::Inherited,
10131014
attrs: Vec::new(),
10141015
id: ast::DUMMY_NODE_ID,
10151016
}}

src/libsyntax/fold.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,15 @@ pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> Poly
847847
}
848848

849849
pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
850-
let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
851850
Spanned {
852851
node: StructField_ {
853-
id: fld.new_id(id),
854-
kind: kind,
855-
ty: fld.fold_ty(ty),
856-
attrs: fold_attrs(attrs, fld),
852+
id: fld.new_id(f.node.id),
853+
ident: f.node.ident.map(|ident| fld.fold_ident(ident)),
854+
vis: f.node.vis,
855+
ty: fld.fold_ty(f.node.ty),
856+
attrs: fold_attrs(f.node.attrs, fld),
857857
},
858-
span: fld.new_span(span)
858+
span: fld.new_span(f.span)
859859
}
860860
}
861861

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use ast::Local;
2929
use ast::MacStmtStyle;
3030
use ast::Mac_;
3131
use ast::{MutTy, Mutability};
32-
use ast::NamedField;
3332
use ast::{Pat, PatKind};
3433
use ast::{PolyTraitRef, QSelf};
3534
use ast::{Stmt, StmtKind};
@@ -38,7 +37,6 @@ use ast::StrStyle;
3837
use ast::SelfKind;
3938
use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
4039
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
41-
use ast::UnnamedField;
4240
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
4341
use ast::{Visibility, WhereClause};
4442
use attr::{ThinAttributes, ThinAttributesExt, AttributesExt};
@@ -3848,7 +3846,8 @@ impl<'a> Parser<'a> {
38483846
self.expect(&token::Colon)?;
38493847
let ty = self.parse_ty_sum()?;
38503848
Ok(spanned(lo, self.last_span.hi, ast::StructField_ {
3851-
kind: NamedField(name, pr),
3849+
ident: Some(name),
3850+
vis: pr,
38523851
id: ast::DUMMY_NODE_ID,
38533852
ty: ty,
38543853
attrs: attrs,
@@ -5247,7 +5246,8 @@ impl<'a> Parser<'a> {
52475246
let attrs = p.parse_outer_attributes()?;
52485247
let lo = p.span.lo;
52495248
let struct_field_ = ast::StructField_ {
5250-
kind: UnnamedField(p.parse_visibility()?),
5249+
vis: p.parse_visibility()?,
5250+
ident: None,
52515251
id: ast::DUMMY_NODE_ID,
52525252
ty: p.parse_ty_sum()?,
52535253
attrs: attrs,

src/libsyntax/print/pprust.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,9 @@ impl<'a> State<'a> {
14071407
self.commasep(
14081408
Inconsistent, struct_def.fields(),
14091409
|s, field| {
1410-
match field.node.kind {
1411-
ast::NamedField(..) => panic!("unexpected named field"),
1412-
ast::UnnamedField(ref vis) => {
1413-
s.print_visibility(vis)?;
1414-
s.maybe_print_comment(field.span.lo)?;
1415-
s.print_type(&field.node.ty)
1416-
}
1417-
}
1410+
s.print_visibility(field.node.vis)?;
1411+
s.maybe_print_comment(field.span.lo)?;
1412+
s.print_type(&field.node.ty)
14181413
}
14191414
)?;
14201415
self.pclose()?;
@@ -1432,19 +1427,14 @@ impl<'a> State<'a> {
14321427
self.hardbreak_if_not_bol()?;
14331428

14341429
for field in struct_def.fields() {
1435-
match field.node.kind {
1436-
ast::UnnamedField(..) => panic!("unexpected unnamed field"),
1437-
ast::NamedField(ident, ref visibility) => {
1438-
self.hardbreak_if_not_bol()?;
1439-
self.maybe_print_comment(field.span.lo)?;
1440-
self.print_outer_attributes(&field.node.attrs)?;
1441-
self.print_visibility(visibility)?;
1442-
self.print_ident(ident)?;
1443-
self.word_nbsp(":")?;
1444-
self.print_type(&field.node.ty)?;
1445-
word(&mut self.s, ",")?;
1446-
}
1447-
}
1430+
self.hardbreak_if_not_bol()?;
1431+
self.maybe_print_comment(field.span.lo)?;
1432+
self.print_outer_attributes(&field.node.attrs)?;
1433+
self.print_visibility(field.node.vis)?;
1434+
self.print_ident(field.node.ident.unwrap())?;
1435+
self.word_nbsp(":")?;
1436+
self.print_type(&field.node.ty)?;
1437+
word(&mut self.s, ",")?;
14481438
}
14491439

14501440
self.bclose(span)

src/libsyntax/visit.rs

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

620620
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
621621
struct_field: &'v StructField) {
622-
walk_opt_ident(visitor, struct_field.span, struct_field.node.ident());
622+
walk_opt_ident(visitor, struct_field.span, struct_field.node.ident);
623623
visitor.visit_ty(&struct_field.node.ty);
624624
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
625625
}

src/libsyntax_ext/deriving/generic/mod.rs

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@
186186
187187
pub use self::StaticFields::*;
188188
pub use self::SubstructureFields::*;
189-
use self::StructType::*;
190189

191190
use std::cell::RefCell;
192191
use std::collections::HashSet;
@@ -1409,11 +1408,6 @@ impl<'a> MethodDef<'a> {
14091408
}
14101409
}
14111410

1412-
#[derive(PartialEq)] // dogfooding!
1413-
enum StructType {
1414-
Unknown, Record, Tuple
1415-
}
1416-
14171411
// general helper methods.
14181412
impl<'a> TraitDef<'a> {
14191413
fn set_expn_info(&self,
@@ -1441,9 +1435,9 @@ impl<'a> TraitDef<'a> {
14411435
let mut just_spans = Vec::new();
14421436
for field in struct_def.fields(){
14431437
let sp = self.set_expn_info(cx, field.span);
1444-
match field.node.kind {
1445-
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
1446-
ast::UnnamedField(..) => just_spans.push(sp),
1438+
match field.node.ident {
1439+
Some(ident) => named_idents.push((ident, sp)),
1440+
_ => just_spans.push(sp),
14471441
}
14481442
}
14491443

@@ -1479,61 +1473,34 @@ impl<'a> TraitDef<'a> {
14791473
-> (P<ast::Pat>, Vec<(Span, Option<Ident>,
14801474
P<Expr>,
14811475
&'a [ast::Attribute])>) {
1482-
if struct_def.fields().is_empty() {
1483-
if struct_def.is_struct() {
1484-
return (cx.pat_struct(self.span, struct_path, vec![]), vec![]);
1485-
} else {
1486-
return (cx.pat_enum(self.span, struct_path, vec![]), vec![]);
1487-
}
1488-
}
1489-
14901476
let mut paths = Vec::new();
1491-
let mut ident_expr = Vec::new();
1492-
let mut struct_type = Unknown;
1493-
1477+
let mut ident_exprs = Vec::new();
14941478
for (i, struct_field) in struct_def.fields().iter().enumerate() {
14951479
let sp = self.set_expn_info(cx, struct_field.span);
1496-
let opt_id = match struct_field.node.kind {
1497-
ast::NamedField(ident, _) if (struct_type == Unknown ||
1498-
struct_type == Record) => {
1499-
struct_type = Record;
1500-
Some(ident)
1501-
}
1502-
ast::UnnamedField(..) if (struct_type == Unknown ||
1503-
struct_type == Tuple) => {
1504-
struct_type = Tuple;
1505-
None
1506-
}
1507-
_ => {
1508-
cx.span_bug(sp, "a struct with named and unnamed fields in `derive`");
1509-
}
1510-
};
15111480
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
15121481
paths.push(codemap::Spanned{span: sp, node: ident});
15131482
let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)));
15141483
let val = cx.expr(sp, ast::ExprKind::Paren(val));
1515-
ident_expr.push((sp, opt_id, val, &struct_field.node.attrs[..]));
1484+
ident_exprs.push((sp, struct_field.node.ident, val, &struct_field.node.attrs[..]));
15161485
}
15171486

15181487
let subpats = self.create_subpatterns(cx, paths, mutbl);
1519-
1520-
// struct_type is definitely not Unknown, since struct_def.fields
1521-
// must be nonempty to reach here
15221488
let pattern = if struct_def.is_struct() {
1523-
let field_pats = subpats.into_iter().zip(&ident_expr)
1524-
.map(|(pat, &(_, id, _, _))| {
1525-
// id is guaranteed to be Some
1489+
let field_pats = subpats.into_iter().zip(&ident_exprs).map(|(pat, &(sp, ident, _, _))| {
1490+
if ident.is_none() {
1491+
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
1492+
}
15261493
codemap::Spanned {
15271494
span: pat.span,
1528-
node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: false },
1495+
node: ast::FieldPat { ident: ident.unwrap(), pat: pat, is_shorthand: false },
15291496
}
15301497
}).collect();
15311498
cx.pat_struct(self.span, struct_path, field_pats)
15321499
} else {
15331500
cx.pat_enum(self.span, struct_path, subpats)
15341501
};
15351502

1536-
(pattern, ident_expr)
1503+
(pattern, ident_exprs)
15371504
}
15381505

15391506
fn create_enum_variant_pattern(&self,

0 commit comments

Comments
 (0)