about summary refs log tree commit diff
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-04-30 20:20:08 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-05-01 07:49:41 -0700
commit3e3e2f00250d77719598ad052bb2016d46e6a148 (patch)
tree54d73d9114028163156ef6947e6b95d80a91c25f
parentc2e1f47955571fab24fc731c0af97e4c71f4ada9 (diff)
downloadrust-3e3e2f00250d77719598ad052bb2016d46e6a148.tar.gz
rust-3e3e2f00250d77719598ad052bb2016d46e6a148.zip
allow parsing attributes on struct fields
-rw-r--r--src/libsyntax/ast.rs1
-rw-r--r--src/libsyntax/ext/pipes/pipec.rs3
-rw-r--r--src/libsyntax/fold.rs7
-rw-r--r--src/libsyntax/parse/obsolete.rs6
-rw-r--r--src/libsyntax/parse/parser.rs30
-rw-r--r--src/libsyntax/print/pprust.rs1
6 files changed, 31 insertions, 17 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 71fd506ebed..a853dbed2f1 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -1158,6 +1158,7 @@ pub struct struct_field_ {
     kind: struct_field_kind,
     id: node_id,
     ty: @Ty,
+    attrs: ~[attribute],
 }
 
 pub type struct_field = spanned<struct_field_>;
diff --git a/src/libsyntax/ext/pipes/pipec.rs b/src/libsyntax/ext/pipes/pipec.rs
index 3ad94905f7f..e876972fe68 100644
--- a/src/libsyntax/ext/pipes/pipec.rs
+++ b/src/libsyntax/ext/pipes/pipec.rs
@@ -415,7 +415,8 @@ impl gen_init for protocol {
                             ast::struct_immutable,
                             ast::inherited),
                     id: cx.next_id(),
-                    ty: fty
+                    ty: fty,
+                    attrs: ~[],
                 },
                 span: dummy_sp()
             }
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index adfc95f3e94..36565395e59 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -222,9 +222,12 @@ pub fn noop_fold_item(i: @item, fld: @ast_fold) -> Option<@item> {
 
 fn noop_fold_struct_field(sf: @struct_field, fld: @ast_fold)
                        -> @struct_field {
+    let fold_attribute = |x| fold_attribute_(x, fld);
+
     @spanned { node: ast::struct_field_ { kind: copy sf.node.kind,
                                           id: sf.node.id,
-                                          ty: fld.fold_ty(sf.node.ty) },
+                                          ty: fld.fold_ty(sf.node.ty),
+                                          attrs: sf.node.attrs.map(|e| fold_attribute(*e)) },
                span: sf.span }
 }
 
@@ -309,6 +312,7 @@ fn fold_struct_field(f: @struct_field, fld: @ast_fold) -> @struct_field {
             kind: copy f.node.kind,
             id: fld.new_id(f.node.id),
             ty: fld.fold_ty(f.node.ty),
+            attrs: /* FIXME (#2543) */ copy f.node.attrs,
         },
         span: fld.new_span(f.span),
     }
@@ -757,6 +761,7 @@ impl ast_fold for AstFoldFns {
                 kind: copy sf.node.kind,
                 id: sf.node.id,
                 ty: (self as @ast_fold).fold_ty(sf.node.ty),
+                attrs: copy sf.node.attrs,
             },
             span: (self.new_span)(sf.span),
         }
diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs
index ce21e0f672d..c1afc53def0 100644
--- a/src/libsyntax/parse/obsolete.rs
+++ b/src/libsyntax/parse/obsolete.rs
@@ -18,7 +18,7 @@ removed.
 */
 
 
-use ast::{expr, expr_lit, lit_nil};
+use ast::{expr, expr_lit, lit_nil, attribute};
 use ast;
 use codemap::{span, respan};
 use parse::parser::Parser;
@@ -282,13 +282,13 @@ pub impl Parser {
         }
     }
 
-    fn try_parse_obsolete_priv_section(&self) -> bool {
+    fn try_parse_obsolete_priv_section(&self, attrs: ~[attribute]) -> bool {
         if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE {
             self.obsolete(copy *self.span, ObsoletePrivSection);
             self.eat_keyword(&~"priv");
             self.bump();
             while *self.token != token::RBRACE {
-                self.parse_single_struct_field(ast::private);
+                self.parse_single_struct_field(ast::private, attrs);
             }
             self.bump();
             true
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 27a1cde2f96..74af7458408 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -2520,7 +2520,9 @@ pub impl Parser {
     }
 
     // parse a structure field
-    fn parse_name_and_ty(&self, pr: visibility) -> @struct_field {
+    fn parse_name_and_ty(&self,
+                         pr: visibility,
+                         attrs: ~[attribute]) -> @struct_field {
         let mut is_mutbl = struct_immutable;
         let lo = self.span.lo;
         if self.eat_keyword(&~"mut") {
@@ -2535,7 +2537,8 @@ pub impl Parser {
         @spanned(lo, self.last_span.hi, ast::struct_field_ {
             kind: named_field(name, is_mutbl, pr),
             id: self.get_id(),
-            ty: ty
+            ty: ty,
+            attrs: attrs,
         })
     }
 
@@ -3318,11 +3321,13 @@ pub impl Parser {
                 &token::RPAREN,
                 seq_sep_trailing_allowed(token::COMMA)
             ) |p| {
+                let attrs = self.parse_outer_attributes();
                 let lo = p.span.lo;
                 let struct_field_ = ast::struct_field_ {
                     kind: unnamed_field,
                     id: self.get_id(),
-                    ty: p.parse_ty(false)
+                    ty: p.parse_ty(false),
+                    attrs: attrs,
                 };
                 @spanned(lo, p.span.hi, struct_field_)
             };
@@ -3359,12 +3364,14 @@ pub impl Parser {
     }
 
     // parse a structure field declaration
-    fn parse_single_struct_field(&self, vis: visibility) -> @struct_field {
+    fn parse_single_struct_field(&self,
+                                 vis: visibility,
+                                 attrs: ~[attribute]) -> @struct_field {
         if self.eat_obsolete_ident("let") {
             self.obsolete(*self.last_span, ObsoleteLet);
         }
 
-        let a_var = self.parse_name_and_ty(vis);
+        let a_var = self.parse_name_and_ty(vis, attrs);
         match *self.token {
             token::SEMI => {
                 self.obsolete(copy *self.span, ObsoleteFieldTerminator);
@@ -3390,26 +3397,25 @@ pub impl Parser {
     // parse an element of a struct definition
     fn parse_struct_decl_field(&self) -> ~[@struct_field] {
 
-        if self.try_parse_obsolete_priv_section() {
+        let attrs = self.parse_outer_attributes();
+
+        if self.try_parse_obsolete_priv_section(attrs) {
             return ~[];
         }
 
-        // Need this to parse comments on fields.
-        let _attrs = self.parse_outer_attributes();
-
         if self.eat_keyword(&~"priv") {
-            return ~[self.parse_single_struct_field(private)]
+            return ~[self.parse_single_struct_field(private, attrs)]
         }
 
         if self.eat_keyword(&~"pub") {
-           return ~[self.parse_single_struct_field(public)];
+           return ~[self.parse_single_struct_field(public, attrs)];
         }
 
         if self.try_parse_obsolete_struct_ctor() {
             return ~[];
         }
 
-        return ~[self.parse_single_struct_field(inherited)];
+        return ~[self.parse_single_struct_field(inherited, attrs)];
     }
 
     // parse visiility: PUB, PRIV, or nothing
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index f23badd0462..ab958d8b5ce 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -700,6 +700,7 @@ pub fn print_struct(s: @ps,
                 ast::named_field(ident, mutability, visibility) => {
                     hardbreak_if_not_bol(s);
                     maybe_print_comment(s, field.span.lo);
+                    print_outer_attributes(s, field.node.attrs);
                     print_visibility(s, visibility);
                     if mutability == ast::struct_mutable {
                         word_nbsp(s, ~"mut");