@@ -55,8 +55,8 @@ impl PpAnn for NoAnn {}
55
55
56
56
pub struct Comments < ' a > {
57
57
sm : & ' a SourceMap ,
58
- comments : Vec < Comment > ,
59
- current : usize ,
58
+ // Stored in reverse order so we can consume them by popping.
59
+ reversed_comments : Vec < Comment > ,
60
60
}
61
61
62
62
/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
@@ -182,29 +182,33 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment>
182
182
183
183
impl < ' a > Comments < ' a > {
184
184
pub fn new ( sm : & ' a SourceMap , filename : FileName , input : String ) -> Comments < ' a > {
185
- let comments = gather_comments ( sm, filename, input) ;
186
- Comments { sm, comments, current : 0 }
185
+ let mut comments = gather_comments ( sm, filename, input) ;
186
+ comments. reverse ( ) ;
187
+ Comments { sm, reversed_comments : comments }
187
188
}
188
189
189
- // FIXME: This shouldn't probably clone lmao
190
- fn next ( & self ) -> Option < Comment > {
191
- self . comments . get ( self . current ) . cloned ( )
190
+ fn peek ( & self ) -> Option < & Comment > {
191
+ self . reversed_comments . last ( )
192
+ }
193
+
194
+ fn next ( & mut self ) -> Option < Comment > {
195
+ self . reversed_comments . pop ( )
192
196
}
193
197
194
198
fn trailing_comment (
195
- & self ,
199
+ & mut self ,
196
200
span : rustc_span:: Span ,
197
201
next_pos : Option < BytePos > ,
198
202
) -> Option < Comment > {
199
- if let Some ( cmnt) = self . next ( ) {
203
+ if let Some ( cmnt) = self . peek ( ) {
200
204
if cmnt. style != CommentStyle :: Trailing {
201
205
return None ;
202
206
}
203
207
let span_line = self . sm . lookup_char_pos ( span. hi ( ) ) ;
204
208
let comment_line = self . sm . lookup_char_pos ( cmnt. pos ) ;
205
209
let next = next_pos. unwrap_or_else ( || cmnt. pos + BytePos ( 1 ) ) ;
206
210
if span. hi ( ) < cmnt. pos && cmnt. pos < next && span_line. line == comment_line. line {
207
- return Some ( cmnt ) ;
211
+ return Some ( self . next ( ) . unwrap ( ) ) ;
208
212
}
209
213
}
210
214
@@ -400,7 +404,8 @@ impl std::ops::DerefMut for State<'_> {
400
404
401
405
/// This trait is used for both AST and HIR pretty-printing.
402
406
pub trait PrintState < ' a > : std:: ops:: Deref < Target = pp:: Printer > + std:: ops:: DerefMut {
403
- fn comments ( & mut self ) -> & mut Option < Comments < ' a > > ;
407
+ fn comments ( & self ) -> Option < & Comments < ' a > > ;
408
+ fn comments_mut ( & mut self ) -> Option < & mut Comments < ' a > > ;
404
409
fn ann_post ( & mut self , ident : Ident ) ;
405
410
fn print_generic_args ( & mut self , args : & ast:: GenericArgs , colons_before_params : bool ) ;
406
411
@@ -442,18 +447,18 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
442
447
443
448
fn maybe_print_comment ( & mut self , pos : BytePos ) -> bool {
444
449
let mut has_comment = false ;
445
- while let Some ( cmnt) = self . next_comment ( ) {
446
- if cmnt. pos < pos {
447
- has_comment = true ;
448
- self . print_comment ( & cmnt) ;
449
- } else {
450
+ while let Some ( cmnt) = self . peek_comment ( ) {
451
+ if cmnt. pos >= pos {
450
452
break ;
451
453
}
454
+ has_comment = true ;
455
+ let cmnt = self . next_comment ( ) . unwrap ( ) ;
456
+ self . print_comment ( cmnt) ;
452
457
}
453
458
has_comment
454
459
}
455
460
456
- fn print_comment ( & mut self , cmnt : & Comment ) {
461
+ fn print_comment ( & mut self , cmnt : Comment ) {
457
462
match cmnt. style {
458
463
CommentStyle :: Mixed => {
459
464
if !self . is_beginning_of_line ( ) {
@@ -517,31 +522,32 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
517
522
self . hardbreak ( ) ;
518
523
}
519
524
}
520
- if let Some ( cmnts) = self . comments ( ) {
521
- cmnts. current += 1 ;
522
- }
525
+ }
526
+
527
+ fn peek_comment < ' b > ( & ' b self ) -> Option < & ' b Comment > where ' a : ' b {
528
+ self . comments ( ) . and_then ( |c| c. peek ( ) )
523
529
}
524
530
525
531
fn next_comment ( & mut self ) -> Option < Comment > {
526
- self . comments ( ) . as_mut ( ) . and_then ( |c| c. next ( ) )
532
+ self . comments_mut ( ) . and_then ( |c| c. next ( ) )
527
533
}
528
534
529
535
fn maybe_print_trailing_comment ( & mut self , span : rustc_span:: Span , next_pos : Option < BytePos > ) {
530
- if let Some ( cmnts) = self . comments ( ) {
536
+ if let Some ( cmnts) = self . comments_mut ( ) {
531
537
if let Some ( cmnt) = cmnts. trailing_comment ( span, next_pos) {
532
- self . print_comment ( & cmnt) ;
538
+ self . print_comment ( cmnt) ;
533
539
}
534
540
}
535
541
}
536
542
537
543
fn print_remaining_comments ( & mut self ) {
538
544
// If there aren't any remaining comments, then we need to manually
539
545
// make sure there is a line break at the end.
540
- if self . next_comment ( ) . is_none ( ) {
546
+ if self . peek_comment ( ) . is_none ( ) {
541
547
self . hardbreak ( ) ;
542
548
}
543
549
while let Some ( cmnt) = self . next_comment ( ) {
544
- self . print_comment ( & cmnt)
550
+ self . print_comment ( cmnt)
545
551
}
546
552
}
547
553
@@ -994,8 +1000,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
994
1000
}
995
1001
996
1002
impl < ' a > PrintState < ' a > for State < ' a > {
997
- fn comments ( & mut self ) -> & mut Option < Comments < ' a > > {
998
- & mut self . comments
1003
+ fn comments ( & self ) -> Option < & Comments < ' a > > {
1004
+ self . comments . as_ref ( )
1005
+ }
1006
+
1007
+ fn comments_mut ( & mut self ) -> Option < & mut Comments < ' a > > {
1008
+ self . comments . as_mut ( )
999
1009
}
1000
1010
1001
1011
fn ann_post ( & mut self , ident : Ident ) {
0 commit comments