summaryrefslogtreecommitdiffstats
path: root/tests/ui/proc-macro/auxiliary/custom-quote.rs
blob: 3b7811748edfac37f58939cebc8c78a79a379705 (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
// ignore-tidy-linelength

#![feature(proc_macro_quote)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
use std::iter::FromIterator;
use std::str::FromStr;
use proc_macro::*;

#[proc_macro]
pub fn custom_quote(input: TokenStream) -> TokenStream {
    let mut tokens: Vec<_> = input.into_iter().collect();
    assert_eq!(tokens.len(), 1, "Unexpected input: {:?}", tokens);
    match tokens.pop() {
        Some(TokenTree::Ident(ident)) => {
            assert_eq!(ident.to_string(), "my_ident");

            let proc_macro_crate = TokenStream::from_str("::proc_macro").unwrap();
            let quoted_span = proc_macro::quote_span(proc_macro_crate, ident.span());
            let prefix = TokenStream::from_str(r#"let mut ident = proc_macro::Ident::new("my_ident", proc_macro::Span::call_site());"#).unwrap();
            let set_span_method = TokenStream::from_str("ident.set_span").unwrap();
            let set_span_arg = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, quoted_span)));
            let suffix = TokenStream::from_str(";proc_macro::TokenStream::from(proc_macro::TokenTree::Ident(ident))").unwrap();
            let full_stream = TokenStream::from_iter([prefix, set_span_method, set_span_arg, suffix]);
            full_stream
        }
        _ => unreachable!()
    }
}