2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::{self, DummyResult};
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2013-10-06 04:15:46 +00:00
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
use std::string::String;
|
2014-04-02 23:54:22 +00:00
|
|
|
|
2019-09-03 14:43:25 +00:00
|
|
|
pub fn expand_concat(
|
2019-02-04 12:49:54 +00:00
|
|
|
cx: &mut base::ExtCtxt<'_>,
|
2019-12-31 17:15:40 +00:00
|
|
|
sp: rustc_span::Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream,
|
2018-07-12 09:58:16 +00:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let es = match base::get_exprs_from_tts(cx, sp, tts) {
|
|
|
|
Some(e) => e,
|
2019-08-13 17:51:54 +00:00
|
|
|
None => return DummyResult::any(sp),
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2014-05-22 23:57:53 +00:00
|
|
|
let mut accumulator = String::new();
|
2018-07-15 03:50:30 +00:00
|
|
|
let mut missing_literal = vec![];
|
2018-12-29 21:56:55 +00:00
|
|
|
let mut has_errors = false;
|
2015-02-01 01:03:04 +00:00
|
|
|
for e in es {
|
2019-09-26 13:39:48 +00:00
|
|
|
match e.kind {
|
2019-09-26 15:56:53 +00:00
|
|
|
ast::ExprKind::Lit(ref lit) => match lit.kind {
|
2019-12-22 22:42:04 +00:00
|
|
|
ast::LitKind::Str(ref s, _) | ast::LitKind::Float(ref s, _) => {
|
2018-07-11 00:50:21 +00:00
|
|
|
accumulator.push_str(&s.as_str());
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2018-07-11 00:50:21 +00:00
|
|
|
ast::LitKind::Char(c) => {
|
|
|
|
accumulator.push(c);
|
|
|
|
}
|
2020-04-17 00:38:52 +00:00
|
|
|
ast::LitKind::Int(
|
|
|
|
i,
|
|
|
|
ast::LitIntType::Unsigned(_)
|
|
|
|
| ast::LitIntType::Signed(_)
|
|
|
|
| ast::LitIntType::Unsuffixed,
|
|
|
|
) => {
|
2018-07-27 09:11:18 +00:00
|
|
|
accumulator.push_str(&i.to_string());
|
2018-07-11 00:50:21 +00:00
|
|
|
}
|
|
|
|
ast::LitKind::Bool(b) => {
|
2018-07-27 09:11:18 +00:00
|
|
|
accumulator.push_str(&b.to_string());
|
2018-07-11 00:50:21 +00:00
|
|
|
}
|
|
|
|
ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
|
|
|
|
cx.span_err(e.span, "cannot concatenate a byte string literal");
|
|
|
|
}
|
2019-06-07 09:53:33 +00:00
|
|
|
ast::LitKind::Err(_) => {
|
|
|
|
has_errors = true;
|
|
|
|
}
|
2018-07-11 00:50:21 +00:00
|
|
|
},
|
2018-12-29 21:56:55 +00:00
|
|
|
ast::ExprKind::Err => {
|
|
|
|
has_errors = true;
|
|
|
|
}
|
2013-10-06 04:15:46 +00:00
|
|
|
_ => {
|
2018-07-15 03:50:30 +00:00
|
|
|
missing_literal.push(e.span);
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 13:20:33 +00:00
|
|
|
if !missing_literal.is_empty() {
|
2018-07-15 03:50:30 +00:00
|
|
|
let mut err = cx.struct_span_err(missing_literal, "expected a literal");
|
2018-07-15 18:52:11 +00:00
|
|
|
err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
|
2018-07-15 03:50:30 +00:00
|
|
|
err.emit();
|
2019-08-13 17:51:54 +00:00
|
|
|
return DummyResult::any(sp);
|
2018-12-29 21:56:55 +00:00
|
|
|
} else if has_errors {
|
2019-08-13 17:51:54 +00:00
|
|
|
return DummyResult::any(sp);
|
2018-07-15 03:50:30 +00:00
|
|
|
}
|
2019-09-14 20:17:11 +00:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|