]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_builtin_macros/src/concat.rs
Update upstream source from tag 'upstream/1.59.0+dfsg1'
[rustc.git] / compiler / rustc_builtin_macros / src / concat.rs
1 use rustc_ast as ast;
2 use rustc_ast::tokenstream::TokenStream;
3 use rustc_expand::base::{self, DummyResult};
4 use rustc_span::symbol::Symbol;
5
6 use std::string::String;
7
8 pub fn expand_concat(
9 cx: &mut base::ExtCtxt<'_>,
10 sp: rustc_span::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, _) | ast::LitKind::Float(ref s, _) => {
24 accumulator.push_str(s.as_str());
25 }
26 ast::LitKind::Char(c) => {
27 accumulator.push(c);
28 }
29 ast::LitKind::Int(
30 i,
31 ast::LitIntType::Unsigned(_)
32 | ast::LitIntType::Signed(_)
33 | ast::LitIntType::Unsuffixed,
34 ) => {
35 accumulator.push_str(&i.to_string());
36 }
37 ast::LitKind::Bool(b) => {
38 accumulator.push_str(&b.to_string());
39 }
40 ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
41 cx.span_err(e.span, "cannot concatenate a byte string literal");
42 }
43 ast::LitKind::Err(_) => {
44 has_errors = true;
45 }
46 },
47 ast::ExprKind::Err => {
48 has_errors = true;
49 }
50 _ => {
51 missing_literal.push(e.span);
52 }
53 }
54 }
55 if !missing_literal.is_empty() {
56 let mut err = cx.struct_span_err(missing_literal, "expected a literal");
57 err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
58 err.emit();
59 return DummyResult::any(sp);
60 } else if has_errors {
61 return DummyResult::any(sp);
62 }
63 let sp = cx.with_def_site_ctxt(sp);
64 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
65 }