@@ -103,7 +103,7 @@ impl ToInternal<token::LitKind> for LitKind {
103103 }
104104}
105105
106- impl FromInternal < ( TokenStream , & mut Rustc < ' _ , ' _ > ) > for Vec < TokenTree < TokenStream , Span , Symbol > > {
106+ impl FromInternal < ( TokenStream , & mut Rustc < ' _ , ' _ > ) > for Vec < TokenTree < Span , Symbol > > {
107107 fn from_internal ( ( stream, rustc) : ( TokenStream , & mut Rustc < ' _ , ' _ > ) ) -> Self {
108108 use rustc_ast:: token:: * ;
109109
@@ -150,7 +150,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
150150
151151 trees. push ( TokenTree :: Group ( Group {
152152 delimiter : rustc_proc_macro:: Delimiter :: from_internal ( delim) ,
153- stream : Some ( stream) ,
153+ stream : Some ( rustc . ts_rustc_to_pm ( stream) ) ,
154154 span : DelimSpan {
155155 open : span. open ,
156156 close : span. close ,
@@ -277,7 +277,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
277277 TokenStream :: token_alone ( token:: Lifetime ( ident. name , is_raw) , ident. span ) ;
278278 trees. push ( TokenTree :: Group ( Group {
279279 delimiter : rustc_proc_macro:: Delimiter :: None ,
280- stream : Some ( stream) ,
280+ stream : Some ( rustc . ts_rustc_to_pm ( stream) ) ,
281281 span : DelimSpan :: from_single ( span) ,
282282 } ) )
283283 }
@@ -309,7 +309,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
309309 }
310310 trees. push ( TokenTree :: Group ( Group {
311311 delimiter : rustc_proc_macro:: Delimiter :: Bracket ,
312- stream : Some ( stream) ,
312+ stream : Some ( rustc . ts_rustc_to_pm ( stream) ) ,
313313 span : DelimSpan :: from_single ( span) ,
314314 } ) ) ;
315315 }
@@ -324,7 +324,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
324324
325325// We use a `SmallVec` because the output size is always one or two `TokenTree`s.
326326impl ToInternal < SmallVec < [ tokenstream:: TokenTree ; 2 ] > >
327- for ( TokenTree < TokenStream , Span , Symbol > , & mut Rustc < ' _ , ' _ > )
327+ for ( TokenTree < Span , Symbol > , & mut Rustc < ' _ , ' _ > )
328328{
329329 fn to_internal ( self ) -> SmallVec < [ tokenstream:: TokenTree ; 2 ] > {
330330 use rustc_ast:: token:: * ;
@@ -378,7 +378,7 @@ impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
378378 tokenstream:: DelimSpan { open, close } ,
379379 DelimSpacing :: new( Spacing :: Alone , Spacing :: Alone ) ,
380380 delimiter. to_internal( ) ,
381- stream. unwrap_or_default( ) ,
381+ rustc . ts_pm_to_rustc ( stream. unwrap_or_default( ) ) ,
382382 ) ]
383383 }
384384 TokenTree :: Ident ( self :: Ident { sym, is_raw, span } ) => {
@@ -431,6 +431,8 @@ impl ToInternal<rustc_errors::Level> for Level {
431431 }
432432}
433433
434+ type BridgeTokenStream = rustc_proc_macro:: bridge:: TokenStream < Span , Symbol > ;
435+
434436pub ( crate ) struct Rustc < ' a , ' b > {
435437 ecx : & ' a mut ExtCtxt < ' b > ,
436438 def_site : Span ,
@@ -456,10 +458,27 @@ impl<'a, 'b> Rustc<'a, 'b> {
456458 fn psess ( & self ) -> & ParseSess {
457459 self . ecx . psess ( )
458460 }
461+
462+ pub ( crate ) fn ts_pm_to_rustc ( & mut self , ts : BridgeTokenStream ) -> tokenstream:: TokenStream {
463+ let mut t = tokenstream:: TokenStream :: new ( vec ! [ ] ) ;
464+ for tree in ts. trees {
465+ let internal: SmallVec < [ tokenstream:: TokenTree ; 2 ] > =
466+ ToInternal :: to_internal ( ( tree, & mut * self ) ) ;
467+ for tree in internal {
468+ t. push_tree ( tree) ;
469+ }
470+ }
471+
472+ t
473+ }
474+
475+ pub ( crate ) fn ts_rustc_to_pm ( & mut self , ts : tokenstream:: TokenStream ) -> BridgeTokenStream {
476+ let trees: Vec < TokenTree < Span , Symbol > > = FromInternal :: from_internal ( ( ts, self ) ) ;
477+ BridgeTokenStream { trees }
478+ }
459479}
460480
461- impl server:: Server for Rustc < ' _ , ' _ > {
462- type TokenStream = TokenStream ;
481+ impl server:: Server for & mut Rustc < ' _ , ' _ > {
463482 type Span = Span ;
464483 type Symbol = Symbol ;
465484
@@ -564,35 +583,24 @@ impl server::Server for Rustc<'_, '_> {
564583 diag. emit ( ) ;
565584 }
566585
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 (
586+ fn ts_from_str ( & mut self , src : & str ) -> BridgeTokenStream {
587+ self . ts_rustc_to_pm ( unwrap_or_emit_fatal ( source_str_to_stream (
581588 self . psess ( ) ,
582589 FileName :: proc_macro_source_code ( src) ,
583590 src. to_string ( ) ,
584591 Some ( self . call_site ) ,
585- ) )
592+ ) ) )
586593 }
587594
588- fn ts_to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
589- pprust:: tts_to_string ( stream)
595+ fn ts_to_string ( & mut self , stream : BridgeTokenStream ) -> String {
596+ pprust:: tts_to_string ( & self . ts_pm_to_rustc ( stream) )
590597 }
591598
592- fn ts_expand_expr ( & mut self , stream : & Self :: TokenStream ) -> Result < Self :: TokenStream , ( ) > {
599+ fn ts_expand_expr ( & mut self , stream : BridgeTokenStream ) -> Result < BridgeTokenStream , ( ) > {
593600 // Parse the expression from our tokenstream.
594601 let expr: PResult < ' _ , _ > = try {
595- let mut p = Parser :: new ( self . psess ( ) , stream. clone ( ) , Some ( "proc_macro expand expr" ) ) ;
602+ let stream = self . ts_pm_to_rustc ( stream) ;
603+ let mut p = Parser :: new ( self . psess ( ) , stream, Some ( "proc_macro expand expr" ) ) ;
596604 let expr = p. parse_expr ( ) ?;
597605 if p. token != token:: Eof {
598606 p. unexpected ( ) ?;
@@ -615,32 +623,34 @@ impl server::Server for Rustc<'_, '_> {
615623 // We don't use `TokenStream::from_ast` as the tokenstream currently cannot
616624 // be recovered in the general case.
617625 match & expr. kind {
618- ast:: ExprKind :: Lit ( token_lit) if token_lit. kind == token:: Bool => {
619- Ok ( tokenstream:: TokenStream :: token_alone (
626+ ast:: ExprKind :: Lit ( token_lit) if token_lit. kind == token:: Bool => Ok ( self
627+ . ts_rustc_to_pm ( tokenstream:: TokenStream :: token_alone (
620628 token:: Ident ( token_lit. symbol , IdentIsRaw :: No ) ,
621629 expr. span ,
622- ) )
623- }
624- ast:: ExprKind :: Lit ( token_lit) => {
625- Ok ( tokenstream:: TokenStream :: token_alone ( token:: Literal ( * token_lit) , expr. span ) )
626- }
630+ ) ) ) ,
631+ ast:: ExprKind :: Lit ( token_lit) => Ok ( self . ts_rustc_to_pm (
632+ tokenstream:: TokenStream :: token_alone ( token:: Literal ( * token_lit) , expr. span ) ,
633+ ) ) ,
627634 ast:: ExprKind :: IncludedBytes ( byte_sym) => {
628635 let lit = token:: Lit :: new (
629636 token:: ByteStr ,
630637 escape_byte_str_symbol ( byte_sym. as_byte_str ( ) ) ,
631638 None ,
632639 ) ;
633- Ok ( tokenstream:: TokenStream :: token_alone ( token:: TokenKind :: Literal ( lit) , expr. span ) )
640+ Ok ( self . ts_rustc_to_pm ( tokenstream:: TokenStream :: token_alone (
641+ token:: TokenKind :: Literal ( lit) ,
642+ expr. span ,
643+ ) ) )
634644 }
635645 ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => match & e. kind {
636646 ast:: ExprKind :: Lit ( token_lit) => match token_lit {
637647 token:: Lit { kind : token:: Integer | token:: Float , .. } => {
638- Ok ( Self :: TokenStream :: from_iter ( [
648+ Ok ( self . ts_rustc_to_pm ( tokenstream :: TokenStream :: from_iter ( [
639649 // FIXME: The span of the `-` token is lost when
640650 // parsing, so we cannot faithfully recover it here.
641651 tokenstream:: TokenTree :: token_joint_hidden ( token:: Minus , e. span ) ,
642652 tokenstream:: TokenTree :: token_alone ( token:: Literal ( * token_lit) , e. span ) ,
643- ] ) )
653+ ] ) ) )
644654 }
645655 _ => Err ( ( ) ) ,
646656 } ,
@@ -650,46 +660,6 @@ impl server::Server for Rustc<'_, '_> {
650660 }
651661 }
652662
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-
693663 fn span_debug ( & mut self , span : Self :: Span ) -> String {
694664 if self . ecx . ecfg . span_debug {
695665 format ! ( "{span:?}" )
0 commit comments