@@ -284,12 +284,6 @@ impl ToInternal<rustc_errors::Level> for Level {
284
284
285
285
pub struct FreeFunctions ;
286
286
287
- #[ derive( Clone ) ]
288
- pub struct TokenStreamIter {
289
- cursor : tokenstream:: Cursor ,
290
- stack : Vec < TokenTree < Group , Punct , Ident , Literal > > ,
291
- }
292
-
293
287
#[ derive( Clone ) ]
294
288
pub struct Group {
295
289
delimiter : Delimiter ,
@@ -398,8 +392,6 @@ impl<'a> Rustc<'a> {
398
392
impl server:: Types for Rustc < ' _ > {
399
393
type FreeFunctions = FreeFunctions ;
400
394
type TokenStream = TokenStream ;
401
- type TokenStreamBuilder = tokenstream:: TokenStreamBuilder ;
402
- type TokenStreamIter = TokenStreamIter ;
403
395
type Group = Group ;
404
396
type Punct = Punct ;
405
397
type Ident = Ident ;
@@ -417,9 +409,6 @@ impl server::FreeFunctions for Rustc<'_> {
417
409
}
418
410
419
411
impl server:: TokenStream for Rustc < ' _ > {
420
- fn new ( & mut self ) -> Self :: TokenStream {
421
- TokenStream :: default ( )
422
- }
423
412
fn is_empty ( & mut self , stream : & Self :: TokenStream ) -> bool {
424
413
stream. is_empty ( )
425
414
}
@@ -440,53 +429,74 @@ impl server::TokenStream for Rustc<'_> {
440
429
) -> Self :: TokenStream {
441
430
tree. to_internal ( )
442
431
}
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 ( )
454
445
}
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
+ }
456
458
builder. build ( )
457
459
}
458
- }
459
-
460
- impl server:: TokenStreamIter for Rustc < ' _ > {
461
- fn next (
460
+ fn into_iter (
462
461
& 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 ( ) ;
465
469
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) ) ;
478
487
}
488
+ Some ( tt) => tts. push ( tt) ,
489
+ None => return tts,
479
490
}
480
- return Some ( tree) ;
481
491
}
482
492
}
483
493
}
484
494
485
495
impl 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 {
487
497
Group {
488
498
delimiter,
489
- stream,
499
+ stream : stream . unwrap_or_default ( ) ,
490
500
span : DelimSpan :: from_single ( server:: Context :: call_site ( self ) ) ,
491
501
flatten : false ,
492
502
}
@@ -519,7 +529,11 @@ impl server::Punct for Rustc<'_> {
519
529
punct. ch
520
530
}
521
531
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
+ }
523
537
}
524
538
fn span ( & mut self , punct : Self :: Punct ) -> Self :: Span {
525
539
punct. span
0 commit comments