]>
Commit | Line | Data |
---|---|---|
3dfed10e | 1 | use rustc_ast as ast; |
74b04a01 | 2 | use rustc_ast::tokenstream::TokenStream; |
dfeec247 | 3 | use rustc_expand::base::{self, DummyResult}; |
487cf647 | 4 | use rustc_session::errors::report_lit_error; |
dfeec247 | 5 | use rustc_span::symbol::Symbol; |
1a4d82fc | 6 | |
e1599b0c | 7 | pub fn expand_concat( |
9fa01778 | 8 | cx: &mut base::ExtCtxt<'_>, |
dfeec247 | 9 | sp: rustc_span::Span, |
e1599b0c | 10 | tts: TokenStream, |
8faf50e0 | 11 | ) -> Box<dyn base::MacResult + 'static> { |
9c376795 | 12 | let Some(es) = base::get_exprs_from_tts(cx, tts) else { |
5e7ed085 | 13 | return DummyResult::any(sp); |
1a4d82fc JJ |
14 | }; |
15 | let mut accumulator = String::new(); | |
8faf50e0 | 16 | let mut missing_literal = vec![]; |
0731742a | 17 | let mut has_errors = false; |
85aaf69f | 18 | for e in es { |
e74abb32 | 19 | match e.kind { |
487cf647 FG |
20 | ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) { |
21 | Ok(ast::LitKind::Str(s, _) | ast::LitKind::Float(s, _)) => { | |
a2a8927a | 22 | accumulator.push_str(s.as_str()); |
1a4d82fc | 23 | } |
487cf647 | 24 | Ok(ast::LitKind::Char(c)) => { |
8faf50e0 XL |
25 | accumulator.push(c); |
26 | } | |
487cf647 | 27 | Ok(ast::LitKind::Int(i, _)) => { |
8faf50e0 XL |
28 | accumulator.push_str(&i.to_string()); |
29 | } | |
487cf647 | 30 | Ok(ast::LitKind::Bool(b)) => { |
8faf50e0 XL |
31 | accumulator.push_str(&b.to_string()); |
32 | } | |
487cf647 | 33 | Ok(ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..)) => { |
8faf50e0 | 34 | cx.span_err(e.span, "cannot concatenate a byte string literal"); |
487cf647 FG |
35 | has_errors = true; |
36 | } | |
37 | Ok(ast::LitKind::Err) => { | |
38 | has_errors = true; | |
8faf50e0 | 39 | } |
487cf647 FG |
40 | Err(err) => { |
41 | report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span); | |
dc9dc135 XL |
42 | has_errors = true; |
43 | } | |
8faf50e0 | 44 | }, |
487cf647 FG |
45 | ast::ExprKind::IncludedBytes(..) => { |
46 | cx.span_err(e.span, "cannot concatenate a byte string literal") | |
47 | } | |
0731742a XL |
48 | ast::ExprKind::Err => { |
49 | has_errors = true; | |
50 | } | |
1a4d82fc | 51 | _ => { |
8faf50e0 | 52 | missing_literal.push(e.span); |
1a4d82fc JJ |
53 | } |
54 | } | |
55 | } | |
74b04a01 | 56 | if !missing_literal.is_empty() { |
8faf50e0 XL |
57 | let mut err = cx.struct_span_err(missing_literal, "expected a literal"); |
58 | err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`"); | |
59 | err.emit(); | |
e1599b0c | 60 | return DummyResult::any(sp); |
0731742a | 61 | } else if has_errors { |
e1599b0c | 62 | return DummyResult::any(sp); |
8faf50e0 | 63 | } |
e1599b0c | 64 | let sp = cx.with_def_site_ctxt(sp); |
476ff2be | 65 | base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator))) |
1a4d82fc | 66 | } |