Skip to content

Commit 0e498da

Browse files
committed
rustc: Allow attributes on methods. Closes #1709
1 parent 6ba3d24 commit 0e498da

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/comp/syntax/ast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ type ty_field_ = {ident: ident, mt: mt};
299299

300300
type ty_field = spanned<ty_field_>;
301301

302-
type ty_method = {ident: ident, decl: fn_decl, tps: [ty_param], span: span};
302+
type ty_method = {ident: ident, attrs: [attribute],
303+
decl: fn_decl, tps: [ty_param], span: span};
303304

304305
enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
305306

@@ -399,7 +400,8 @@ enum ret_style {
399400
return_val, // everything else
400401
}
401402

402-
type method = {ident: ident, tps: [ty_param], decl: fn_decl, body: blk,
403+
type method = {ident: ident, attrs: [attribute],
404+
tps: [ty_param], decl: fn_decl, body: blk,
403405
id: node_id, span: span};
404406

405407
type _mod = {view_items: [@view_item], items: [@item]};

src/comp/syntax/parse/parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
282282

283283
fn parse_ty_methods(p: parser) -> [ast::ty_method] {
284284
parse_seq(token::LBRACE, token::RBRACE, seq_sep_none(), {|p|
285+
let attrs = parse_outer_attributes(p);
285286
let flo = p.span.lo;
286287
expect_word(p, "fn");
287288
let ident = parse_method_name(p);
@@ -290,7 +291,7 @@ fn parse_ty_methods(p: parser) -> [ast::ty_method] {
290291
expect(p, token::SEMI);
291292
alt f {
292293
ast::ty_fn(_, d) {
293-
{ident: ident, decl: d, tps: tps,
294+
{ident: ident, attrs: attrs, decl: d, tps: tps,
294295
span: ast_util::mk_sp(flo, fhi)}
295296
}
296297
}
@@ -1849,13 +1850,15 @@ fn parse_method_name(p: parser) -> ast::ident {
18491850
}
18501851

18511852
fn parse_method(p: parser) -> @ast::method {
1853+
let attrs = parse_outer_attributes(p);
18521854
let lo = p.span.lo;
18531855
expect_word(p, "fn");
18541856
let ident = parse_method_name(p);
18551857
let tps = parse_ty_params(p);
18561858
let decl = parse_fn_decl(p, ast::impure_fn);
1857-
let body = parse_block(p);
1858-
@{ident: ident, tps: tps, decl: decl, body: body,
1859+
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
1860+
let attrs = attrs + inner_attrs;
1861+
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
18591862
id: p.get_id(), span: ast_util::mk_sp(lo, body.span.hi)}
18601863
}
18611864

src/comp/syntax/print/pprust.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,16 +510,18 @@ fn print_item(s: ps, &&item: @ast::item) {
510510
for meth in methods {
511511
hardbreak_if_not_bol(s);
512512
maybe_print_comment(s, meth.span.lo);
513+
print_outer_attributes(s, meth.attrs);
513514
print_fn(s, meth.decl, meth.ident, meth.tps);
514515
word(s.s, " ");
515-
print_block(s, meth.body);
516+
print_block_with_attrs(s, meth.body, meth.attrs);
516517
}
517518
bclose(s, item.span);
518519
}
519520
ast::item_iface(tps, methods) {
520521
head(s, "iface");
521522
word(s.s, item.ident);
522523
print_type_params(s, tps);
524+
word(s.s, " ");
523525
bopen(s);
524526
for meth in methods { print_ty_method(s, meth); }
525527
bclose(s, item.span);
@@ -566,11 +568,10 @@ fn print_variant(s: ps, v: ast::variant) {
566568

567569
fn print_ty_method(s: ps, m: ast::ty_method) {
568570
hardbreak_if_not_bol(s);
569-
cbox(s, indent_unit);
570571
maybe_print_comment(s, m.span.lo);
572+
print_outer_attributes(s, m.attrs);
571573
print_ty_fn(s, none, m.decl, some(m.ident), some(m.tps));
572574
word(s.s, ";");
573-
end(s);
574575
}
575576

576577
fn print_outer_attributes(s: ps, attrs: [ast::attribute]) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// pp-exact - Make sure we print all the attributes
2+
3+
#[frobable]
4+
iface frobable {
5+
#[frob_attr]
6+
fn frob();
7+
#[defrob_attr]
8+
fn defrob();
9+
}
10+
11+
#[int_frobable]
12+
impl frobable for int {
13+
#[frob_attr1]
14+
fn frob() {
15+
#[frob_attr2];
16+
}
17+
18+
#[defrob_attr1]
19+
fn defrob() {
20+
#[defrob_attr2];
21+
}
22+
}
23+
24+
fn main() { }

0 commit comments

Comments
 (0)