rustc_builtin_macros/
concat_bytes.rs

1use rustc_ast::tokenstream::TokenStream;
2use rustc_ast::{ExprKind, LitIntType, LitKind, StrStyle, UintTy, token};
3use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
4use rustc_session::errors::report_lit_error;
5use rustc_span::{ErrorGuaranteed, Span};
6
7use crate::errors;
8use crate::util::get_exprs_from_tts;
9
10/// Emits errors for literal expressions that are invalid inside and outside of an array.
11fn invalid_type_err(
12    cx: &ExtCtxt<'_>,
13    token_lit: token::Lit,
14    span: Span,
15    is_nested: bool,
16) -> ErrorGuaranteed {
17    use errors::{
18        ConcatBytesInvalid, ConcatBytesInvalidSuggestion, ConcatBytesNonU8, ConcatBytesOob,
19    };
20    let snippet = cx.sess.source_map().span_to_snippet(span).ok();
21    let dcx = cx.dcx();
22    match LitKind::from_token_lit(token_lit) {
23        Ok(LitKind::CStr(_, style)) => {
24            // Avoid ambiguity in handling of terminal `NUL` by refusing to
25            // concatenate C string literals as bytes.
26            let sugg = if let Some(mut as_bstr) = snippet
27                && style == StrStyle::Cooked
28                && as_bstr.starts_with('c')
29                && as_bstr.ends_with('"')
30            {
31                // Suggest`c"foo"` -> `b"foo\0"` if we can
32                as_bstr.replace_range(0..1, "b");
33                as_bstr.pop();
34                as_bstr.push_str(r#"\0""#);
35                Some(ConcatBytesInvalidSuggestion::CStrLit { span, as_bstr })
36            } else {
37                // No suggestion for a missing snippet, raw strings, or if for some reason we have
38                // a span that doesn't match `c"foo"` (possible if a proc macro assigns a span
39                // that doesn't actually point to a C string).
40                None
41            };
42            // We can only provide a suggestion if we have a snip and it is not a raw string
43            dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "C string", sugg, cs_note: Some(()) })
44        }
45        Ok(LitKind::Char(_)) => {
46            let sugg =
47                snippet.map(|snippet| ConcatBytesInvalidSuggestion::CharLit { span, snippet });
48            dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "character", sugg, cs_note: None })
49        }
50        Ok(LitKind::Str(_, _)) => {
51            // suggestion would be invalid if we are nested
52            let sugg = if !is_nested {
53                snippet.map(|snippet| ConcatBytesInvalidSuggestion::StrLit { span, snippet })
54            } else {
55                None
56            };
57            dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "string", sugg, cs_note: None })
58        }
59        Ok(LitKind::Float(_, _)) => {
60            dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "float", sugg: None, cs_note: None })
61        }
62        Ok(LitKind::Bool(_)) => dcx.emit_err(ConcatBytesInvalid {
63            span,
64            lit_kind: "boolean",
65            sugg: None,
66            cs_note: None,
67        }),
68        Ok(LitKind::Int(_, _)) if !is_nested => {
69            let sugg =
70                snippet.map(|snippet| ConcatBytesInvalidSuggestion::IntLit { span, snippet });
71            dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "numeric", sugg, cs_note: None })
72        }
73        Ok(LitKind::Int(val, LitIntType::Unsuffixed | LitIntType::Unsigned(UintTy::U8))) => {
74            assert!(val.get() > u8::MAX.into()); // must be an error
75            dcx.emit_err(ConcatBytesOob { span })
76        }
77        Ok(LitKind::Int(_, _)) => dcx.emit_err(ConcatBytesNonU8 { span }),
78        Ok(LitKind::ByteStr(..) | LitKind::Byte(_)) => unreachable!(),
79        Ok(LitKind::Err(guar)) => guar,
80        Err(err) => report_lit_error(&cx.sess.psess, err, token_lit, span),
81    }
82}
83
84/// Returns `expr` as a *single* byte literal if applicable.
85///
86/// Otherwise, returns `None`, and either pushes the `expr`'s span to `missing_literals` or
87/// updates `guar` accordingly.
88fn handle_array_element(
89    cx: &ExtCtxt<'_>,
90    guar: &mut Option<ErrorGuaranteed>,
91    missing_literals: &mut Vec<rustc_span::Span>,
92    expr: &Box<rustc_ast::Expr>,
93) -> Option<u8> {
94    let dcx = cx.dcx();
95
96    match expr.kind {
97        ExprKind::Lit(token_lit) => {
98            match LitKind::from_token_lit(token_lit) {
99                Ok(LitKind::Int(
100                    val,
101                    LitIntType::Unsuffixed | LitIntType::Unsigned(UintTy::U8),
102                )) if let Ok(val) = u8::try_from(val.get()) => {
103                    return Some(val);
104                }
105                Ok(LitKind::Byte(val)) => return Some(val),
106                Ok(LitKind::ByteStr(..)) => {
107                    guar.get_or_insert_with(|| {
108                        dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: true })
109                    });
110                }
111                _ => {
112                    guar.get_or_insert_with(|| invalid_type_err(cx, token_lit, expr.span, true));
113                }
114            };
115        }
116        ExprKind::Array(_) | ExprKind::Repeat(_, _) => {
117            guar.get_or_insert_with(|| {
118                dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: false })
119            });
120        }
121        ExprKind::IncludedBytes(..) => {
122            guar.get_or_insert_with(|| {
123                dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: false })
124            });
125        }
126        _ => missing_literals.push(expr.span),
127    }
128
129    None
130}
131
132pub(crate) fn expand_concat_bytes(
133    cx: &mut ExtCtxt<'_>,
134    sp: Span,
135    tts: TokenStream,
136) -> MacroExpanderResult<'static> {
137    let ExpandResult::Ready(mac) = get_exprs_from_tts(cx, tts) else {
138        return ExpandResult::Retry(());
139    };
140    let es = match mac {
141        Ok(es) => es,
142        Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
143    };
144    let mut accumulator = Vec::new();
145    let mut missing_literals = vec![];
146    let mut guar = None;
147    for e in es {
148        match &e.kind {
149            ExprKind::Array(exprs) => {
150                for expr in exprs {
151                    if let Some(elem) =
152                        handle_array_element(cx, &mut guar, &mut missing_literals, expr)
153                    {
154                        accumulator.push(elem);
155                    }
156                }
157            }
158            ExprKind::Repeat(expr, count) => {
159                if let ExprKind::Lit(token_lit) = count.value.kind
160                    && let Ok(LitKind::Int(count_val, _)) = LitKind::from_token_lit(token_lit)
161                {
162                    if let Some(elem) =
163                        handle_array_element(cx, &mut guar, &mut missing_literals, expr)
164                    {
165                        for _ in 0..count_val.get() {
166                            accumulator.push(elem);
167                        }
168                    }
169                } else {
170                    guar = Some(
171                        cx.dcx().emit_err(errors::ConcatBytesBadRepeat { span: count.value.span }),
172                    );
173                }
174            }
175            &ExprKind::Lit(token_lit) => match LitKind::from_token_lit(token_lit) {
176                Ok(LitKind::Byte(val)) => {
177                    accumulator.push(val);
178                }
179                Ok(LitKind::ByteStr(ref byte_sym, _)) => {
180                    accumulator.extend_from_slice(byte_sym.as_byte_str());
181                }
182                _ => {
183                    guar.get_or_insert_with(|| invalid_type_err(cx, token_lit, e.span, false));
184                }
185            },
186            ExprKind::IncludedBytes(byte_sym) => {
187                accumulator.extend_from_slice(byte_sym.as_byte_str());
188            }
189            ExprKind::Err(guarantee) => {
190                guar = Some(*guarantee);
191            }
192            ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
193            _ => {
194                missing_literals.push(e.span);
195            }
196        }
197    }
198    ExpandResult::Ready(if !missing_literals.is_empty() {
199        let guar = cx.dcx().emit_err(errors::ConcatBytesMissingLiteral { spans: missing_literals });
200        MacEager::expr(DummyResult::raw_expr(sp, Some(guar)))
201    } else if let Some(guar) = guar {
202        MacEager::expr(DummyResult::raw_expr(sp, Some(guar)))
203    } else {
204        let sp = cx.with_def_site_ctxt(sp);
205        MacEager::expr(cx.expr_byte_str(sp, accumulator))
206    })
207}