rustc_expand/
proc_macro_server.rs

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