diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2016-04-06 17:15:31 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2016-04-06 17:15:31 +0530 |
| commit | af7b00b68fc7960e98fb914be52d9a6a16fe2224 (patch) | |
| tree | 13260050b5b5463c491ae61c941f60477421fdc0 /src/libsyntax | |
| parent | 772c600d4d6f39daa6d07d1a60ee0df3d3426978 (diff) | |
| parent | 8fe4290f1cf87bf7b0a0661e6bbe84f3319e614d (diff) | |
Rollup merge of #32682 - petrochenkov:field3, r=Manishearth
The AST part of https://github.com/rust-lang/rust/pull/31937 Unlike HIR, AST still uses `Option` for field names because parser can't know field indexes reliably due to constructions like ``` struct S(#[cfg(false)] u8, u8); // The index of the second field changes from 1 during parsing to 0 after expansion. ``` and I wouldn't like to put the burden of renaming fields on expansion passes and syntax extensions. plugin-[breaking-change] cc https://github.com/rust-lang/rust/issues/31645 r? @Manishearth
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 40 | ||||
| -rw-r--r-- | src/libsyntax/ast_util.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/config.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 23 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 32 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 6 |
8 files changed, 47 insertions, 92 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a441f2990cd..dbe7b078498 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -10,7 +10,6 @@ // The Rust abstract syntax tree. -pub use self::StructFieldKind::*; pub use self::TyParamBound::*; pub use self::UnsafeSource::*; pub use self::ViewPath_::*; @@ -1877,46 +1876,15 @@ pub enum Visibility { } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub struct StructField_ { - pub kind: StructFieldKind, +pub struct StructField { + pub span: Span, + pub ident: Option<Ident>, + pub vis: Visibility, pub id: NodeId, pub ty: P<Ty>, pub attrs: Vec<Attribute>, } -impl StructField_ { - pub fn ident(&self) -> Option<Ident> { - match self.kind { - NamedField(ref ident, _) => Some(ident.clone()), - UnnamedField(_) => None - } - } -} - -pub type StructField = Spanned<StructField_>; - -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub enum StructFieldKind { - NamedField(Ident, Visibility), - /// Element of a tuple-like struct - UnnamedField(Visibility), -} - -impl StructFieldKind { - pub fn is_unnamed(&self) -> bool { - match *self { - UnnamedField(..) => true, - NamedField(..) => false, - } - } - - pub fn visibility(&self) -> &Visibility { - match *self { - NamedField(_, ref vis) | UnnamedField(ref vis) => vis - } - } -} - /// Fields and Ids of enum variants and structs /// /// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 852b153044f..ffba9e824d4 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -95,12 +95,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident token::gensym_ident(&pretty[..]) } -pub fn struct_field_visibility(field: ast::StructField) -> Visibility { - match field.node.kind { - ast::NamedField(_, v) | ast::UnnamedField(v) => v - } -} - // ______________________________________________________________________ // Enumerating the IDs which appear in an AST @@ -269,7 +263,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { } fn visit_struct_field(&mut self, struct_field: &StructField) { - self.operation.visit_id(struct_field.node.id); + self.operation.visit_id(struct_field.id); visit::walk_struct_field(self, struct_field) } diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 9acb1805cdd..4554a280e5f 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -180,12 +180,12 @@ fn fold_struct<F>(cx: &mut Context<F>, vdata: ast::VariantData) -> ast::VariantD match vdata { ast::VariantData::Struct(fields, id) => { ast::VariantData::Struct(fields.into_iter().filter(|m| { - (cx.in_cfg)(&m.node.attrs) + (cx.in_cfg)(&m.attrs) }).collect(), id) } ast::VariantData::Tuple(fields, id) => { ast::VariantData::Tuple(fields.into_iter().filter(|m| { - (cx.in_cfg)(&m.node.attrs) + (cx.in_cfg)(&m.attrs) }).collect(), id) } ast::VariantData::Unit(id) => ast::VariantData::Unit(id) @@ -434,7 +434,7 @@ impl<'v, 'a, 'b> visit::Visitor<'v> for StmtExprAttrFeatureVisitor<'a, 'b> { } fn visit_struct_field(&mut self, s: &'v ast::StructField) { - if node_survives_cfg(&s.node.attrs, self.config) { + if node_survives_cfg(&s.attrs, self.config) { visit::walk_struct_field(self, s); } } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 0eb42f17f68..a4e5b68277d 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1007,12 +1007,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant { let fields: Vec<_> = tys.into_iter().map(|ty| { - Spanned { span: ty.span, node: ast::StructField_ { + ast::StructField { + span: ty.span, ty: ty, - kind: ast::UnnamedField(ast::Visibility::Inherited), + ident: None, + vis: ast::Visibility::Inherited, attrs: Vec::new(), id: ast::DUMMY_NODE_ID, - }} + } }).collect(); let vdata = if fields.is_empty() { diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 46bcb8067a3..46191b95f81 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -847,15 +847,13 @@ pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> Poly } pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField { - let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f; - Spanned { - node: StructField_ { - id: fld.new_id(id), - kind: kind, - ty: fld.fold_ty(ty), - attrs: fold_attrs(attrs, fld), - }, - span: fld.new_span(span) + StructField { + span: fld.new_span(f.span), + id: fld.new_id(f.id), + ident: f.ident.map(|ident| fld.fold_ident(ident)), + vis: f.vis, + ty: fld.fold_ty(f.ty), + attrs: fold_attrs(f.attrs, fld), } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 89a504e1ebd..28e4682f66b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -29,7 +29,6 @@ use ast::Local; use ast::MacStmtStyle; use ast::Mac_; use ast::{MutTy, Mutability}; -use ast::NamedField; use ast::{Pat, PatKind}; use ast::{PolyTraitRef, QSelf}; use ast::{Stmt, StmtKind}; @@ -38,7 +37,6 @@ use ast::StrStyle; use ast::SelfKind; use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef}; use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds}; -use ast::UnnamedField; use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple}; use ast::{Visibility, WhereClause}; use attr::{ThinAttributes, ThinAttributesExt, AttributesExt}; @@ -3847,12 +3845,14 @@ impl<'a> Parser<'a> { let name = self.parse_ident()?; self.expect(&token::Colon)?; let ty = self.parse_ty_sum()?; - Ok(spanned(lo, self.last_span.hi, ast::StructField_ { - kind: NamedField(name, pr), + Ok(StructField { + span: mk_sp(lo, self.last_span.hi), + ident: Some(name), + vis: pr, id: ast::DUMMY_NODE_ID, ty: ty, attrs: attrs, - })) + }) } /// Emit an expected item after attributes error. @@ -5246,13 +5246,16 @@ impl<'a> Parser<'a> { |p| { let attrs = p.parse_outer_attributes()?; let lo = p.span.lo; - let struct_field_ = ast::StructField_ { - kind: UnnamedField(p.parse_visibility()?), + let vis = p.parse_visibility()?; + let ty = p.parse_ty_sum()?; + Ok(StructField { + span: mk_sp(lo, p.span.hi), + vis: vis, + ident: None, id: ast::DUMMY_NODE_ID, - ty: p.parse_ty_sum()?, + ty: ty, attrs: attrs, - }; - Ok(spanned(lo, p.span.hi, struct_field_)) + }) })?; Ok(fields) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a8f28ed3d9e..e2b1d2f5e7a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1407,14 +1407,9 @@ impl<'a> State<'a> { self.commasep( Inconsistent, struct_def.fields(), |s, field| { - match field.node.kind { - ast::NamedField(..) => panic!("unexpected named field"), - ast::UnnamedField(ref vis) => { - s.print_visibility(vis)?; - s.maybe_print_comment(field.span.lo)?; - s.print_type(&field.node.ty) - } - } + s.print_visibility(&field.vis)?; + s.maybe_print_comment(field.span.lo)?; + s.print_type(&field.ty) } )?; self.pclose()?; @@ -1432,19 +1427,14 @@ impl<'a> State<'a> { self.hardbreak_if_not_bol()?; for field in struct_def.fields() { - match field.node.kind { - ast::UnnamedField(..) => panic!("unexpected unnamed field"), - ast::NamedField(ident, ref visibility) => { - self.hardbreak_if_not_bol()?; - self.maybe_print_comment(field.span.lo)?; - self.print_outer_attributes(&field.node.attrs)?; - self.print_visibility(visibility)?; - self.print_ident(ident)?; - self.word_nbsp(":")?; - self.print_type(&field.node.ty)?; - word(&mut self.s, ",")?; - } - } + self.hardbreak_if_not_bol()?; + self.maybe_print_comment(field.span.lo)?; + self.print_outer_attributes(&field.attrs)?; + self.print_visibility(&field.vis)?; + self.print_ident(field.ident.unwrap())?; + self.word_nbsp(":")?; + self.print_type(&field.ty)?; + word(&mut self.s, ",")?; } self.bclose(span) diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 1251f9bfe13..839bbf4805d 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -619,9 +619,9 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, struct_field: &'v StructField) { - walk_opt_ident(visitor, struct_field.span, struct_field.node.ident()); - visitor.visit_ty(&struct_field.node.ty); - walk_list!(visitor, visit_attribute, &struct_field.node.attrs); + walk_opt_ident(visitor, struct_field.span, struct_field.ident); + visitor.visit_ty(&struct_field.ty); + walk_list!(visitor, visit_attribute, &struct_field.attrs); } pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) { |
