rustc: Allow attributes on methods. Closes #1709

This commit is contained in:
Brian Anderson 2012-01-30 11:43:45 -08:00
parent 6ba3d24355
commit 0e498da47e
4 changed files with 38 additions and 8 deletions

View File

@ -299,7 +299,8 @@ type ty_field_ = {ident: ident, mt: mt};
type ty_field = spanned<ty_field_>;
type ty_method = {ident: ident, decl: fn_decl, tps: [ty_param], span: span};
type ty_method = {ident: ident, attrs: [attribute],
decl: fn_decl, tps: [ty_param], span: span};
enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
@ -399,7 +400,8 @@ enum ret_style {
return_val, // everything else
}
type method = {ident: ident, tps: [ty_param], decl: fn_decl, body: blk,
type method = {ident: ident, attrs: [attribute],
tps: [ty_param], decl: fn_decl, body: blk,
id: node_id, span: span};
type _mod = {view_items: [@view_item], items: [@item]};

View File

@ -282,6 +282,7 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
fn parse_ty_methods(p: parser) -> [ast::ty_method] {
parse_seq(token::LBRACE, token::RBRACE, seq_sep_none(), {|p|
let attrs = parse_outer_attributes(p);
let flo = p.span.lo;
expect_word(p, "fn");
let ident = parse_method_name(p);
@ -290,7 +291,7 @@ fn parse_ty_methods(p: parser) -> [ast::ty_method] {
expect(p, token::SEMI);
alt f {
ast::ty_fn(_, d) {
{ident: ident, decl: d, tps: tps,
{ident: ident, attrs: attrs, decl: d, tps: tps,
span: ast_util::mk_sp(flo, fhi)}
}
}
@ -1849,13 +1850,15 @@ fn parse_method_name(p: parser) -> ast::ident {
}
fn parse_method(p: parser) -> @ast::method {
let attrs = parse_outer_attributes(p);
let lo = p.span.lo;
expect_word(p, "fn");
let ident = parse_method_name(p);
let tps = parse_ty_params(p);
let decl = parse_fn_decl(p, ast::impure_fn);
let body = parse_block(p);
@{ident: ident, tps: tps, decl: decl, body: body,
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
let attrs = attrs + inner_attrs;
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
id: p.get_id(), span: ast_util::mk_sp(lo, body.span.hi)}
}

View File

@ -510,9 +510,10 @@ fn print_item(s: ps, &&item: @ast::item) {
for meth in methods {
hardbreak_if_not_bol(s);
maybe_print_comment(s, meth.span.lo);
print_outer_attributes(s, meth.attrs);
print_fn(s, meth.decl, meth.ident, meth.tps);
word(s.s, " ");
print_block(s, meth.body);
print_block_with_attrs(s, meth.body, meth.attrs);
}
bclose(s, item.span);
}
@ -520,6 +521,7 @@ fn print_item(s: ps, &&item: @ast::item) {
head(s, "iface");
word(s.s, item.ident);
print_type_params(s, tps);
word(s.s, " ");
bopen(s);
for meth in methods { print_ty_method(s, meth); }
bclose(s, item.span);
@ -566,11 +568,10 @@ fn print_variant(s: ps, v: ast::variant) {
fn print_ty_method(s: ps, m: ast::ty_method) {
hardbreak_if_not_bol(s);
cbox(s, indent_unit);
maybe_print_comment(s, m.span.lo);
print_outer_attributes(s, m.attrs);
print_ty_fn(s, none, m.decl, some(m.ident), some(m.tps));
word(s.s, ";");
end(s);
}
fn print_outer_attributes(s: ps, attrs: [ast::attribute]) {

View File

@ -0,0 +1,24 @@
// pp-exact - Make sure we print all the attributes
#[frobable]
iface frobable {
#[frob_attr]
fn frob();
#[defrob_attr]
fn defrob();
}
#[int_frobable]
impl frobable for int {
#[frob_attr1]
fn frob() {
#[frob_attr2];
}
#[defrob_attr1]
fn defrob() {
#[defrob_attr2];
}
}
fn main() { }