2 use rustc_ast::{ptr::P, tokenstream::TokenStream};
3 use rustc_errors::Applicability;
4 use rustc_expand::base::{self, DummyResult};
7 /// Emits errors for literal expressions that are invalid inside and outside of an array.
9 cx: &mut base::ExtCtxt<'_>,
10 token_lit: ast::token::Lit,
14 match ast::LitKind::from_token_lit(token_lit) {
15 Ok(ast::LitKind::Char(_)) => {
16 let mut err = cx.struct_span_err(span, "cannot concatenate character literals");
17 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(span) {
20 "try using a byte character",
21 format!("b{}", snippet),
22 Applicability::MachineApplicable,
27 Ok(ast::LitKind::Str(_, _)) => {
28 let mut err = cx.struct_span_err(span, "cannot concatenate string literals");
29 // suggestion would be invalid if we are nested
31 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(span) {
34 "try using a byte string",
35 format!("b{}", snippet),
36 Applicability::MachineApplicable,
42 Ok(ast::LitKind::Float(_, _)) => {
43 cx.span_err(span, "cannot concatenate float literals");
45 Ok(ast::LitKind::Bool(_)) => {
46 cx.span_err(span, "cannot concatenate boolean literals");
48 Ok(ast::LitKind::Err) => {}
49 Ok(ast::LitKind::Int(_, _)) if !is_nested => {
50 let mut err = cx.struct_span_err(span, "cannot concatenate numeric literals");
51 if let Ok(snippet) = cx.sess.source_map().span_to_snippet(span) {
54 "try wrapping the number in an array",
55 format!("[{}]", snippet),
56 Applicability::MachineApplicable,
63 ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
65 assert!(val > u8::MAX.into()); // must be an error
66 cx.span_err(span, "numeric literal is out of bounds");
68 Ok(ast::LitKind::Int(_, _)) => {
69 cx.span_err(span, "numeric literal is not a `u8`");
75 fn handle_array_element(
76 cx: &mut base::ExtCtxt<'_>,
77 has_errors: &mut bool,
78 missing_literals: &mut Vec<rustc_span::Span>,
79 expr: &P<rustc_ast::Expr>,
82 ast::ExprKind::Array(_) | ast::ExprKind::Repeat(_, _) => {
84 cx.span_err(expr.span, "cannot concatenate doubly nested array");
89 ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
92 ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
93 )) if val <= u8::MAX.into() => Some(val as u8),
95 Ok(ast::LitKind::Byte(val)) => Some(val),
96 Ok(ast::LitKind::ByteStr(_)) => {
98 cx.struct_span_err(expr.span, "cannot concatenate doubly nested array")
99 .note("byte strings are treated as arrays of bytes")
100 .help("try flattening the array")
108 invalid_type_err(cx, token_lit, expr.span, true);
114 ast::ExprKind::IncludedBytes(..) => {
116 cx.struct_span_err(expr.span, "cannot concatenate doubly nested array")
117 .note("byte strings are treated as arrays of bytes")
118 .help("try flattening the array")
125 missing_literals.push(expr.span);
131 pub fn expand_concat_bytes(
132 cx: &mut base::ExtCtxt<'_>,
133 sp: rustc_span::Span,
135 ) -> Box<dyn base::MacResult + 'static> {
136 let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
137 return DummyResult::any(sp);
139 let mut accumulator = Vec::new();
140 let mut missing_literals = vec![];
141 let mut has_errors = false;
144 ast::ExprKind::Array(ref exprs) => {
147 handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
149 accumulator.push(elem);
153 ast::ExprKind::Repeat(ref expr, ref count) => {
154 if let ast::ExprKind::Lit(token_lit) = count.value.kind
155 && let Ok(ast::LitKind::Int(count_val, _)) =
156 ast::LitKind::from_token_lit(token_lit)
159 handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
161 for _ in 0..count_val {
162 accumulator.push(elem);
166 cx.span_err(count.value.span, "repeat count is not a positive number");
169 ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
170 Ok(ast::LitKind::Byte(val)) => {
171 accumulator.push(val);
173 Ok(ast::LitKind::ByteStr(ref bytes)) => {
174 accumulator.extend_from_slice(&bytes);
178 invalid_type_err(cx, token_lit, e.span, false);
183 ast::ExprKind::IncludedBytes(ref bytes) => {
184 accumulator.extend_from_slice(bytes);
186 ast::ExprKind::Err => {
190 missing_literals.push(e.span);
194 if !missing_literals.is_empty() {
195 let mut err = cx.struct_span_err(missing_literals.clone(), "expected a byte literal");
196 err.note("only byte literals (like `b\"foo\"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`");
198 return base::MacEager::expr(DummyResult::raw_expr(sp, true));
199 } else if has_errors {
200 return base::MacEager::expr(DummyResult::raw_expr(sp, true));
202 let sp = cx.with_def_site_ctxt(sp);
203 base::MacEager::expr(cx.expr_byte_str(sp, accumulator))