diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-13 10:06:27 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-13 10:15:30 +1000 |
| commit | 74e1b46ab23fe1f2deb414968340ca35e7882ae0 (patch) | |
| tree | ee65229cf5c50d34ab89279304e266ecbd24d837 | |
| parent | 5e7a80b2d2462060e81295ea6a044f7012bba8cb (diff) | |
| download | rust-74e1b46ab23fe1f2deb414968340ca35e7882ae0.tar.gz rust-74e1b46ab23fe1f2deb414968340ca35e7882ae0.zip | |
Make `Comments::next` consume a comment.
This avoids the need for a clone, fixing a FIXME comment.
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state.rs | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 9eb552cdcdf..329c2167d55 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -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. @@ -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( |
