From 4e0822e2538bbff9d53c151adaf37ec1fe29de9e Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 9 Oct 2013 23:13:43 +1100 Subject: [PATCH] syntax: allow macros to expand to items with attributes. Fixes #4471. --- src/libsyntax/ext/tt/macro_rules.rs | 7 ++++-- src/test/run-pass/macro-attributes.rs | 35 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/test/run-pass/macro-attributes.rs diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index de0afe23668..343116df642 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -20,6 +20,7 @@ use ext::tt::macro_parser::{named_match, matched_seq, matched_nonterminal}; use ext::tt::macro_parser::{parse, parse_or_else, success, failure}; use parse::lexer::{new_tt_reader, reader}; use parse::parser::Parser; +use parse::attr::parser_attr; use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str}; use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF}; use print; @@ -54,12 +55,14 @@ impl AnyMacro for ParserAnyMacro { ret } fn make_item(&self) -> Option<@ast::item> { - let ret = self.parser.parse_item(~[]); // no attrs + let attrs = self.parser.parse_outer_attributes(); + let ret = self.parser.parse_item(attrs); self.ensure_complete_parse(false); ret } fn make_stmt(&self) -> @ast::Stmt { - let ret = self.parser.parse_stmt(~[]); // no attrs + let attrs = self.parser.parse_outer_attributes(); + let ret = self.parser.parse_stmt(attrs); self.ensure_complete_parse(true); ret } diff --git a/src/test/run-pass/macro-attributes.rs b/src/test/run-pass/macro-attributes.rs new file mode 100644 index 00000000000..7cbc8ef98c9 --- /dev/null +++ b/src/test/run-pass/macro-attributes.rs @@ -0,0 +1,35 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// xfail-pretty - token trees can't pretty print + +#[feature(macro_rules)]; + +macro_rules! compiles_fine { + ($at:attr) => { + // test that the different types of attributes work + #[attribute] + /// Documentation! + $at + + // check that the attributes are recognised by requiring this + // to be removed to avoid a compile error + #[cfg(always_remove)] + static MISTYPED: () = "foo"; + } +} + +// item +compiles_fine!(#[foo]) + +pub fn main() { + // statement + compiles_fine!(#[bar]); +}