Skip to main content

rustc_expand/
proc_macro_server.rs

1use std::ops::{Bound, Range};
2
3use ast::token::IdentIsRaw;
4use rustc_ast as ast;
5use rustc_ast::token;
6use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream};
7use rustc_ast::util::literal::escape_byte_str_symbol;
8use rustc_ast_pretty::pprust;
9use rustc_data_structures::fx::FxHashMap;
10use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan};
11use rustc_parse::lexer::{StripTokens, nfc_normalize};
12use rustc_parse::parser::Parser;
13use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream};
14use rustc_proc_macro::bridge::{
15    DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
16};
17use rustc_proc_macro::{Delimiter, Level};
18use rustc_session::Session;
19use rustc_session::parse::ParseSess;
20use rustc_span::def_id::CrateNum;
21use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
22use smallvec::{SmallVec, smallvec};
23
24use crate::base::ExtCtxt;
25
26trait FromInternal<T> {
27    fn from_internal(x: T) -> Self;
28}
29
30trait ToInternal<T> {
31    fn to_internal(self) -> T;
32}
33
34impl FromInternal<token::Delimiter> for Delimiter {
35    fn from_internal(delim: token::Delimiter) -> Delimiter {
36        match delim {
37            token::Delimiter::Parenthesis => Delimiter::Parenthesis,
38            token::Delimiter::Brace => Delimiter::Brace,
39            token::Delimiter::Bracket => Delimiter::Bracket,
40            token::Delimiter::Invisible(_) => Delimiter::None,
41        }
42    }
43}
44
45impl ToInternal<token::Delimiter> for Delimiter {
46    fn to_internal(self) -> token::Delimiter {
47        match self {
48            Delimiter::Parenthesis => token::Delimiter::Parenthesis,
49            Delimiter::Brace => token::Delimiter::Brace,
50            Delimiter::Bracket => token::Delimiter::Bracket,
51            Delimiter::None => token::Delimiter::Invisible(token::InvisibleOrigin::ProcMacro),
52        }
53    }
54}
55
56impl FromInternal<token::LitKind> for LitKind {
57    fn from_internal(kind: token::LitKind) -> Self {
58        match kind {
59            token::Byte => LitKind::Byte,
60            token::Char => LitKind::Char,
61            token::Integer => LitKind::Integer,
62            token::Float => LitKind::Float,
63            token::Str => LitKind::Str,
64            token::StrRaw(n) => LitKind::StrRaw(n),
65            token::ByteStr => LitKind::ByteStr,
66            token::ByteStrRaw(n) => LitKind::ByteStrRaw(n),
67            token::CStr => LitKind::CStr,
68            token::CStrRaw(n) => LitKind::CStrRaw(n),
69            token::Err(_guar) => {
70                // This is the only place a `rustc_proc_macro::bridge::LitKind::ErrWithGuar`
71                // is constructed. Note that an `ErrorGuaranteed` is available,
72                // as required. See the comment in `to_internal`.
73                LitKind::ErrWithGuar
74            }
75            token::Bool => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
76        }
77    }
78}
79
80impl ToInternal<token::LitKind> for LitKind {
81    fn to_internal(self) -> token::LitKind {
82        match self {
83            LitKind::Byte => token::Byte,
84            LitKind::Char => token::Char,
85            LitKind::Integer => token::Integer,
86            LitKind::Float => token::Float,
87            LitKind::Str => token::Str,
88            LitKind::StrRaw(n) => token::StrRaw(n),
89            LitKind::ByteStr => token::ByteStr,
90            LitKind::ByteStrRaw(n) => token::ByteStrRaw(n),
91            LitKind::CStr => token::CStr,
92            LitKind::CStrRaw(n) => token::CStrRaw(n),
93            LitKind::ErrWithGuar => {
94                // This is annoying but valid. `LitKind::ErrWithGuar` would
95                // have an `ErrorGuaranteed` except that type isn't available
96                // in that crate. So we have to fake one. And we don't want to
97                // use a delayed bug because there might be lots of these,
98                // which would be expensive.
99                #[allow(deprecated)]
100                let guar = ErrorGuaranteed::unchecked_error_guaranteed();
101                token::Err(guar)
102            }
103        }
104    }
105}
106
107impl FromInternal<TokenStream> for Vec<TokenTree<TokenStream, Span, Symbol>> {
108    fn from_internal(stream: TokenStream) -> Self {
109        use rustc_ast::token::*;
110
111        // Estimate the capacity as `stream.len()` rounded up to the next power
112        // of two to limit the number of required reallocations.
113        let mut trees = Vec::with_capacity(stream.len().next_power_of_two());
114
115        for tree in stream.iter() {
116            let (Token { kind, span }, joint) = match tree.clone() {
117                tokenstream::TokenTree::Delimited(span, _, mut delim, mut stream) => {
118                    // In `mk_delimited` we avoid nesting invisible delimited
119                    // of the same `MetaVarKind`. Here we do the same but
120                    // ignore the `MetaVarKind` because it is discarded when we
121                    // convert it to a `Group`.
122                    while let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim
123                        && stream.len() == 1
124                        && let tree = stream.get(0).unwrap()
125                        && let tokenstream::TokenTree::Delimited(_, _, delim2, stream2) = tree
126                        && let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim2
127                    {
128                        delim = *delim2;
129                        stream = stream2.clone();
130                    }
131
132                    trees.push(TokenTree::Group(Group {
133                        delimiter: rustc_proc_macro::Delimiter::from_internal(delim),
134                        stream: Some(stream),
135                        span: DelimSpan {
136                            open: span.open,
137                            close: span.close,
138                            entire: span.entire(),
139                        },
140                    }));
141                    continue;
142                }
143                tokenstream::TokenTree::Token(token, spacing) => {
144                    // Do not be tempted to check here that the `spacing`
145                    // values are "correct" w.r.t. the token stream (e.g. that
146                    // `Spacing::Joint` is actually followed by a `Punct` token
147                    // tree). Because the problem in #76399 was introduced that
148                    // way.
149                    //
150                    // This is where the `Hidden` in `JointHidden` applies,
151                    // because the jointness is effectively hidden from proc
152                    // macros.
153                    let joint = match spacing {
154                        Spacing::Alone | Spacing::JointHidden => false,
155                        Spacing::Joint => true,
156                    };
157                    (token, joint)
158                }
159            };
160
161            // Split the operator into one or more `Punct`s, one per character.
162            // The final one inherits the jointness of the original token. Any
163            // before that get `joint = true`.
164            let mut op = |s: &str| {
165                if !s.is_ascii() {
    ::core::panicking::panic("assertion failed: s.is_ascii()")
};assert!(s.is_ascii());
166                trees.extend(s.bytes().enumerate().map(|(i, ch)| {
167                    let is_final = i == s.len() - 1;
168                    // Split the token span into single chars. Unless the span
169                    // is an unusual one, e.g. due to proc macro expansion. We
170                    // determine this by assuming any span with a length that
171                    // matches the operator length is a normal one, and any
172                    // span with a different length is an unusual one.
173                    let span = if (span.hi() - span.lo()).to_usize() == s.len() {
174                        let lo = span.lo() + BytePos::from_usize(i);
175                        let hi = lo + BytePos::from_usize(1);
176                        span.with_lo(lo).with_hi(hi)
177                    } else {
178                        span
179                    };
180                    let joint = if is_final { joint } else { true };
181                    TokenTree::Punct(Punct { ch, joint, span })
182                }));
183            };
184
185            match kind {
186                Eq => op("="),
187                Lt => op("<"),
188                Le => op("<="),
189                EqEq => op("=="),
190                Ne => op("!="),
191                Ge => op(">="),
192                Gt => op(">"),
193                AndAnd => op("&&"),
194                OrOr => op("||"),
195                Bang => op("!"),
196                Tilde => op("~"),
197                Plus => op("+"),
198                Minus => op("-"),
199                Star => op("*"),
200                Slash => op("/"),
201                Percent => op("%"),
202                Caret => op("^"),
203                And => op("&"),
204                Or => op("|"),
205                Shl => op("<<"),
206                Shr => op(">>"),
207                PlusEq => op("+="),
208                MinusEq => op("-="),
209                StarEq => op("*="),
210                SlashEq => op("/="),
211                PercentEq => op("%="),
212                CaretEq => op("^="),
213                AndEq => op("&="),
214                OrEq => op("|="),
215                ShlEq => op("<<="),
216                ShrEq => op(">>="),
217                At => op("@"),
218                Dot => op("."),
219                DotDot => op(".."),
220                DotDotDot => op("..."),
221                DotDotEq => op("..="),
222                Comma => op(","),
223                Semi => op(";"),
224                Colon => op(":"),
225                PathSep => op("::"),
226                RArrow => op("->"),
227                LArrow => op("<-"),
228                FatArrow => op("=>"),
229                Pound => op("#"),
230                Dollar => op("$"),
231                Question => op("?"),
232                SingleQuote => op("'"),
233
234                Ident(sym, is_raw) => trees.push(TokenTree::Ident(Ident {
235                    sym,
236                    is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
    IdentIsRaw::Yes => true,
    _ => false,
}matches!(is_raw, IdentIsRaw::Yes),
237                    span,
238                })),
239                NtIdent(ident, is_raw) => trees.push(TokenTree::Ident(Ident {
240                    sym: ident.name,
241                    is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
    IdentIsRaw::Yes => true,
    _ => false,
}matches!(is_raw, IdentIsRaw::Yes),
242                    span: ident.span,
243                })),
244
245                Lifetime(name, is_raw) => {
246                    let ident = rustc_span::Ident::new(name, span).without_first_quote();
247                    trees.extend([
248                        TokenTree::Punct(Punct { ch: b'\'', joint: true, span }),
249                        TokenTree::Ident(Ident {
250                            sym: ident.name,
251                            is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
    IdentIsRaw::Yes => true,
    _ => false,
}matches!(is_raw, IdentIsRaw::Yes),
252                            span,
253                        }),
254                    ]);
255                }
256                NtLifetime(ident, is_raw) => {
257                    let stream =
258                        TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span);
259                    trees.push(TokenTree::Group(Group {
260                        delimiter: rustc_proc_macro::Delimiter::None,
261                        stream: Some(stream),
262                        span: DelimSpan::from_single(span),
263                    }))
264                }
265
266                Literal(token::Lit { kind, symbol, suffix }) => {
267                    trees.push(TokenTree::Literal(self::Literal {
268                        kind: FromInternal::from_internal(kind),
269                        symbol,
270                        suffix,
271                        span,
272                    }));
273                }
274                DocComment(_, attr_style, data) => {
275                    let mut escaped = String::new();
276                    for ch in data.as_str().chars() {
277                        escaped.extend(ch.escape_debug());
278                    }
279                    let stream = [
280                        Ident(sym::doc, IdentIsRaw::No),
281                        Eq,
282                        TokenKind::lit(token::Str, Symbol::intern(&escaped), None),
283                    ]
284                    .into_iter()
285                    .map(|kind| tokenstream::TokenTree::token_alone(kind, span))
286                    .collect();
287                    trees.push(TokenTree::Punct(Punct { ch: b'#', joint: false, span }));
288                    if attr_style == ast::AttrStyle::Inner {
289                        trees.push(TokenTree::Punct(Punct { ch: b'!', joint: false, span }));
290                    }
291                    trees.push(TokenTree::Group(Group {
292                        delimiter: rustc_proc_macro::Delimiter::Bracket,
293                        stream: Some(stream),
294                        span: DelimSpan::from_single(span),
295                    }));
296                }
297
298                OpenParen | CloseParen | OpenBrace | CloseBrace | OpenBracket | CloseBracket
299                | OpenInvisible(_) | CloseInvisible(_) | Eof => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
300            }
301        }
302        trees
303    }
304}
305
306// We use a `SmallVec` because the output size is always one or two `TokenTree`s.
307impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
308    for (TokenTree<TokenStream, Span, Symbol>, &mut Rustc<'_, '_>)
309{
310    fn to_internal(self) -> SmallVec<[tokenstream::TokenTree; 2]> {
311        use rustc_ast::token::*;
312
313        // The code below is conservative, using `token_alone`/`Spacing::Alone`
314        // in most places. It's hard in general to do better when working at
315        // the token level. When the resulting code is pretty-printed by
316        // `print_tts` the `space_between` function helps avoid a lot of
317        // unnecessary whitespace, so the results aren't too bad.
318        let (tree, rustc) = self;
319        match tree {
320            TokenTree::Punct(Punct { ch, joint, span }) => {
321                let kind = match ch {
322                    b'=' => Eq,
323                    b'<' => Lt,
324                    b'>' => Gt,
325                    b'!' => Bang,
326                    b'~' => Tilde,
327                    b'+' => Plus,
328                    b'-' => Minus,
329                    b'*' => Star,
330                    b'/' => Slash,
331                    b'%' => Percent,
332                    b'^' => Caret,
333                    b'&' => And,
334                    b'|' => Or,
335                    b'@' => At,
336                    b'.' => Dot,
337                    b',' => Comma,
338                    b';' => Semi,
339                    b':' => Colon,
340                    b'#' => Pound,
341                    b'$' => Dollar,
342                    b'?' => Question,
343                    b'\'' => SingleQuote,
344                    _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
345                };
346                // We never produce `token::Spacing::JointHidden` here, which
347                // means the pretty-printing of code produced by proc macros is
348                // ugly, with lots of whitespace between tokens. This is
349                // unavoidable because `proc_macro::Spacing` only applies to
350                // `Punct` token trees.
351                {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(if joint {
                tokenstream::TokenTree::token_joint(kind, span)
            } else { tokenstream::TokenTree::token_alone(kind, span) });
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [if joint {
                                tokenstream::TokenTree::token_joint(kind, span)
                            } else {
                                tokenstream::TokenTree::token_alone(kind, span)
                            }])))
    }
}smallvec![if joint {
352                    tokenstream::TokenTree::token_joint(kind, span)
353                } else {
354                    tokenstream::TokenTree::token_alone(kind, span)
355                }]
356            }
357            TokenTree::Group(Group { delimiter, stream, span: DelimSpan { open, close, .. } }) => {
358                {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(tokenstream::TokenTree::Delimited(tokenstream::DelimSpan {
                    open,
                    close,
                }, DelimSpacing::new(Spacing::Alone, Spacing::Alone),
                delimiter.to_internal(), stream.unwrap_or_default()));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [tokenstream::TokenTree::Delimited(tokenstream::DelimSpan {
                                    open,
                                    close,
                                }, DelimSpacing::new(Spacing::Alone, Spacing::Alone),
                                delimiter.to_internal(), stream.unwrap_or_default())])))
    }
}smallvec![tokenstream::TokenTree::Delimited(
359                    tokenstream::DelimSpan { open, close },
360                    DelimSpacing::new(Spacing::Alone, Spacing::Alone),
361                    delimiter.to_internal(),
362                    stream.unwrap_or_default(),
363                )]
364            }
365            TokenTree::Ident(self::Ident { sym, is_raw, span }) => {
366                rustc.psess().symbol_gallery.insert(sym, span);
367                {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(tokenstream::TokenTree::token_alone(Ident(sym,
                    is_raw.into()), span));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [tokenstream::TokenTree::token_alone(Ident(sym,
                                    is_raw.into()), span)])))
    }
}smallvec![tokenstream::TokenTree::token_alone(Ident(sym, is_raw.into()), span)]
368            }
369            TokenTree::Literal(self::Literal {
370                kind: self::LitKind::Integer,
371                symbol,
372                suffix,
373                span,
374            }) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
375                let symbol = Symbol::intern(symbol);
376                let integer = TokenKind::lit(token::Integer, symbol, suffix);
377                let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
378                let b = tokenstream::TokenTree::token_alone(integer, span);
379                {
    let count = 0usize + 1usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(a);
        vec.push(b);
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [a, b])))
    }
}smallvec![a, b]
380            }
381            TokenTree::Literal(self::Literal {
382                kind: self::LitKind::Float,
383                symbol,
384                suffix,
385                span,
386            }) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
387                let symbol = Symbol::intern(symbol);
388                let float = TokenKind::lit(token::Float, symbol, suffix);
389                let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
390                let b = tokenstream::TokenTree::token_alone(float, span);
391                {
    let count = 0usize + 1usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(a);
        vec.push(b);
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [a, b])))
    }
}smallvec![a, b]
392            }
393            TokenTree::Literal(self::Literal { kind, symbol, suffix, span }) => {
394                {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(tokenstream::TokenTree::token_alone(TokenKind::lit(kind.to_internal(),
                    symbol, suffix), span));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [tokenstream::TokenTree::token_alone(TokenKind::lit(kind.to_internal(),
                                    symbol, suffix), span)])))
    }
}smallvec![tokenstream::TokenTree::token_alone(
395                    TokenKind::lit(kind.to_internal(), symbol, suffix),
396                    span,
397                )]
398            }
399        }
400    }
401}
402
403impl ToInternal<rustc_errors::Level> for Level {
404    fn to_internal(self) -> rustc_errors::Level {
405        match self {
406            Level::Error => rustc_errors::Level::Error,
407            Level::Warning => rustc_errors::Level::Warning,
408            Level::Note => rustc_errors::Level::Note,
409            Level::Help => rustc_errors::Level::Help,
410            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unknown proc_macro::Level variant: {0:?}", self)));
}unreachable!("unknown proc_macro::Level variant: {:?}", self),
411        }
412    }
413}
414
415fn cancel_diags_into_string(diags: Vec<Diag<'_>>) -> String {
416    let mut messages = diags.into_iter().flat_map(Diag::cancel_into_message);
417    let msg = messages.next().expect("no diagnostic has a message");
418    messages.for_each(|_| ()); // consume iterator to cancel the remaining diagnostics
419    msg
420}
421
422pub(crate) struct Rustc<'a, 'b> {
423    ecx: &'a mut ExtCtxt<'b>,
424    def_site: Span,
425    call_site: Span,
426    mixed_site: Span,
427    krate: CrateNum,
428    rebased_spans: FxHashMap<usize, Span>,
429}
430
431impl<'a, 'b> Rustc<'a, 'b> {
432    pub(crate) fn new(ecx: &'a mut ExtCtxt<'b>) -> Self {
433        let expn_data = ecx.current_expansion.id.expn_data();
434        Rustc {
435            def_site: ecx.with_def_site_ctxt(expn_data.def_site),
436            call_site: ecx.with_call_site_ctxt(expn_data.call_site),
437            mixed_site: ecx.with_mixed_site_ctxt(expn_data.call_site),
438            krate: expn_data.macro_def_id.unwrap().krate,
439            rebased_spans: FxHashMap::default(),
440            ecx,
441        }
442    }
443
444    fn sess(&self) -> &Session {
445        &self.ecx.sess
446    }
447
448    fn psess(&self) -> &ParseSess {
449        self.ecx.psess()
450    }
451}
452
453impl server::Server for Rustc<'_, '_> {
454    type TokenStream = TokenStream;
455    type Span = Span;
456    type Symbol = Symbol;
457
458    fn globals(&mut self) -> ExpnGlobals<Self::Span> {
459        ExpnGlobals {
460            def_site: self.def_site,
461            call_site: self.call_site,
462            mixed_site: self.mixed_site,
463        }
464    }
465
466    fn intern_symbol(string: &str) -> Self::Symbol {
467        Symbol::intern(string)
468    }
469
470    fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
471        f(symbol.as_str())
472    }
473
474    fn injected_env_var(&mut self, var: &str) -> Option<String> {
475        self.ecx.sess.opts.logical_env.get(var).cloned()
476    }
477
478    fn track_env_var(&mut self, var: &str, value: Option<&str>) {
479        self.ecx
480            .sess
481            .env_depinfo
482            .borrow_mut()
483            .insert((Symbol::intern(var), value.map(Symbol::intern)));
484    }
485
486    fn track_path(&mut self, path: &str) {
487        self.ecx.sess.file_depinfo.borrow_mut().insert(Symbol::intern(path));
488    }
489
490    fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, String> {
491        let name = FileName::proc_macro_source_code(s);
492
493        let mut parser =
494            new_parser_from_source_str(self.psess(), name, s.to_owned(), StripTokens::Nothing)
495                .map_err(cancel_diags_into_string)?;
496
497        let first_span = parser.token.span.data();
498        let minus_present = parser.eat(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Minus,
    token_type: ::rustc_parse::parser::token_type::TokenType::Minus,
}exp!(Minus));
499
500        let lit_span = parser.token.span.data();
501        let token::Literal(mut lit) = parser.token.kind else {
502            return Err("not a literal".to_string());
503        };
504
505        // Check no comment or whitespace surrounding the (possibly negative)
506        // literal, or more tokens after it.
507        if (lit_span.hi.0 - first_span.lo.0) as usize != s.len() {
508            return Err("comment or whitespace around literal".to_string());
509        }
510
511        if minus_present {
512            // If minus is present, check no comment or whitespace in between it
513            // and the literal token.
514            if first_span.hi.0 != lit_span.lo.0 {
515                return Err("comment or whitespace after minus".to_string());
516            }
517
518            // Check literal is a kind we allow to be negated in a proc macro token.
519            match lit.kind {
520                token::LitKind::Bool
521                | token::LitKind::Byte
522                | token::LitKind::Char
523                | token::LitKind::Str
524                | token::LitKind::StrRaw(_)
525                | token::LitKind::ByteStr
526                | token::LitKind::ByteStrRaw(_)
527                | token::LitKind::CStr
528                | token::LitKind::CStrRaw(_)
529                | token::LitKind::Err(_) => {
530                    return Err("non-numeric literal may not be negated".to_string());
531                }
532                token::LitKind::Integer | token::LitKind::Float => {}
533            }
534
535            // Synthesize a new symbol that includes the minus sign.
536            let symbol = Symbol::intern(&s[..1 + lit.symbol.as_str().len()]);
537            lit = token::Lit::new(lit.kind, symbol, lit.suffix);
538        }
539        let token::Lit { kind, symbol, suffix } = lit;
540        Ok(Literal {
541            kind: FromInternal::from_internal(kind),
542            symbol,
543            suffix,
544            span: self.call_site,
545        })
546    }
547
548    fn emit_diagnostic(&mut self, diagnostic: Diagnostic<Self::Span>) {
549        let message = rustc_errors::DiagMessage::from(diagnostic.message);
550        let mut diag: Diag<'_, ()> =
551            Diag::new(self.psess().dcx(), diagnostic.level.to_internal(), message);
552        diag.span(MultiSpan::from_spans(diagnostic.spans));
553        for child in diagnostic.children {
554            diag.sub(child.level.to_internal(), child.message, MultiSpan::from_spans(child.spans));
555        }
556        diag.emit();
557    }
558
559    fn ts_drop(&mut self, stream: Self::TokenStream) {
560        drop(stream);
561    }
562
563    fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
564        stream.clone()
565    }
566
567    fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
568        stream.is_empty()
569    }
570
571    fn ts_from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
572        source_str_to_stream(
573            self.psess(),
574            FileName::proc_macro_source_code(src),
575            src.to_string(),
576            Some(self.call_site),
577        )
578        .map_err(cancel_diags_into_string)
579    }
580
581    fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String {
582        pprust::tts_to_string(stream)
583    }
584
585    fn ts_expand_expr(&mut self, stream: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
586        // Parse the expression from our tokenstream.
587        let expr = try {
588            let mut p = Parser::new(self.psess(), stream.clone(), Some("proc_macro expand expr"));
589            let expr = p.parse_expr()?;
590            if p.token != token::Eof {
591                p.unexpected()?;
592            }
593            expr
594        };
595        let expr = expr.map_err(|err| {
596            err.emit();
597        })?;
598
599        // Perform eager expansion on the expression.
600        let expr = self
601            .ecx
602            .expander()
603            .fully_expand_fragment(crate::expand::AstFragment::Expr(expr))
604            .make_expr();
605
606        // NOTE: For now, limit `expand_expr` to exclusively expand to literals.
607        // This may be relaxed in the future.
608        // We don't use `TokenStream::from_ast` as the tokenstream currently cannot
609        // be recovered in the general case.
610        match &expr.kind {
611            ast::ExprKind::Lit(token_lit) if token_lit.kind == token::Bool => {
612                Ok(tokenstream::TokenStream::token_alone(
613                    token::Ident(token_lit.symbol, IdentIsRaw::No),
614                    expr.span,
615                ))
616            }
617            ast::ExprKind::Lit(token_lit) => {
618                Ok(tokenstream::TokenStream::token_alone(token::Literal(*token_lit), expr.span))
619            }
620            ast::ExprKind::IncludedBytes(byte_sym) => {
621                let lit = token::Lit::new(
622                    token::ByteStr,
623                    escape_byte_str_symbol(byte_sym.as_byte_str()),
624                    None,
625                );
626                Ok(tokenstream::TokenStream::token_alone(token::TokenKind::Literal(lit), expr.span))
627            }
628            ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind {
629                ast::ExprKind::Lit(token_lit) => match token_lit {
630                    token::Lit { kind: token::Integer | token::Float, .. } => {
631                        Ok(Self::TokenStream::from_iter([
632                            // FIXME: The span of the `-` token is lost when
633                            // parsing, so we cannot faithfully recover it here.
634                            tokenstream::TokenTree::token_joint_hidden(token::Minus, e.span),
635                            tokenstream::TokenTree::token_alone(token::Literal(*token_lit), e.span),
636                        ]))
637                    }
638                    _ => Err(()),
639                },
640                _ => Err(()),
641            },
642            _ => Err(()),
643        }
644    }
645
646    fn ts_from_token_tree(
647        &mut self,
648        tree: TokenTree<Self::TokenStream, Self::Span, Self::Symbol>,
649    ) -> Self::TokenStream {
650        Self::TokenStream::new((tree, &mut *self).to_internal().into_iter().collect::<Vec<_>>())
651    }
652
653    fn ts_concat_trees(
654        &mut self,
655        base: Option<Self::TokenStream>,
656        trees: Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>>,
657    ) -> Self::TokenStream {
658        let mut stream = base.unwrap_or_default();
659        for tree in trees {
660            for tt in (tree, &mut *self).to_internal() {
661                stream.push_tree(tt);
662            }
663        }
664        stream
665    }
666
667    fn ts_concat_streams(
668        &mut self,
669        base: Option<Self::TokenStream>,
670        streams: Vec<Self::TokenStream>,
671    ) -> Self::TokenStream {
672        let mut stream = base.unwrap_or_default();
673        for s in streams {
674            stream.push_stream(s);
675        }
676        stream
677    }
678
679    fn ts_into_trees(
680        &mut self,
681        stream: Self::TokenStream,
682    ) -> Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>> {
683        FromInternal::from_internal(stream)
684    }
685
686    fn span_debug(&mut self, span: Self::Span) -> String {
687        if self.ecx.ecfg.span_debug {
688            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", span))
    })format!("{span:?}")
689        } else {
690            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?} bytes({1}..{2})",
                span.ctxt(), span.lo().0, span.hi().0))
    })format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
691        }
692    }
693
694    fn span_file(&mut self, span: Self::Span) -> String {
695        self.psess()
696            .source_map()
697            .lookup_char_pos(span.lo())
698            .file
699            .name
700            .prefer_remapped_unconditionally()
701            .to_string()
702    }
703
704    fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
705        self.psess()
706            .source_map()
707            .lookup_char_pos(span.lo())
708            .file
709            .name
710            .clone()
711            .into_local_path()
712            .map(|p| {
713                p.to_str()
714                    .expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
715                    .to_string()
716            })
717    }
718
719    fn span_parent(&mut self, span: Self::Span) -> Option<Self::Span> {
720        span.parent_callsite()
721    }
722
723    fn span_source(&mut self, span: Self::Span) -> Self::Span {
724        span.source_callsite()
725    }
726
727    fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
728        let source_map = self.psess().source_map();
729
730        let relative_start_pos = source_map.lookup_byte_offset(span.lo()).pos;
731        let relative_end_pos = source_map.lookup_byte_offset(span.hi()).pos;
732
733        Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
734    }
735    fn span_start(&mut self, span: Self::Span) -> Self::Span {
736        span.shrink_to_lo()
737    }
738
739    fn span_end(&mut self, span: Self::Span) -> Self::Span {
740        span.shrink_to_hi()
741    }
742
743    fn span_line(&mut self, span: Self::Span) -> usize {
744        let loc = self.psess().source_map().lookup_char_pos(span.lo());
745        loc.line
746    }
747
748    fn span_column(&mut self, span: Self::Span) -> usize {
749        let loc = self.psess().source_map().lookup_char_pos(span.lo());
750        loc.col.to_usize() + 1
751    }
752
753    fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
754        let self_loc = self.psess().source_map().lookup_char_pos(first.lo());
755        let other_loc = self.psess().source_map().lookup_char_pos(second.lo());
756
757        if self_loc.file.stable_id != other_loc.file.stable_id {
758            return None;
759        }
760
761        Some(first.to(second))
762    }
763
764    fn span_subspan(
765        &mut self,
766        span: Self::Span,
767        start: Bound<usize>,
768        end: Bound<usize>,
769    ) -> Option<Self::Span> {
770        let length = span.hi().to_usize() - span.lo().to_usize();
771
772        let start = match start {
773            Bound::Included(lo) => lo,
774            Bound::Excluded(lo) => lo.checked_add(1)?,
775            Bound::Unbounded => 0,
776        };
777
778        let end = match end {
779            Bound::Included(hi) => hi.checked_add(1)?,
780            Bound::Excluded(hi) => hi,
781            Bound::Unbounded => length,
782        };
783
784        // Bounds check the values, preventing addition overflow and OOB spans.
785        if start > u32::MAX as usize
786            || end > u32::MAX as usize
787            || (u32::MAX - start as u32) < span.lo().to_u32()
788            || (u32::MAX - end as u32) < span.lo().to_u32()
789            || start >= end
790            || end > length
791        {
792            return None;
793        }
794
795        let new_lo = span.lo() + BytePos::from_usize(start);
796        let new_hi = span.lo() + BytePos::from_usize(end);
797        Some(span.with_lo(new_lo).with_hi(new_hi))
798    }
799
800    fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
801        span.with_ctxt(at.ctxt())
802    }
803
804    fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
805        self.psess().source_map().span_to_snippet(span).ok()
806    }
807
808    /// Saves the provided span into the metadata of
809    /// *the crate we are currently compiling*, which must
810    /// be a proc-macro crate. This id can be passed to
811    /// `recover_proc_macro_span` when our current crate
812    /// is *run* as a proc-macro.
813    ///
814    /// Let's suppose that we have two crates - `my_client`
815    /// and `my_proc_macro`. The `my_proc_macro` crate
816    /// contains a procedural macro `my_macro`, which
817    /// is implemented as: `quote! { "hello" }`
818    ///
819    /// When we *compile* `my_proc_macro`, we will execute
820    /// the `quote` proc-macro. This will save the span of
821    /// "hello" into the metadata of `my_proc_macro`. As a result,
822    /// the body of `my_proc_macro` (after expansion) will end
823    /// up containing a call that looks like this:
824    /// `proc_macro::Ident::new("hello", proc_macro::Span::recover_proc_macro_span(0))`
825    ///
826    /// where `0` is the id returned by this function.
827    /// When `my_proc_macro` *executes* (during the compilation of `my_client`),
828    /// the call to `recover_proc_macro_span` will load the corresponding
829    /// span from the metadata of `my_proc_macro` (which we have access to,
830    /// since we've loaded `my_proc_macro` from disk in order to execute it).
831    /// In this way, we have obtained a span pointing into `my_proc_macro`
832    fn span_save_span(&mut self, span: Self::Span) -> usize {
833        self.sess().save_proc_macro_span(span)
834    }
835
836    fn span_recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
837        let (resolver, krate, def_site) = (&*self.ecx.resolver, self.krate, self.def_site);
838        *self.rebased_spans.entry(id).or_insert_with(|| {
839            // FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding,
840            // replace it with a def-site context until we are encoding it properly.
841            resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
842        })
843    }
844
845    fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
846        let sym = nfc_normalize(string);
847        if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) }
848    }
849}