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