2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2024-02-25 21:25:26 +00:00
|
|
|
use rustc_ast::{ExprKind, LitKind, UnOp};
|
2024-03-12 02:55:17 +00:00
|
|
|
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
|
2022-10-10 02:40:56 +00:00
|
|
|
use rustc_session::errors::report_lit_error;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::Symbol;
|
2013-10-06 04:15:46 +00:00
|
|
|
|
2024-04-25 22:44:23 +00:00
|
|
|
use crate::errors;
|
|
|
|
use crate::util::get_exprs_from_tts;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn expand_concat(
|
2024-02-25 21:25:26 +00:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
2019-12-31 17:15:40 +00:00
|
|
|
sp: rustc_span::Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream,
|
2024-03-12 02:55:17 +00:00
|
|
|
) -> MacroExpanderResult<'static> {
|
|
|
|
let ExpandResult::Ready(mac) = get_exprs_from_tts(cx, tts) else {
|
|
|
|
return ExpandResult::Retry(());
|
|
|
|
};
|
|
|
|
let es = match mac {
|
2024-02-25 21:22:11 +00:00
|
|
|
Ok(es) => es,
|
2024-03-12 02:55:17 +00:00
|
|
|
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
|
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![];
|
2024-02-25 21:22:11 +00:00
|
|
|
let mut guar = None;
|
2015-02-01 01:03:04 +00:00
|
|
|
for e in es {
|
2019-09-26 13:39:48 +00:00
|
|
|
match e.kind {
|
2024-02-25 21:25:26 +00:00
|
|
|
ExprKind::Lit(token_lit) => match LitKind::from_token_lit(token_lit) {
|
|
|
|
Ok(LitKind::Str(s, _) | LitKind::Float(s, _)) => {
|
2021-12-15 03:39:23 +00:00
|
|
|
accumulator.push_str(s.as_str());
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(LitKind::Char(c)) => {
|
2018-07-11 00:50:21 +00:00
|
|
|
accumulator.push(c);
|
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(LitKind::Int(i, _)) => {
|
2018-07-27 09:11:18 +00:00
|
|
|
accumulator.push_str(&i.to_string());
|
2018-07-11 00:50:21 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(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
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(LitKind::CStr(..)) => {
|
2024-02-25 21:22:11 +00:00
|
|
|
guar = Some(cx.dcx().emit_err(errors::ConcatCStrLit { span: e.span }));
|
2023-03-05 15:03:22 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(LitKind::Byte(..) | LitKind::ByteStr(..)) => {
|
2024-02-25 21:22:11 +00:00
|
|
|
guar = Some(cx.dcx().emit_err(errors::ConcatBytestr { span: e.span }));
|
2022-10-10 02:40:56 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
Ok(LitKind::Err(guarantee)) => {
|
2024-02-25 21:22:11 +00:00
|
|
|
guar = Some(guarantee);
|
2018-07-11 00:50:21 +00:00
|
|
|
}
|
2022-10-10 02:40:56 +00:00
|
|
|
Err(err) => {
|
2024-03-04 05:31:49 +00:00
|
|
|
guar = Some(report_lit_error(&cx.sess.psess, err, token_lit, e.span));
|
2019-06-07 09:53:33 +00:00
|
|
|
}
|
2018-07-11 00:50:21 +00:00
|
|
|
},
|
2023-01-14 10:00:17 +00:00
|
|
|
// We also want to allow negative numeric literals.
|
2024-02-25 21:25:26 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref expr) if let ExprKind::Lit(token_lit) = expr.kind => {
|
|
|
|
match LitKind::from_token_lit(token_lit) {
|
|
|
|
Ok(LitKind::Int(i, _)) => accumulator.push_str(&format!("-{i}")),
|
|
|
|
Ok(LitKind::Float(f, _)) => accumulator.push_str(&format!("-{f}")),
|
2023-01-14 10:00:17 +00:00
|
|
|
Err(err) => {
|
2024-03-04 05:31:49 +00:00
|
|
|
guar = Some(report_lit_error(&cx.sess.psess, err, token_lit, e.span));
|
2023-01-14 10:00:17 +00:00
|
|
|
}
|
|
|
|
_ => missing_literal.push(e.span),
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
ExprKind::IncludedBytes(..) => {
|
2023-12-18 09:54:03 +00:00
|
|
|
cx.dcx().emit_err(errors::ConcatBytestr { span: e.span });
|
2022-10-31 18:30:09 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
ExprKind::Err(guarantee) => {
|
2024-02-25 21:22:11 +00:00
|
|
|
guar = Some(guarantee);
|
2018-12-29 21:56:55 +00:00
|
|
|
}
|
2024-02-25 21:25:26 +00:00
|
|
|
ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 10:00:17 +00:00
|
|
|
|
2024-03-12 02:55:17 +00:00
|
|
|
ExpandResult::Ready(if !missing_literal.is_empty() {
|
2024-02-25 21:22:11 +00:00
|
|
|
let guar = cx.dcx().emit_err(errors::ConcatMissingLiteral { spans: missing_literal });
|
2024-03-12 02:55:17 +00:00
|
|
|
DummyResult::any(sp, guar)
|
2024-02-25 21:22:11 +00:00
|
|
|
} else if let Some(guar) = guar {
|
2024-03-12 02:55:17 +00:00
|
|
|
DummyResult::any(sp, guar)
|
|
|
|
} else {
|
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
|
|
|
MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
|
|
|
|
})
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|