Skip to content

Commit 74e1b46

Browse files
committed
Make Comments::next consume a comment.
This avoids the need for a clone, fixing a FIXME comment.
1 parent 5e7a80b commit 74e1b46

File tree

1 file changed

+7
-9
lines changed
  • compiler/rustc_ast_pretty/src/pprust

1 file changed

+7
-9
lines changed

Diff for: compiler/rustc_ast_pretty/src/pprust/state.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl PpAnn for NoAnn {}
5555

5656
pub struct Comments<'a> {
5757
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>,
6060
}
6161

6262
/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
@@ -182,19 +182,17 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment>
182182

183183
impl<'a> Comments<'a> {
184184
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 }
187188
}
188189

189190
fn peek(&self) -> Option<&Comment> {
190-
self.comments.get(self.current)
191+
self.reversed_comments.last()
191192
}
192193

193-
// FIXME: This shouldn't probably clone lmao
194194
fn next(&mut self) -> Option<Comment> {
195-
let cmnt = self.comments.get(self.current).cloned();
196-
self.current += 1;
197-
cmnt
195+
self.reversed_comments.pop()
198196
}
199197

200198
fn trailing_comment(

0 commit comments

Comments
 (0)