Skip to content

Commit 3e3e2f0

Browse files
committed
allow parsing attributes on struct fields
1 parent c2e1f47 commit 3e3e2f0

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,7 @@ pub struct struct_field_ {
11581158
kind: struct_field_kind,
11591159
id: node_id,
11601160
ty: @Ty,
1161+
attrs: ~[attribute],
11611162
}
11621163

11631164
pub type struct_field = spanned<struct_field_>;

src/libsyntax/ext/pipes/pipec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ impl gen_init for protocol {
415415
ast::struct_immutable,
416416
ast::inherited),
417417
id: cx.next_id(),
418-
ty: fty
418+
ty: fty,
419+
attrs: ~[],
419420
},
420421
span: dummy_sp()
421422
}

src/libsyntax/fold.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ pub fn noop_fold_item(i: @item, fld: @ast_fold) -> Option<@item> {
222222

223223
fn noop_fold_struct_field(sf: @struct_field, fld: @ast_fold)
224224
-> @struct_field {
225+
let fold_attribute = |x| fold_attribute_(x, fld);
226+
225227
@spanned { node: ast::struct_field_ { kind: copy sf.node.kind,
226228
id: sf.node.id,
227-
ty: fld.fold_ty(sf.node.ty) },
229+
ty: fld.fold_ty(sf.node.ty),
230+
attrs: sf.node.attrs.map(|e| fold_attribute(*e)) },
228231
span: sf.span }
229232
}
230233

@@ -309,6 +312,7 @@ fn fold_struct_field(f: @struct_field, fld: @ast_fold) -> @struct_field {
309312
kind: copy f.node.kind,
310313
id: fld.new_id(f.node.id),
311314
ty: fld.fold_ty(f.node.ty),
315+
attrs: /* FIXME (#2543) */ copy f.node.attrs,
312316
},
313317
span: fld.new_span(f.span),
314318
}
@@ -757,6 +761,7 @@ impl ast_fold for AstFoldFns {
757761
kind: copy sf.node.kind,
758762
id: sf.node.id,
759763
ty: (self as @ast_fold).fold_ty(sf.node.ty),
764+
attrs: copy sf.node.attrs,
760765
},
761766
span: (self.new_span)(sf.span),
762767
}

src/libsyntax/parse/obsolete.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ removed.
1818
*/
1919

2020

21-
use ast::{expr, expr_lit, lit_nil};
21+
use ast::{expr, expr_lit, lit_nil, attribute};
2222
use ast;
2323
use codemap::{span, respan};
2424
use parse::parser::Parser;
@@ -282,13 +282,13 @@ pub impl Parser {
282282
}
283283
}
284284

285-
fn try_parse_obsolete_priv_section(&self) -> bool {
285+
fn try_parse_obsolete_priv_section(&self, attrs: ~[attribute]) -> bool {
286286
if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE {
287287
self.obsolete(copy *self.span, ObsoletePrivSection);
288288
self.eat_keyword(&~"priv");
289289
self.bump();
290290
while *self.token != token::RBRACE {
291-
self.parse_single_struct_field(ast::private);
291+
self.parse_single_struct_field(ast::private, attrs);
292292
}
293293
self.bump();
294294
true

src/libsyntax/parse/parser.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,9 @@ pub impl Parser {
25202520
}
25212521
25222522
// parse a structure field
2523-
fn parse_name_and_ty(&self, pr: visibility) -> @struct_field {
2523+
fn parse_name_and_ty(&self,
2524+
pr: visibility,
2525+
attrs: ~[attribute]) -> @struct_field {
25242526
let mut is_mutbl = struct_immutable;
25252527
let lo = self.span.lo;
25262528
if self.eat_keyword(&~"mut") {
@@ -2535,7 +2537,8 @@ pub impl Parser {
25352537
@spanned(lo, self.last_span.hi, ast::struct_field_ {
25362538
kind: named_field(name, is_mutbl, pr),
25372539
id: self.get_id(),
2538-
ty: ty
2540+
ty: ty,
2541+
attrs: attrs,
25392542
})
25402543
}
25412544

@@ -3318,11 +3321,13 @@ pub impl Parser {
33183321
&token::RPAREN,
33193322
seq_sep_trailing_allowed(token::COMMA)
33203323
) |p| {
3324+
let attrs = self.parse_outer_attributes();
33213325
let lo = p.span.lo;
33223326
let struct_field_ = ast::struct_field_ {
33233327
kind: unnamed_field,
33243328
id: self.get_id(),
3325-
ty: p.parse_ty(false)
3329+
ty: p.parse_ty(false),
3330+
attrs: attrs,
33263331
};
33273332
@spanned(lo, p.span.hi, struct_field_)
33283333
};
@@ -3359,12 +3364,14 @@ pub impl Parser {
33593364
}
33603365

33613366
// parse a structure field declaration
3362-
fn parse_single_struct_field(&self, vis: visibility) -> @struct_field {
3367+
fn parse_single_struct_field(&self,
3368+
vis: visibility,
3369+
attrs: ~[attribute]) -> @struct_field {
33633370
if self.eat_obsolete_ident("let") {
33643371
self.obsolete(*self.last_span, ObsoleteLet);
33653372
}
33663373

3367-
let a_var = self.parse_name_and_ty(vis);
3374+
let a_var = self.parse_name_and_ty(vis, attrs);
33683375
match *self.token {
33693376
token::SEMI => {
33703377
self.obsolete(copy *self.span, ObsoleteFieldTerminator);
@@ -3390,26 +3397,25 @@ pub impl Parser {
33903397
// parse an element of a struct definition
33913398
fn parse_struct_decl_field(&self) -> ~[@struct_field] {
33923399

3393-
if self.try_parse_obsolete_priv_section() {
3400+
let attrs = self.parse_outer_attributes();
3401+
3402+
if self.try_parse_obsolete_priv_section(attrs) {
33943403
return ~[];
33953404
}
33963405

3397-
// Need this to parse comments on fields.
3398-
let _attrs = self.parse_outer_attributes();
3399-
34003406
if self.eat_keyword(&~"priv") {
3401-
return ~[self.parse_single_struct_field(private)]
3407+
return ~[self.parse_single_struct_field(private, attrs)]
34023408
}
34033409

34043410
if self.eat_keyword(&~"pub") {
3405-
return ~[self.parse_single_struct_field(public)];
3411+
return ~[self.parse_single_struct_field(public, attrs)];
34063412
}
34073413

34083414
if self.try_parse_obsolete_struct_ctor() {
34093415
return ~[];
34103416
}
34113417

3412-
return ~[self.parse_single_struct_field(inherited)];
3418+
return ~[self.parse_single_struct_field(inherited, attrs)];
34133419
}
34143420

34153421
// parse visiility: PUB, PRIV, or nothing

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ pub fn print_struct(s: @ps,
700700
ast::named_field(ident, mutability, visibility) => {
701701
hardbreak_if_not_bol(s);
702702
maybe_print_comment(s, field.span.lo);
703+
print_outer_attributes(s, field.node.attrs);
703704
print_visibility(s, visibility);
704705
if mutability == ast::struct_mutable {
705706
word_nbsp(s, ~"mut");

0 commit comments

Comments
 (0)