rustc: Allow attributes on methods. Closes #1709
This commit is contained in:
parent
6ba3d24355
commit
0e498da47e
4 changed files with 38 additions and 8 deletions
|
@ -299,7 +299,8 @@ type ty_field_ = {ident: ident, mt: mt};
|
||||||
|
|
||||||
type ty_field = spanned<ty_field_>;
|
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, }
|
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
|
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};
|
id: node_id, span: span};
|
||||||
|
|
||||||
type _mod = {view_items: [@view_item], items: [@item]};
|
type _mod = {view_items: [@view_item], items: [@item]};
|
||||||
|
|
|
@ -282,6 +282,7 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
|
||||||
|
|
||||||
fn parse_ty_methods(p: parser) -> [ast::ty_method] {
|
fn parse_ty_methods(p: parser) -> [ast::ty_method] {
|
||||||
parse_seq(token::LBRACE, token::RBRACE, seq_sep_none(), {|p|
|
parse_seq(token::LBRACE, token::RBRACE, seq_sep_none(), {|p|
|
||||||
|
let attrs = parse_outer_attributes(p);
|
||||||
let flo = p.span.lo;
|
let flo = p.span.lo;
|
||||||
expect_word(p, "fn");
|
expect_word(p, "fn");
|
||||||
let ident = parse_method_name(p);
|
let ident = parse_method_name(p);
|
||||||
|
@ -290,7 +291,7 @@ fn parse_ty_methods(p: parser) -> [ast::ty_method] {
|
||||||
expect(p, token::SEMI);
|
expect(p, token::SEMI);
|
||||||
alt f {
|
alt f {
|
||||||
ast::ty_fn(_, d) {
|
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)}
|
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 {
|
fn parse_method(p: parser) -> @ast::method {
|
||||||
|
let attrs = parse_outer_attributes(p);
|
||||||
let lo = p.span.lo;
|
let lo = p.span.lo;
|
||||||
expect_word(p, "fn");
|
expect_word(p, "fn");
|
||||||
let ident = parse_method_name(p);
|
let ident = parse_method_name(p);
|
||||||
let tps = parse_ty_params(p);
|
let tps = parse_ty_params(p);
|
||||||
let decl = parse_fn_decl(p, ast::impure_fn);
|
let decl = parse_fn_decl(p, ast::impure_fn);
|
||||||
let body = parse_block(p);
|
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
|
||||||
@{ident: ident, tps: tps, decl: decl, body: body,
|
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)}
|
id: p.get_id(), span: ast_util::mk_sp(lo, body.span.hi)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -510,9 +510,10 @@ fn print_item(s: ps, &&item: @ast::item) {
|
||||||
for meth in methods {
|
for meth in methods {
|
||||||
hardbreak_if_not_bol(s);
|
hardbreak_if_not_bol(s);
|
||||||
maybe_print_comment(s, meth.span.lo);
|
maybe_print_comment(s, meth.span.lo);
|
||||||
|
print_outer_attributes(s, meth.attrs);
|
||||||
print_fn(s, meth.decl, meth.ident, meth.tps);
|
print_fn(s, meth.decl, meth.ident, meth.tps);
|
||||||
word(s.s, " ");
|
word(s.s, " ");
|
||||||
print_block(s, meth.body);
|
print_block_with_attrs(s, meth.body, meth.attrs);
|
||||||
}
|
}
|
||||||
bclose(s, item.span);
|
bclose(s, item.span);
|
||||||
}
|
}
|
||||||
|
@ -520,6 +521,7 @@ fn print_item(s: ps, &&item: @ast::item) {
|
||||||
head(s, "iface");
|
head(s, "iface");
|
||||||
word(s.s, item.ident);
|
word(s.s, item.ident);
|
||||||
print_type_params(s, tps);
|
print_type_params(s, tps);
|
||||||
|
word(s.s, " ");
|
||||||
bopen(s);
|
bopen(s);
|
||||||
for meth in methods { print_ty_method(s, meth); }
|
for meth in methods { print_ty_method(s, meth); }
|
||||||
bclose(s, item.span);
|
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) {
|
fn print_ty_method(s: ps, m: ast::ty_method) {
|
||||||
hardbreak_if_not_bol(s);
|
hardbreak_if_not_bol(s);
|
||||||
cbox(s, indent_unit);
|
|
||||||
maybe_print_comment(s, m.span.lo);
|
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));
|
print_ty_fn(s, none, m.decl, some(m.ident), some(m.tps));
|
||||||
word(s.s, ";");
|
word(s.s, ";");
|
||||||
end(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_outer_attributes(s: ps, attrs: [ast::attribute]) {
|
fn print_outer_attributes(s: ps, attrs: [ast::attribute]) {
|
||||||
|
|
24
src/test/run-pass/method-attributes.rs
Normal file
24
src/test/run-pass/method-attributes.rs
Normal 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() { }
|
Loading…
Add table
Add a link
Reference in a new issue