3 use syntax::symbol::Symbol;
4 use syntax::tokenstream;
6 use std::string::String;
8 pub fn expand_syntax_ext(
9 cx: &mut base::ExtCtxt<'_>,
11 tts: &[tokenstream::TokenTree],
12 ) -> Box<dyn base::MacResult + 'static> {
13 let es = match base::get_exprs_from_tts(cx, sp, tts) {
15 None => return base::DummyResult::expr(sp),
17 let mut accumulator = String::new();
18 let mut missing_literal = vec![];
19 let mut has_errors = false;
22 ast::ExprKind::Lit(ref lit) => match lit.node {
23 ast::LitKind::Str(ref s, _)
24 | ast::LitKind::Float(ref s, _)
25 | ast::LitKind::FloatUnsuffixed(ref s) => {
26 accumulator.push_str(&s.as_str());
28 ast::LitKind::Char(c) => {
31 ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
32 | ast::LitKind::Int(i, ast::LitIntType::Signed(_))
33 | ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
34 accumulator.push_str(&i.to_string());
36 ast::LitKind::Bool(b) => {
37 accumulator.push_str(&b.to_string());
39 ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
40 cx.span_err(e.span, "cannot concatenate a byte string literal");
42 ast::LitKind::Err(_) => {
46 ast::ExprKind::Err => {
50 missing_literal.push(e.span);
54 if missing_literal.len() > 0 {
55 let mut err = cx.struct_span_err(missing_literal, "expected a literal");
56 err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
58 return base::DummyResult::expr(sp);
59 } else if has_errors {
60 return base::DummyResult::expr(sp);
62 let sp = sp.apply_mark(cx.current_expansion.id);
63 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))