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