@@ -277,12 +277,6 @@ impl ToInternal<rustc_errors::Level> for Level {
277
277
278
278
pub struct FreeFunctions ;
279
279
280
- #[ derive( Clone ) ]
281
- pub struct TokenStreamIter {
282
- cursor : tokenstream:: Cursor ,
283
- stack : Vec < TokenTree < Group , Punct , Ident , Literal > > ,
284
- }
285
-
286
280
#[ derive( Clone ) ]
287
281
pub struct Group {
288
282
delimiter : Delimiter ,
@@ -382,8 +376,6 @@ impl<'a, 'b> Rustc<'a, 'b> {
382
376
impl server:: Types for Rustc < ' _ , ' _ > {
383
377
type FreeFunctions = FreeFunctions ;
384
378
type TokenStream = TokenStream ;
385
- type TokenStreamBuilder = tokenstream:: TokenStreamBuilder ;
386
- type TokenStreamIter = TokenStreamIter ;
387
379
type Group = Group ;
388
380
type Punct = Punct ;
389
381
type Ident = Ident ;
@@ -408,9 +400,6 @@ impl server::FreeFunctions for Rustc<'_, '_> {
408
400
}
409
401
410
402
impl server:: TokenStream for Rustc < ' _ , ' _ > {
411
- fn new ( & mut self ) -> Self :: TokenStream {
412
- TokenStream :: default ( )
413
- }
414
403
fn is_empty ( & mut self , stream : & Self :: TokenStream ) -> bool {
415
404
stream. is_empty ( )
416
405
}
@@ -481,53 +470,74 @@ impl server::TokenStream for Rustc<'_, '_> {
481
470
) -> Self :: TokenStream {
482
471
tree. to_internal ( )
483
472
}
484
- fn into_iter ( & mut self , stream : Self :: TokenStream ) -> Self :: TokenStreamIter {
485
- TokenStreamIter { cursor : stream. into_trees ( ) , stack : vec ! [ ] }
486
- }
487
- }
488
-
489
- impl server:: TokenStreamBuilder for Rustc < ' _ , ' _ > {
490
- fn new ( & mut self ) -> Self :: TokenStreamBuilder {
491
- tokenstream:: TokenStreamBuilder :: new ( )
492
- }
493
- fn push ( & mut self , builder : & mut Self :: TokenStreamBuilder , stream : Self :: TokenStream ) {
494
- builder. push ( stream) ;
473
+ fn concat_trees (
474
+ & mut self ,
475
+ base : Option < Self :: TokenStream > ,
476
+ trees : Vec < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > ,
477
+ ) -> Self :: TokenStream {
478
+ let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
479
+ if let Some ( base) = base {
480
+ builder. push ( base) ;
481
+ }
482
+ for tree in trees {
483
+ builder. push ( tree. to_internal ( ) ) ;
484
+ }
485
+ builder. build ( )
495
486
}
496
- fn build ( & mut self , builder : Self :: TokenStreamBuilder ) -> Self :: TokenStream {
487
+ fn concat_streams (
488
+ & mut self ,
489
+ base : Option < Self :: TokenStream > ,
490
+ streams : Vec < Self :: TokenStream > ,
491
+ ) -> Self :: TokenStream {
492
+ let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
493
+ if let Some ( base) = base {
494
+ builder. push ( base) ;
495
+ }
496
+ for stream in streams {
497
+ builder. push ( stream) ;
498
+ }
497
499
builder. build ( )
498
500
}
499
- }
500
-
501
- impl server:: TokenStreamIter for Rustc < ' _ , ' _ > {
502
- fn next (
501
+ fn into_iter (
503
502
& mut self ,
504
- iter : & mut Self :: TokenStreamIter ,
505
- ) -> Option < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > {
503
+ stream : Self :: TokenStream ,
504
+ ) -> Vec < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > {
505
+ // XXX: This is a raw port of the previous approach, and can probably be
506
+ // optimized.
507
+ let mut cursor = stream. into_trees ( ) ;
508
+ let mut stack = Vec :: new ( ) ;
509
+ let mut tts = Vec :: new ( ) ;
506
510
loop {
507
- let tree = iter. stack . pop ( ) . or_else ( || {
508
- let next = iter. cursor . next_with_spacing ( ) ?;
509
- Some ( TokenTree :: from_internal ( ( next, & mut iter. stack , self ) ) )
510
- } ) ?;
511
- // A hack used to pass AST fragments to attribute and derive macros
512
- // as a single nonterminal token instead of a token stream.
513
- // Such token needs to be "unwrapped" and not represented as a delimited group.
514
- // FIXME: It needs to be removed, but there are some compatibility issues (see #73345).
515
- if let TokenTree :: Group ( ref group) = tree {
516
- if group. flatten {
517
- iter. cursor . append ( group. stream . clone ( ) ) ;
518
- continue ;
511
+ let next = stack. pop ( ) . or_else ( || {
512
+ let next = cursor. next_with_spacing ( ) ?;
513
+ Some ( TokenTree :: from_internal ( ( next, & mut stack, self ) ) )
514
+ } ) ;
515
+ match next {
516
+ Some ( TokenTree :: Group ( group) ) => {
517
+ // A hack used to pass AST fragments to attribute and derive
518
+ // macros as a single nonterminal token instead of a token
519
+ // stream. Such token needs to be "unwrapped" and not
520
+ // represented as a delimited group.
521
+ // FIXME: It needs to be removed, but there are some
522
+ // compatibility issues (see #73345).
523
+ if group. flatten {
524
+ cursor. append ( group. stream ) ;
525
+ continue ;
526
+ }
527
+ tts. push ( TokenTree :: Group ( group) ) ;
519
528
}
529
+ Some ( tt) => tts. push ( tt) ,
530
+ None => return tts,
520
531
}
521
- return Some ( tree) ;
522
532
}
523
533
}
524
534
}
525
535
526
536
impl server:: Group for Rustc < ' _ , ' _ > {
527
- fn new ( & mut self , delimiter : Delimiter , stream : Self :: TokenStream ) -> Self :: Group {
537
+ fn new ( & mut self , delimiter : Delimiter , stream : Option < Self :: TokenStream > ) -> Self :: Group {
528
538
Group {
529
539
delimiter,
530
- stream,
540
+ stream : stream . unwrap_or_default ( ) ,
531
541
span : DelimSpan :: from_single ( server:: Span :: call_site ( self ) ) ,
532
542
flatten : false ,
533
543
}
0 commit comments