@@ -284,12 +284,6 @@ impl ToInternal<rustc_errors::Level> for Level {
284284
285285pub struct FreeFunctions ;
286286
287- #[ derive( Clone ) ]
288- pub struct TokenStreamIter {
289- cursor : tokenstream:: Cursor ,
290- stack : Vec < TokenTree < Group , Punct , Ident , Literal > > ,
291- }
292-
293287#[ derive( Clone ) ]
294288pub struct Group {
295289 delimiter : Delimiter ,
@@ -398,8 +392,6 @@ impl<'a> Rustc<'a> {
398392impl server:: Types for Rustc < ' _ > {
399393 type FreeFunctions = FreeFunctions ;
400394 type TokenStream = TokenStream ;
401- type TokenStreamBuilder = tokenstream:: TokenStreamBuilder ;
402- type TokenStreamIter = TokenStreamIter ;
403395 type Group = Group ;
404396 type Punct = Punct ;
405397 type Ident = Ident ;
@@ -417,9 +409,6 @@ impl server::FreeFunctions for Rustc<'_> {
417409}
418410
419411impl server:: TokenStream for Rustc < ' _ > {
420- fn new ( & mut self ) -> Self :: TokenStream {
421- TokenStream :: default ( )
422- }
423412 fn is_empty ( & mut self , stream : & Self :: TokenStream ) -> bool {
424413 stream. is_empty ( )
425414 }
@@ -440,53 +429,74 @@ impl server::TokenStream for Rustc<'_> {
440429 ) -> Self :: TokenStream {
441430 tree. to_internal ( )
442431 }
443- fn into_iter ( & mut self , stream : Self :: TokenStream ) -> Self :: TokenStreamIter {
444- TokenStreamIter { cursor : stream. trees ( ) , stack : vec ! [ ] }
445- }
446- }
447-
448- impl server:: TokenStreamBuilder for Rustc < ' _ > {
449- fn new ( & mut self ) -> Self :: TokenStreamBuilder {
450- tokenstream:: TokenStreamBuilder :: new ( )
451- }
452- fn push ( & mut self , builder : & mut Self :: TokenStreamBuilder , stream : Self :: TokenStream ) {
453- builder. push ( stream) ;
432+ fn concat_trees (
433+ & mut self ,
434+ base : Option < Self :: TokenStream > ,
435+ trees : Vec < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > ,
436+ ) -> Self :: TokenStream {
437+ let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
438+ if let Some ( base) = base {
439+ builder. push ( base) ;
440+ }
441+ for tree in trees {
442+ builder. push ( tree. to_internal ( ) ) ;
443+ }
444+ builder. build ( )
454445 }
455- fn build ( & mut self , builder : Self :: TokenStreamBuilder ) -> Self :: TokenStream {
446+ fn concat_streams (
447+ & mut self ,
448+ base : Option < Self :: TokenStream > ,
449+ streams : Vec < Self :: TokenStream > ,
450+ ) -> Self :: TokenStream {
451+ let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
452+ if let Some ( base) = base {
453+ builder. push ( base) ;
454+ }
455+ for stream in streams {
456+ builder. push ( stream) ;
457+ }
456458 builder. build ( )
457459 }
458- }
459-
460- impl server:: TokenStreamIter for Rustc < ' _ > {
461- fn next (
460+ fn into_iter (
462461 & mut self ,
463- iter : & mut Self :: TokenStreamIter ,
464- ) -> Option < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > {
462+ stream : Self :: TokenStream ,
463+ ) -> Vec < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > {
464+ // XXX: This is a raw port of the previous approach, and can probably be
465+ // optimized.
466+ let mut cursor = stream. trees ( ) ;
467+ let mut stack = Vec :: new ( ) ;
468+ let mut tts = Vec :: new ( ) ;
465469 loop {
466- let tree = iter. stack . pop ( ) . or_else ( || {
467- let next = iter. cursor . next_with_spacing ( ) ?;
468- Some ( TokenTree :: from_internal ( ( next, & mut iter. stack , self ) ) )
469- } ) ?;
470- // A hack used to pass AST fragments to attribute and derive macros
471- // as a single nonterminal token instead of a token stream.
472- // Such token needs to be "unwrapped" and not represented as a delimited group.
473- // FIXME: It needs to be removed, but there are some compatibility issues (see #73345).
474- if let TokenTree :: Group ( ref group) = tree {
475- if group. flatten {
476- iter. cursor . append ( group. stream . clone ( ) ) ;
477- continue ;
470+ let next = stack. pop ( ) . or_else ( || {
471+ let next = cursor. next_with_spacing ( ) ?;
472+ Some ( TokenTree :: from_internal ( ( next, & mut stack, self ) ) )
473+ } ) ;
474+ match next {
475+ Some ( TokenTree :: Group ( group) ) => {
476+ // A hack used to pass AST fragments to attribute and derive
477+ // macros as a single nonterminal token instead of a token
478+ // stream. Such token needs to be "unwrapped" and not
479+ // represented as a delimited group.
480+ // FIXME: It needs to be removed, but there are some
481+ // compatibility issues (see #73345).
482+ if group. flatten {
483+ cursor. append ( group. stream ) ;
484+ continue ;
485+ }
486+ tts. push ( TokenTree :: Group ( group) ) ;
478487 }
488+ Some ( tt) => tts. push ( tt) ,
489+ None => return tts,
479490 }
480- return Some ( tree) ;
481491 }
482492 }
483493}
484494
485495impl server:: Group for Rustc < ' _ > {
486- fn new ( & mut self , delimiter : Delimiter , stream : Self :: TokenStream ) -> Self :: Group {
496+ fn new ( & mut self , delimiter : Delimiter , stream : Option < Self :: TokenStream > ) -> Self :: Group {
487497 Group {
488498 delimiter,
489- stream,
499+ stream : stream . unwrap_or_default ( ) ,
490500 span : DelimSpan :: from_single ( server:: Context :: call_site ( self ) ) ,
491501 flatten : false ,
492502 }
@@ -519,7 +529,11 @@ impl server::Punct for Rustc<'_> {
519529 punct. ch
520530 }
521531 fn spacing ( & mut self , punct : Self :: Punct ) -> Spacing {
522- if punct. joint { Spacing :: Joint } else { Spacing :: Alone }
532+ if punct. joint {
533+ Spacing :: Joint
534+ } else {
535+ Spacing :: Alone
536+ }
523537 }
524538 fn span ( & mut self , punct : Self :: Punct ) -> Self :: Span {
525539 punct. span
0 commit comments