summaryrefslogtreecommitdiffstats
path: root/src/test/ui/proc-macro/auxiliary/duplicate.rs
blob: b8f82b46f094513d0b85ae1163f3b77663b601cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// force-host
// no-prefer-dynamic

#![deny(unused)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::*;

#[proc_macro_attribute]
pub fn duplicate(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut new_name = Some(attr.into_iter().nth(0).unwrap());
    let mut encountered_idents = 0;
    let input = item.to_string();
    let ret = item
        .into_iter()
        .map(move |token| match token {
            TokenTree::Ident(_) if encountered_idents == 1 => {
                encountered_idents += 1;
                new_name.take().unwrap()
            }
            TokenTree::Ident(_) => {
                encountered_idents += 1;
                token
            }
            _ => token,
        })
        .collect::<TokenStream>();
    let mut input_again = input.parse::<TokenStream>().unwrap();
    input_again.extend(ret);
    input_again
}