3 use syntax::ext::build::AstBuilder;
4 use syntax::symbol::Symbol;
5 use syntax::tokenstream;
7 use std::string::String;
9 pub fn expand_syntax_ext(
10 cx: &mut base::ExtCtxt<'_>,
12 tts: &[tokenstream::TokenTree],
13 ) -> Box<dyn base::MacResult + 'static> {
14 let es = match base::get_exprs_from_tts(cx, sp, tts) {
16 None => return base::DummyResult::expr(sp),
18 let mut accumulator = String::new();
19 let mut missing_literal = vec![];
20 let mut has_errors = false;
23 ast::ExprKind::Lit(ref lit) => match lit.node {
24 ast::LitKind::Str(ref s, _)
25 | ast::LitKind::Err(ref s)
26 | ast::LitKind::Float(ref s, _)
27 | ast::LitKind::FloatUnsuffixed(ref s) => {
28 accumulator.push_str(&s.as_str());
30 ast::LitKind::Char(c) => {
33 ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
34 | ast::LitKind::Int(i, ast::LitIntType::Signed(_))
35 | ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
36 accumulator.push_str(&i.to_string());
38 ast::LitKind::Bool(b) => {
39 accumulator.push_str(&b.to_string());
41 ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
42 cx.span_err(e.span, "cannot concatenate a byte string literal");
45 ast::ExprKind::Err => {
49 missing_literal.push(e.span);
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!()`");
57 return base::DummyResult::expr(sp);
58 } else if has_errors {
59 return base::DummyResult::expr(sp);
61 let sp = sp.apply_mark(cx.current_expansion.mark);
62 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))