]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/macro_crate_test.rs
Rollup merge of #28991 - goyox86:goyox86/rustfmting-liblog-II, r=alexcrichton
[rust.git] / src / test / auxiliary / macro_crate_test.rs
1 // Copyright 2013-2015 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 // force-host
12
13 #![feature(plugin_registrar, quote, rustc_private)]
14
15 extern crate syntax;
16 extern crate rustc;
17
18 use syntax::ast::{self, TokenTree, Item, MetaItem, ImplItem, TraitItem};
19 use syntax::codemap::Span;
20 use syntax::ext::base::*;
21 use syntax::parse::{self, token};
22 use syntax::ptr::P;
23 use rustc::plugin::Registry;
24
25 #[macro_export]
26 macro_rules! exported_macro { () => (2) }
27 macro_rules! unexported_macro { () => (3) }
28
29 #[plugin_registrar]
30 pub fn plugin_registrar(reg: &mut Registry) {
31     reg.register_macro("make_a_1", expand_make_a_1);
32     reg.register_macro("identity", expand_identity);
33     reg.register_syntax_extension(
34         token::intern("into_multi_foo"),
35         // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
36         MultiModifier(Box::new(expand_into_foo_multi)));
37     reg.register_syntax_extension(
38         token::intern("duplicate"),
39         // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
40         MultiDecorator(Box::new(expand_duplicate)));
41 }
42
43 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
44                    -> Box<MacResult+'static> {
45     if !tts.is_empty() {
46         cx.span_fatal(sp, "make_a_1 takes no arguments");
47     }
48     MacEager::expr(quote_expr!(cx, 1))
49 }
50
51 // See Issue #15750
52 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
53                    -> Box<MacResult+'static> {
54     // Parse an expression and emit it unchanged.
55     let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
56         cx.cfg(), tts.to_vec());
57     let expr = parser.parse_expr();
58     MacEager::expr(quote_expr!(&mut *cx, $expr))
59 }
60
61 fn expand_into_foo_multi(cx: &mut ExtCtxt,
62                          sp: Span,
63                          attr: &MetaItem,
64                          it: Annotatable) -> Annotatable {
65     match it {
66         Annotatable::Item(it) => {
67             Annotatable::Item(P(Item {
68                 attrs: it.attrs.clone(),
69                 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
70             }))
71         }
72         Annotatable::ImplItem(it) => {
73             quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
74                 match i.node {
75                     ast::ItemImpl(_, _, _, _, _, mut items) => {
76                         Annotatable::ImplItem(items.pop().expect("impl method not found"))
77                     }
78                     _ => unreachable!("impl parsed to something other than impl")
79                 }
80             })
81         }
82         Annotatable::TraitItem(it) => {
83             quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
84                 match i.node {
85                     ast::ItemTrait(_, _, _, mut items) => {
86                         Annotatable::TraitItem(items.pop().expect("trait method not found"))
87                     }
88                     _ => unreachable!("trait parsed to something other than trait")
89                 }
90             })
91         }
92     }
93 }
94
95 // Create a duplicate of the annotatable, based on the MetaItem
96 fn expand_duplicate(cx: &mut ExtCtxt,
97                     sp: Span,
98                     mi: &MetaItem,
99                     it: &Annotatable,
100                     push: &mut FnMut(Annotatable))
101 {
102     let copy_name = match mi.node {
103         ast::MetaItem_::MetaList(_, ref xs) => {
104             if let ast::MetaItem_::MetaWord(ref w) = xs[0].node {
105                 token::str_to_ident(&w)
106             } else {
107                 cx.span_err(mi.span, "Expected word");
108                 return;
109             }
110         }
111         _ => {
112             cx.span_err(mi.span, "Expected list");
113             return;
114         }
115     };
116
117     // Duplicate the item but replace its ident by the MetaItem
118     match it.clone() {
119         Annotatable::Item(it) => {
120             let mut new_it = (*it).clone();
121             new_it.attrs.clear();
122             new_it.ident = copy_name;
123             push(Annotatable::Item(P(new_it)));
124         }
125         Annotatable::ImplItem(it) => {
126             let mut new_it = (*it).clone();
127             new_it.attrs.clear();
128             new_it.ident = copy_name;
129             push(Annotatable::ImplItem(P(new_it)));
130         }
131         Annotatable::TraitItem(tt) => {
132             let mut new_it = (*tt).clone();
133             new_it.attrs.clear();
134             new_it.ident = copy_name;
135             push(Annotatable::TraitItem(P(new_it)));
136         }
137     }
138 }
139
140 pub fn foo() {}