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