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