]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/auxiliary/macro_crate_test.rs
Rollup merge of #57278 - mati865:config_clippy, r=alexcrichton
[rust.git] / src / test / ui-fulldeps / auxiliary / macro_crate_test.rs
1 // force-host
2
3 #![feature(plugin_registrar, quote, rustc_private)]
4
5 extern crate syntax;
6 extern crate syntax_pos;
7 extern crate rustc;
8 extern crate rustc_plugin;
9
10 use syntax::ast::{self, Item, MetaItem, ItemKind};
11 use syntax::ext::base::*;
12 use syntax::parse;
13 use syntax::ptr::P;
14 use syntax::symbol::Symbol;
15 use syntax::tokenstream::TokenTree;
16 use syntax_pos::Span;
17 use rustc_plugin::Registry;
18
19 #[macro_export]
20 macro_rules! exported_macro { () => (2) }
21 macro_rules! unexported_macro { () => (3) }
22
23 #[plugin_registrar]
24 pub fn plugin_registrar(reg: &mut Registry) {
25     reg.register_macro("make_a_1", expand_make_a_1);
26     reg.register_macro("identity", expand_identity);
27     reg.register_syntax_extension(
28         Symbol::intern("into_multi_foo"),
29         MultiModifier(Box::new(expand_into_foo_multi)));
30     reg.register_syntax_extension(
31         Symbol::intern("noop_attribute"),
32         MultiModifier(Box::new(expand_noop_attribute)));
33     reg.register_syntax_extension(
34         Symbol::intern("duplicate"),
35         MultiDecorator(Box::new(expand_duplicate)));
36 }
37
38 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
39                    -> Box<MacResult+'static> {
40     if !tts.is_empty() {
41         cx.span_fatal(sp, "make_a_1 takes no arguments");
42     }
43     MacEager::expr(quote_expr!(cx, 1))
44 }
45
46 // See Issue #15750
47 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
48                    -> Box<MacResult+'static> {
49     // Parse an expression and emit it unchanged.
50     let mut parser = parse::new_parser_from_tts(cx.parse_sess(), tts.to_vec());
51     let expr = parser.parse_expr().unwrap();
52     MacEager::expr(quote_expr!(&mut *cx, $expr))
53 }
54
55 fn expand_into_foo_multi(cx: &mut ExtCtxt,
56                          _sp: Span,
57                          _attr: &MetaItem,
58                          it: Annotatable) -> Annotatable {
59     match it {
60         Annotatable::Item(it) => {
61             Annotatable::Item(P(Item {
62                 attrs: it.attrs.clone(),
63                 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
64             }))
65         }
66         Annotatable::ImplItem(_) => {
67             quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
68                 match i.node {
69                     ItemKind::Impl(.., mut items) => {
70                         Annotatable::ImplItem(P(items.pop().expect("impl method not found")))
71                     }
72                     _ => unreachable!("impl parsed to something other than impl")
73                 }
74             })
75         }
76         Annotatable::TraitItem(_) => {
77             quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
78                 match i.node {
79                     ItemKind::Trait(.., mut items) => {
80                         Annotatable::TraitItem(P(items.pop().expect("trait method not found")))
81                     }
82                     _ => unreachable!("trait parsed to something other than trait")
83                 }
84             })
85         }
86         // covered in proc_macro/macros-in-extern.rs
87         Annotatable::ForeignItem(_) => unimplemented!(),
88         // covered in proc_macro/attr-stmt-expr.rs
89         Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")
90     }
91 }
92
93 fn expand_noop_attribute(_cx: &mut ExtCtxt,
94                          _sp: Span,
95                          _attr: &MetaItem,
96                          it: Annotatable) -> Annotatable {
97     it
98 }
99
100 // Create a duplicate of the annotatable, based on the MetaItem
101 fn expand_duplicate(cx: &mut ExtCtxt,
102                     _sp: Span,
103                     mi: &MetaItem,
104                     it: &Annotatable,
105                     push: &mut FnMut(Annotatable))
106 {
107     let copy_name = match mi.node {
108         ast::MetaItemKind::List(ref xs) => {
109             if let Some(word) = xs[0].word() {
110                 word.ident.segments.last().unwrap().ident
111             } else {
112                 cx.span_err(mi.span, "Expected word");
113                 return;
114             }
115         }
116         _ => {
117             cx.span_err(mi.span, "Expected list");
118             return;
119         }
120     };
121
122     // Duplicate the item but replace its ident by the MetaItem
123     match it.clone() {
124         Annotatable::Item(it) => {
125             let mut new_it = (*it).clone();
126             new_it.attrs.clear();
127             new_it.ident = copy_name;
128             push(Annotatable::Item(P(new_it)));
129         }
130         Annotatable::ImplItem(it) => {
131             let mut new_it = (*it).clone();
132             new_it.attrs.clear();
133             new_it.ident = copy_name;
134             push(Annotatable::ImplItem(P(new_it)));
135         }
136         Annotatable::TraitItem(tt) => {
137             let mut new_it = (*tt).clone();
138             new_it.attrs.clear();
139             new_it.ident = copy_name;
140             push(Annotatable::TraitItem(P(new_it)));
141         }
142         // covered in proc_macro/macros-in-extern.rs
143         Annotatable::ForeignItem(_) => unimplemented!(),
144         // covered in proc_macro/attr-stmt-expr.rs
145         Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")
146     }
147 }
148
149 pub fn foo() {}