Skip to content

Commit 2999479

Browse files
committed
rustc: Allow attributes on enum variants. Closes #1663
1 parent 08c6cb5 commit 2999479

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

src/comp/syntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ type native_mod =
418418

419419
type variant_arg = {ty: @ty, id: node_id};
420420

421-
type variant_ = {name: ident, args: [variant_arg], id: node_id,
422-
disr_expr: option::t<@expr>};
421+
type variant_ = {name: ident, attrs: [attribute], args: [variant_arg],
422+
id: node_id, disr_expr: option::t<@expr>};
423423

424424
type variant = spanned<variant_>;
425425

src/comp/syntax/fold.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,18 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
441441
}
442442
let fold_variant_arg = bind fold_variant_arg_(_, fld);
443443
let args = vec::map(v.args, fold_variant_arg);
444+
445+
let fold_meta_item = bind fold_meta_item_(_, fld);
446+
let fold_attribute = bind fold_attribute_(_, fold_meta_item);
447+
let attrs = vec::map(v.attrs, fold_attribute);
448+
444449
let de = alt v.disr_expr {
445450
some(e) {some(fld.fold_expr(e))}
446451
none {none}
447452
};
448-
ret {name: v.name, args: args, id: v.id,
453+
ret {name: v.name,
454+
attrs: attrs,
455+
args: args, id: v.id,
449456
disr_expr: de};
450457
}
451458

src/comp/syntax/parse/parser.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,16 +2042,19 @@ fn parse_item_enum(p: parser, attrs: [ast::attribute]) -> @ast::item {
20422042
let variant =
20432043
spanned(ty.span.lo, ty.span.hi,
20442044
{name: id,
2045+
attrs: [],
20452046
args: [{ty: ty, id: p.get_id()}],
20462047
id: p.get_id(),
20472048
disr_expr: none});
20482049
ret mk_item(p, lo, ty.span.hi, id,
20492050
ast::item_enum([variant], ty_params), attrs);
20502051
}
20512052
expect(p, token::LBRACE);
2053+
20522054
let all_nullary = true, have_disr = false;
20532055

20542056
while p.token != token::RBRACE {
2057+
let variant_attrs = parse_outer_attributes(p);
20552058
let vlo = p.span.lo;
20562059
let ident = parse_value_ident(p);
20572060
let args = [], disr_expr = none;
@@ -2068,7 +2071,8 @@ fn parse_item_enum(p: parser, attrs: [ast::attribute]) -> @ast::item {
20682071
disr_expr = some(parse_expr(p));
20692072
}
20702073

2071-
let vr = {name: ident, args: args, id: p.get_id(),
2074+
let vr = {name: ident, attrs: variant_attrs,
2075+
args: args, id: p.get_id(),
20722076
disr_expr: disr_expr};
20732077
variants += [spanned(vlo, p.last_span.hi, vr)];
20742078

src/comp/syntax/print/pprust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ fn print_item(s: ps, &&item: @ast::item) {
434434
for v: ast::variant in variants {
435435
space_if_not_bol(s);
436436
maybe_print_comment(s, v.span.lo);
437+
print_outer_attributes(s, v.node.attrs);
437438
ibox(s, indent_unit);
438439
word(s.s, v.node.name);
439440
if vec::len(v.node.args) > 0u {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// pp-exact - Make sure we actually print the attributes
2+
3+
enum crew_of_enterprise_d {
4+
5+
#[captain]
6+
jean_luc_picard,
7+
8+
#[commander]
9+
william_t_riker,
10+
11+
#[chief_medical_officer]
12+
beverly_crusher,
13+
14+
#[ships_councellor]
15+
deanna_troi,
16+
17+
#[lieutenant_commander]
18+
data,
19+
20+
#[chief_of_security]
21+
worf,
22+
23+
#[chief_engineer]
24+
geordi_la_forge,
25+
}
26+
27+
fn boldly_go(_crew_member: crew_of_enterprise_d, _where: str) { }
28+
29+
fn main() { boldly_go(worf, "where no one has gone before"); }

0 commit comments

Comments
 (0)