]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/concat.rs
resolve: Remove some bits relevant only to legacy plugins
[rust.git] / src / libsyntax_ext / concat.rs
1 use syntax::ast;
2 use syntax_expand::base::{self, DummyResult};
3 use syntax::symbol::Symbol;
4 use syntax::tokenstream::TokenStream;
5
6 use std::string::String;
7
8 pub fn expand_concat(
9     cx: &mut base::ExtCtxt<'_>,
10     sp: syntax_pos::Span,
11     tts: TokenStream,
12 ) -> Box<dyn base::MacResult + 'static> {
13     let es = match base::get_exprs_from_tts(cx, sp, tts) {
14         Some(e) => e,
15         None => return DummyResult::any(sp),
16     };
17     let mut accumulator = String::new();
18     let mut missing_literal = vec![];
19     let mut has_errors = false;
20     for e in es {
21         match e.kind {
22             ast::ExprKind::Lit(ref lit) => match lit.kind {
23                 ast::LitKind::Str(ref s, _)
24                 | ast::LitKind::Float(ref s, _) => {
25                     accumulator.push_str(&s.as_str());
26                 }
27                 ast::LitKind::Char(c) => {
28                     accumulator.push(c);
29                 }
30                 ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
31                 | ast::LitKind::Int(i, ast::LitIntType::Signed(_))
32                 | ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
33                     accumulator.push_str(&i.to_string());
34                 }
35                 ast::LitKind::Bool(b) => {
36                     accumulator.push_str(&b.to_string());
37                 }
38                 ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
39                     cx.span_err(e.span, "cannot concatenate a byte string literal");
40                 }
41                 ast::LitKind::Err(_) => {
42                     has_errors = true;
43                 }
44             },
45             ast::ExprKind::Err => {
46                 has_errors = true;
47             }
48             _ => {
49                 missing_literal.push(e.span);
50             }
51         }
52     }
53     if missing_literal.len() > 0 {
54         let mut err = cx.struct_span_err(missing_literal, "expected a literal");
55         err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
56         err.emit();
57         return DummyResult::any(sp);
58     } else if has_errors {
59         return DummyResult::any(sp);
60     }
61     let sp = cx.with_def_site_ctxt(sp);
62     base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
63 }