Skip to content

Commit

Permalink
Make Comments::next consume a comment.
Browse files Browse the repository at this point in the history
This avoids the need for a clone, fixing a FIXME comment.
  • Loading branch information
nnethercote committed May 13, 2024
1 parent 5e7a80b commit 74e1b46
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl PpAnn for NoAnn {}

pub struct Comments<'a> {
sm: &'a SourceMap,
comments: Vec<Comment>,
current: usize,
// Stored in reverse order so we can consume them by popping.
reversed_comments: Vec<Comment>,
}

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

impl<'a> Comments<'a> {
pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
let comments = gather_comments(sm, filename, input);
Comments { sm, comments, current: 0 }
let mut comments = gather_comments(sm, filename, input);
comments.reverse();
Comments { sm, reversed_comments: comments }
}

fn peek(&self) -> Option<&Comment> {
self.comments.get(self.current)
self.reversed_comments.last()
}

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

fn trailing_comment(
Expand Down

0 comments on commit 74e1b46

Please sign in to comment.