Skip to content

Commit d81740e

Browse files
committed
Grow scan_stack in the conventional direction
The pretty printer algorithm involves 2 VecDeques: a ring-buffer of tokens and a deque of ring-buffer indices. Confusingly, those two deques were being grown in opposite directions for no good reason. Ring-buffer pushes would go on the "back" of the ring-buffer (i.e. higher indices) while scan_stack pushes would go on the "front" (i.e. lower indices). This commit flips the scan_stack accesses to grow the scan_stack and ring-buffer in the same direction, where push does the same operation as a Vec push i.e. inserting on the high-index end.
1 parent eec6016 commit d81740e

File tree

1 file changed

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

1 file changed

+9
-9
lines changed

compiler/rustc_ast_pretty/src/pp.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,15 @@ impl Printer {
266266
self.buf.clear();
267267
}
268268
let right = self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total });
269-
self.scan_stack.push_front(right);
269+
self.scan_stack.push_back(right);
270270
}
271271

272272
fn scan_end(&mut self) {
273273
if self.scan_stack.is_empty() {
274274
self.print_end();
275275
} else {
276276
let right = self.buf.push(BufEntry { token: Token::End, size: -1 });
277-
self.scan_stack.push_front(right);
277+
self.scan_stack.push_back(right);
278278
}
279279
}
280280

@@ -287,7 +287,7 @@ impl Printer {
287287
self.check_stack(0);
288288
}
289289
let right = self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total });
290-
self.scan_stack.push_front(right);
290+
self.scan_stack.push_back(right);
291291
self.right_total += b.blank_space;
292292
}
293293

@@ -304,8 +304,8 @@ impl Printer {
304304

305305
fn check_stream(&mut self) {
306306
while self.right_total - self.left_total > self.space {
307-
if *self.scan_stack.back().unwrap() == self.buf.index_of_first() {
308-
self.scan_stack.pop_back().unwrap();
307+
if *self.scan_stack.front().unwrap() == self.buf.index_of_first() {
308+
self.scan_stack.pop_front().unwrap();
309309
self.buf.first_mut().unwrap().size = SIZE_INFINITY;
310310
}
311311
self.advance_left();
@@ -345,25 +345,25 @@ impl Printer {
345345
}
346346

347347
fn check_stack(&mut self, mut k: usize) {
348-
while let Some(&x) = self.scan_stack.front() {
348+
while let Some(&x) = self.scan_stack.back() {
349349
let mut entry = &mut self.buf[x];
350350
match entry.token {
351351
Token::Begin(_) => {
352352
if k == 0 {
353353
break;
354354
}
355-
self.scan_stack.pop_front().unwrap();
355+
self.scan_stack.pop_back().unwrap();
356356
entry.size += self.right_total;
357357
k -= 1;
358358
}
359359
Token::End => {
360360
// paper says + not =, but that makes no sense.
361-
self.scan_stack.pop_front().unwrap();
361+
self.scan_stack.pop_back().unwrap();
362362
entry.size = 1;
363363
k += 1;
364364
}
365365
_ => {
366-
self.scan_stack.pop_front().unwrap();
366+
self.scan_stack.pop_back().unwrap();
367367
entry.size += self.right_total;
368368
if k == 0 {
369369
break;

0 commit comments

Comments
 (0)