2 use rustc_ast::{ptr::P, tokenstream::TokenStream};
3 use rustc_data_structures::sync::Lrc;
4 use rustc_errors::Applicability;
5 use rustc_expand::base::{self, DummyResult};
7 /// Emits errors for literal expressions that are invalid inside and outside of an array.
8 fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_nested: bool) {
9 let ast::ExprKind::Lit(lit) = &expr.kind else {
13 ast::LitKind::Char(_) => {
14 let mut err = cx.struct_span_err(expr.span, "cannot concatenate character literals");
15 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) {
18 "try using a byte character",
19 format!("b{}", snippet),
20 Applicability::MachineApplicable,
25 ast::LitKind::Str(_, _) => {
26 let mut err = cx.struct_span_err(expr.span, "cannot concatenate string literals");
27 // suggestion would be invalid if we are nested
29 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) {
32 "try using a byte string",
33 format!("b{}", snippet),
34 Applicability::MachineApplicable,
40 ast::LitKind::Float(_, _) => {
41 cx.span_err(expr.span, "cannot concatenate float literals");
43 ast::LitKind::Bool(_) => {
44 cx.span_err(expr.span, "cannot concatenate boolean literals");
46 ast::LitKind::Err(_) => {}
47 ast::LitKind::Int(_, _) if !is_nested => {
48 let mut err = cx.struct_span_err(expr.span, "cannot concatenate numeric literals");
49 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) {
52 "try wrapping the number in an array",
53 format!("[{}]", snippet),
54 Applicability::MachineApplicable,
61 ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
63 assert!(val > u8::MAX.into()); // must be an error
64 cx.span_err(expr.span, "numeric literal is out of bounds");
66 ast::LitKind::Int(_, _) => {
67 cx.span_err(expr.span, "numeric literal is not a `u8`");
73 fn handle_array_element(
74 cx: &mut base::ExtCtxt<'_>,
75 has_errors: &mut bool,
76 missing_literals: &mut Vec<rustc_span::Span>,
77 expr: &P<rustc_ast::Expr>,
80 ast::ExprKind::Array(_) | ast::ExprKind::Repeat(_, _) => {
82 cx.span_err(expr.span, "cannot concatenate doubly nested array");
87 ast::ExprKind::Lit(ref lit) => match lit.kind {
90 ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
91 ) if val <= u8::MAX.into() => Some(val as u8),
93 ast::LitKind::Byte(val) => Some(val),
94 ast::LitKind::ByteStr(_) => {
96 cx.struct_span_err(expr.span, "cannot concatenate doubly nested array")
97 .note("byte strings are treated as arrays of bytes")
98 .help("try flattening the array")
106 invalid_type_err(cx, expr, true);
113 missing_literals.push(expr.span);
119 pub fn expand_concat_bytes(
120 cx: &mut base::ExtCtxt<'_>,
121 sp: rustc_span::Span,
123 ) -> Box<dyn base::MacResult + 'static> {
124 let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
125 return DummyResult::any(sp);
127 let mut accumulator = Vec::new();
128 let mut missing_literals = vec![];
129 let mut has_errors = false;
132 ast::ExprKind::Array(ref exprs) => {
135 handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
137 accumulator.push(elem);
141 ast::ExprKind::Repeat(ref expr, ref count) => {
142 if let ast::ExprKind::Lit(ast::Lit {
143 kind: ast::LitKind::Int(count_val, _), ..
144 }) = count.value.kind
147 handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
149 for _ in 0..count_val {
150 accumulator.push(elem);
154 cx.span_err(count.value.span, "repeat count is not a positive number");
157 ast::ExprKind::Lit(ref lit) => match lit.kind {
158 ast::LitKind::Byte(val) => {
159 accumulator.push(val);
161 ast::LitKind::ByteStr(ref bytes) => {
162 accumulator.extend_from_slice(&bytes);
166 invalid_type_err(cx, &e, false);
171 ast::ExprKind::Err => {
175 missing_literals.push(e.span);
179 if !missing_literals.is_empty() {
180 let mut err = cx.struct_span_err(missing_literals.clone(), "expected a byte literal");
181 err.note("only byte literals (like `b\"foo\"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`");
183 return base::MacEager::expr(DummyResult::raw_expr(sp, true));
184 } else if has_errors {
185 return base::MacEager::expr(DummyResult::raw_expr(sp, true));
187 let sp = cx.with_def_site_ctxt(sp);
188 base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(accumulator))))