]> git.proxmox.com Git - rustc.git/blame - src/test/ui/auxiliary/cond_plugin.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / test / ui / auxiliary / cond_plugin.rs
CommitLineData
a1dfa0c6 1// force-host
041b39d2 2// no-prefer-dynamic
9e0c209e 3
041b39d2 4#![crate_type = "proc-macro"]
e1599b0c 5#![feature(proc_macro_quote)]
9e0c209e 6
041b39d2 7extern crate proc_macro;
9e0c209e 8
83c7162d 9use proc_macro::*;
9e0c209e 10
041b39d2
XL
11#[proc_macro]
12pub fn cond(input: TokenStream) -> TokenStream {
32a655c1 13 let mut conds = Vec::new();
041b39d2 14 let mut input = input.into_iter().peekable();
32a655c1 15 while let Some(tree) = input.next() {
83c7162d
XL
16 let cond = match tree {
17 TokenTree::Group(tt) => tt.stream(),
32a655c1
SL
18 _ => panic!("Invalid input"),
19 };
041b39d2
XL
20 let mut cond_trees = cond.clone().into_iter();
21 let test = cond_trees.next().expect("Unexpected empty condition in `cond!`");
22 let rhs = cond_trees.collect::<TokenStream>();
32a655c1
SL
23 if rhs.is_empty() {
24 panic!("Invalid macro usage in cond: {}", cond);
25 }
83c7162d 26 let is_else = match test {
94b46f34 27 TokenTree::Ident(ref word) => &*word.to_string() == "else",
32a655c1
SL
28 _ => false,
29 };
30 conds.push(if is_else || input.peek().is_none() {
cc61c64b 31 quote!({ $rhs })
32a655c1 32 } else {
cc61c64b 33 quote!(if $test { $rhs } else)
32a655c1
SL
34 });
35 }
36
83c7162d 37 conds.into_iter().flat_map(|x| x.into_iter()).collect()
9e0c209e 38}