Skip to content

Commit 4e0822e

Browse files
committed
syntax: allow macros to expand to items with attributes.
Fixes #4471.
1 parent 3a70df1 commit 4e0822e

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use ext::tt::macro_parser::{named_match, matched_seq, matched_nonterminal};
2020
use ext::tt::macro_parser::{parse, parse_or_else, success, failure};
2121
use parse::lexer::{new_tt_reader, reader};
2222
use parse::parser::Parser;
23+
use parse::attr::parser_attr;
2324
use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str};
2425
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF};
2526
use print;
@@ -54,12 +55,14 @@ impl AnyMacro for ParserAnyMacro {
5455
ret
5556
}
5657
fn make_item(&self) -> Option<@ast::item> {
57-
let ret = self.parser.parse_item(~[]); // no attrs
58+
let attrs = self.parser.parse_outer_attributes();
59+
let ret = self.parser.parse_item(attrs);
5860
self.ensure_complete_parse(false);
5961
ret
6062
}
6163
fn make_stmt(&self) -> @ast::Stmt {
62-
let ret = self.parser.parse_stmt(~[]); // no attrs
64+
let attrs = self.parser.parse_outer_attributes();
65+
let ret = self.parser.parse_stmt(attrs);
6366
self.ensure_complete_parse(true);
6467
ret
6568
}

src/test/run-pass/macro-attributes.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty - token trees can't pretty print
12+
13+
#[feature(macro_rules)];
14+
15+
macro_rules! compiles_fine {
16+
($at:attr) => {
17+
// test that the different types of attributes work
18+
#[attribute]
19+
/// Documentation!
20+
$at
21+
22+
// check that the attributes are recognised by requiring this
23+
// to be removed to avoid a compile error
24+
#[cfg(always_remove)]
25+
static MISTYPED: () = "foo";
26+
}
27+
}
28+
29+
// item
30+
compiles_fine!(#[foo])
31+
32+
pub fn main() {
33+
// statement
34+
compiles_fine!(#[bar]);
35+
}

0 commit comments

Comments
 (0)