From eec6016ec3e0cc6be3ae75007586b6a56372b382 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:26:16 -0800 Subject: [PATCH 01/13] Delete unused Display for pretty printer Token --- compiler/rustc_ast_pretty/src/pp.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index bdd70148d85a0..8b7ea5d792dfb 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -137,7 +137,6 @@ mod ring; use ring::RingBuffer; use std::borrow::Cow; use std::collections::VecDeque; -use std::fmt; /// How to break. Described in more detail in the module docs. #[derive(Clone, Copy, PartialEq)] @@ -175,17 +174,6 @@ impl Token { } } -impl fmt::Display for Token { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - Token::String(ref s) => write!(f, "STR({},{})", s, s.len()), - Token::Break(_) => f.write_str("BREAK"), - Token::Begin(_) => f.write_str("BEGIN"), - Token::End => f.write_str("END"), - } - } -} - #[derive(Copy, Clone)] enum PrintStackBreak { Fits, From d81740ed2a63d377a725b0fbf935c391f5c7eb5e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:27:26 -0800 Subject: [PATCH 02/13] 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. --- compiler/rustc_ast_pretty/src/pp.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 8b7ea5d792dfb..0e3e7909afb84 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -266,7 +266,7 @@ impl Printer { self.buf.clear(); } let right = self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); - self.scan_stack.push_front(right); + self.scan_stack.push_back(right); } fn scan_end(&mut self) { @@ -274,7 +274,7 @@ impl Printer { self.print_end(); } else { let right = self.buf.push(BufEntry { token: Token::End, size: -1 }); - self.scan_stack.push_front(right); + self.scan_stack.push_back(right); } } @@ -287,7 +287,7 @@ impl Printer { self.check_stack(0); } let right = self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); - self.scan_stack.push_front(right); + self.scan_stack.push_back(right); self.right_total += b.blank_space; } @@ -304,8 +304,8 @@ impl Printer { fn check_stream(&mut self) { while self.right_total - self.left_total > self.space { - if *self.scan_stack.back().unwrap() == self.buf.index_of_first() { - self.scan_stack.pop_back().unwrap(); + if *self.scan_stack.front().unwrap() == self.buf.index_of_first() { + self.scan_stack.pop_front().unwrap(); self.buf.first_mut().unwrap().size = SIZE_INFINITY; } self.advance_left(); @@ -345,25 +345,25 @@ impl Printer { } fn check_stack(&mut self, mut k: usize) { - while let Some(&x) = self.scan_stack.front() { + while let Some(&x) = self.scan_stack.back() { let mut entry = &mut self.buf[x]; match entry.token { Token::Begin(_) => { if k == 0 { break; } - self.scan_stack.pop_front().unwrap(); + self.scan_stack.pop_back().unwrap(); entry.size += self.right_total; k -= 1; } Token::End => { // paper says + not =, but that makes no sense. - self.scan_stack.pop_front().unwrap(); + self.scan_stack.pop_back().unwrap(); entry.size = 1; k += 1; } _ => { - self.scan_stack.pop_front().unwrap(); + self.scan_stack.pop_back().unwrap(); entry.size += self.right_total; if k == 0 { break; From d981c5b354c40a6097c83a72173ae8a5569db2e1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:35:02 -0800 Subject: [PATCH 03/13] Eliminate a token clone from advance_left --- compiler/rustc_ast_pretty/src/pp.rs | 3 +-- compiler/rustc_ast_pretty/src/pp/ring.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 0e3e7909afb84..b93463e99fd5c 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -319,7 +319,7 @@ impl Printer { let mut left_size = self.buf.first().unwrap().size; while left_size >= 0 { - let left = self.buf.first().unwrap().token.clone(); + let left = self.buf.pop_first().unwrap().token; let len = match left { Token::Break(b) => b.blank_space, @@ -335,7 +335,6 @@ impl Printer { self.left_total += len; - self.buf.advance_left(); if self.buf.is_empty() { break; } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index d20142eb591fe..8187394fe30e0 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -32,11 +32,6 @@ impl RingBuffer { index } - pub fn advance_left(&mut self) { - self.data.pop_front().unwrap(); - self.offset += 1; - } - pub fn clear(&mut self) { self.data.clear(); } @@ -53,6 +48,12 @@ impl RingBuffer { self.data.front_mut() } + pub fn pop_first(&mut self) -> Option { + let first = self.data.pop_front()?; + self.offset += 1; + Some(first) + } + pub fn last(&self) -> Option<&T> { self.data.back() } From 351011ec3f043ff17e53f92a2f05e3e58e8e2bf6 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:36:29 -0800 Subject: [PATCH 04/13] Simplify left_total tracking --- compiler/rustc_ast_pretty/src/pp.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index b93463e99fd5c..583bdc616cb91 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -321,20 +321,14 @@ impl Printer { while left_size >= 0 { let left = self.buf.pop_first().unwrap().token; - let len = match left { - Token::Break(b) => b.blank_space, - Token::String(ref s) => { - let len = s.len() as isize; - assert_eq!(len, left_size); - len - } - _ => 0, - }; + match &left { + Token::Break(b) => self.left_total += b.blank_space, + Token::String(s) => self.left_total += s.len() as isize, + _ => {} + } self.print(left, left_size); - self.left_total += len; - if self.buf.is_empty() { break; } @@ -447,11 +441,7 @@ impl Printer { Token::Begin(b) => self.print_begin(*b, l), Token::End => self.print_end(), Token::Break(b) => self.print_break(*b, l), - Token::String(s) => { - let len = s.len() as isize; - assert_eq!(len, l); - self.print_string(s); - } + Token::String(s) => self.print_string(s), } self.last_printed = Some(token); } From d2eb46cfecf62210ee313da76acadedd7a2cbfcb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:37:45 -0800 Subject: [PATCH 05/13] Simplify advance_left --- compiler/rustc_ast_pretty/src/pp.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 583bdc616cb91..2c7962e44d05c 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -316,24 +316,20 @@ impl Printer { } fn advance_left(&mut self) { - let mut left_size = self.buf.first().unwrap().size; + while self.buf.first().unwrap().size >= 0 { + let left = self.buf.pop_first().unwrap(); - while left_size >= 0 { - let left = self.buf.pop_first().unwrap().token; - - match &left { + match &left.token { Token::Break(b) => self.left_total += b.blank_space, Token::String(s) => self.left_total += s.len() as isize, _ => {} } - self.print(left, left_size); + self.print(left.token, left.size); if self.buf.is_empty() { break; } - - left_size = self.buf.first().unwrap().size; } } From ae75ba692a456216139abe42e88b4ca45b973127 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:39:38 -0800 Subject: [PATCH 06/13] Inline print into advance_left --- compiler/rustc_ast_pretty/src/pp.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 2c7962e44d05c..1a6a7f2c52a71 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -325,7 +325,14 @@ impl Printer { _ => {} } - self.print(left.token, left.size); + match &left.token { + Token::Begin(b) => self.print_begin(*b, left.size), + Token::End => self.print_end(), + Token::Break(b) => self.print_break(*b, left.size), + Token::String(s) => self.print_string(s), + } + + self.last_printed = Some(left.token); if self.buf.is_empty() { break; @@ -432,16 +439,6 @@ impl Printer { self.out.push_str(s); } - fn print(&mut self, token: Token, l: isize) { - match &token { - Token::Begin(b) => self.print_begin(*b, l), - Token::End => self.print_end(), - Token::Break(b) => self.print_break(*b, l), - Token::String(s) => self.print_string(s), - } - self.last_printed = Some(token); - } - // Convenience functions to talk to the printer. /// "raw box" From ea23a1fac7d053a05c2d38bfb48e1692a4dc90a3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:41:22 -0800 Subject: [PATCH 07/13] Combine advance_left matches --- compiler/rustc_ast_pretty/src/pp.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 1a6a7f2c52a71..e1234d67ec418 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -320,16 +320,16 @@ impl Printer { let left = self.buf.pop_first().unwrap(); match &left.token { - Token::Break(b) => self.left_total += b.blank_space, - Token::String(s) => self.left_total += s.len() as isize, - _ => {} - } - - match &left.token { + Token::String(s) => { + self.left_total += s.len() as isize; + self.print_string(s); + } + Token::Break(b) => { + self.left_total += b.blank_space; + self.print_break(*b, left.size); + } Token::Begin(b) => self.print_begin(*b, left.size), Token::End => self.print_end(), - Token::Break(b) => self.print_break(*b, left.size), - Token::String(s) => self.print_string(s), } self.last_printed = Some(left.token); From d5f15a8c18a80cce04641e4801deee94b3a0bf45 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:44:17 -0800 Subject: [PATCH 08/13] Replace all single character variable names --- compiler/rustc_ast_pretty/src/pp.rs | 96 +++++++++++++++-------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index e1234d67ec418..dadfac06847f4 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -248,8 +248,8 @@ impl Printer { } /// Be very careful with this! - pub fn replace_last_token_still_buffered(&mut self, t: Token) { - self.buf.last_mut().unwrap().token = t; + pub fn replace_last_token_still_buffered(&mut self, token: Token) { + self.buf.last_mut().unwrap().token = token; } fn scan_eof(&mut self) { @@ -259,13 +259,13 @@ impl Printer { } } - fn scan_begin(&mut self, b: BeginToken) { + fn scan_begin(&mut self, token: BeginToken) { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; self.buf.clear(); } - let right = self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); + let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total }); self.scan_stack.push_back(right); } @@ -278,7 +278,7 @@ impl Printer { } } - fn scan_break(&mut self, b: BreakToken) { + fn scan_break(&mut self, token: BreakToken) { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; @@ -286,17 +286,17 @@ impl Printer { } else { self.check_stack(0); } - let right = self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); + let right = self.buf.push(BufEntry { token: Token::Break(token), size: -self.right_total }); self.scan_stack.push_back(right); - self.right_total += b.blank_space; + self.right_total += token.blank_space; } - fn scan_string(&mut self, s: Cow<'static, str>) { + fn scan_string(&mut self, string: Cow<'static, str>) { if self.scan_stack.is_empty() { - self.print_string(&s); + self.print_string(&string); } else { - let len = s.len() as isize; - self.buf.push(BufEntry { token: Token::String(s), size: len }); + let len = string.len() as isize; + self.buf.push(BufEntry { token: Token::String(string), size: len }); self.right_total += len; self.check_stream(); } @@ -320,15 +320,15 @@ impl Printer { let left = self.buf.pop_first().unwrap(); match &left.token { - Token::String(s) => { - self.left_total += s.len() as isize; - self.print_string(s); + Token::String(string) => { + self.left_total += string.len() as isize; + self.print_string(string); } - Token::Break(b) => { - self.left_total += b.blank_space; - self.print_break(*b, left.size); + Token::Break(token) => { + self.left_total += token.blank_space; + self.print_break(*token, left.size); } - Token::Begin(b) => self.print_begin(*b, left.size), + Token::Begin(token) => self.print_begin(*token, left.size), Token::End => self.print_end(), } @@ -340,28 +340,28 @@ impl Printer { } } - fn check_stack(&mut self, mut k: usize) { - while let Some(&x) = self.scan_stack.back() { - let mut entry = &mut self.buf[x]; + fn check_stack(&mut self, mut depth: usize) { + while let Some(&index) = self.scan_stack.back() { + let mut entry = &mut self.buf[index]; match entry.token { Token::Begin(_) => { - if k == 0 { + if depth == 0 { break; } self.scan_stack.pop_back().unwrap(); entry.size += self.right_total; - k -= 1; + depth -= 1; } Token::End => { // paper says + not =, but that makes no sense. self.scan_stack.pop_back().unwrap(); entry.size = 1; - k += 1; + depth += 1; } _ => { self.scan_stack.pop_back().unwrap(); entry.size += self.right_total; - if k == 0 { + if depth == 0 { break; } } @@ -385,11 +385,13 @@ impl Printer { }) } - fn print_begin(&mut self, b: BeginToken, l: isize) { - if l > self.space { - let col = self.margin - self.space + b.offset; - self.print_stack - .push(PrintStackElem { offset: col, pbreak: PrintStackBreak::Broken(b.breaks) }); + fn print_begin(&mut self, token: BeginToken, size: isize) { + if size > self.space { + let col = self.margin - self.space + token.offset; + self.print_stack.push(PrintStackElem { + offset: col, + pbreak: PrintStackBreak::Broken(token.breaks), + }); } else { self.print_stack.push(PrintStackElem { offset: 0, pbreak: PrintStackBreak::Fits }); } @@ -399,31 +401,31 @@ impl Printer { self.print_stack.pop().unwrap(); } - fn print_break(&mut self, b: BreakToken, l: isize) { + fn print_break(&mut self, token: BreakToken, size: isize) { let top = self.get_top(); match top.pbreak { PrintStackBreak::Fits => { - self.space -= b.blank_space; - self.indent(b.blank_space); + self.space -= token.blank_space; + self.indent(token.blank_space); } PrintStackBreak::Broken(Breaks::Consistent) => { - self.print_newline(top.offset + b.offset); - self.space = self.margin - (top.offset + b.offset); + self.print_newline(top.offset + token.offset); + self.space = self.margin - (top.offset + token.offset); } PrintStackBreak::Broken(Breaks::Inconsistent) => { - if l > self.space { - self.print_newline(top.offset + b.offset); - self.space = self.margin - (top.offset + b.offset); + if size > self.space { + self.print_newline(top.offset + token.offset); + self.space = self.margin - (top.offset + token.offset); } else { - self.indent(b.blank_space); - self.space -= b.blank_space; + self.indent(token.blank_space); + self.space -= token.blank_space; } } } } - fn print_string(&mut self, s: &str) { - let len = s.len() as isize; + fn print_string(&mut self, string: &str) { + let len = string.len() as isize; // assert!(len <= space); self.space -= len; @@ -436,14 +438,14 @@ impl Printer { self.out.reserve(self.pending_indentation as usize); self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize)); self.pending_indentation = 0; - self.out.push_str(s); + self.out.push_str(string); } // Convenience functions to talk to the printer. /// "raw box" - pub fn rbox(&mut self, indent: usize, b: Breaks) { - self.scan_begin(BeginToken { offset: indent as isize, breaks: b }) + pub fn rbox(&mut self, indent: usize, breaks: Breaks) { + self.scan_begin(BeginToken { offset: indent as isize, breaks }) } /// Inconsistent breaking box @@ -470,8 +472,8 @@ impl Printer { } pub fn word>>(&mut self, wrd: S) { - let s = wrd.into(); - self.scan_string(s) + let string = wrd.into(); + self.scan_string(string) } fn spaces(&mut self, n: usize) { From 65dd67096e6771a3bdf5b9b4a4cd638777a0ae89 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:46:49 -0800 Subject: [PATCH 09/13] Touch up print_string --- compiler/rustc_ast_pretty/src/pp.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index dadfac06847f4..11114b5322077 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -137,6 +137,7 @@ mod ring; use ring::RingBuffer; use std::borrow::Cow; use std::collections::VecDeque; +use std::iter; /// How to break. Described in more detail in the module docs. #[derive(Clone, Copy, PartialEq)] @@ -425,10 +426,6 @@ impl Printer { } fn print_string(&mut self, string: &str) { - let len = string.len() as isize; - // assert!(len <= space); - self.space -= len; - // Write the pending indent. A more concise way of doing this would be: // // write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?; @@ -436,9 +433,11 @@ impl Printer { // But that is significantly slower. This code is sufficiently hot, and indents can get // sufficiently large, that the difference is significant on some workloads. self.out.reserve(self.pending_indentation as usize); - self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize)); + self.out.extend(iter::repeat(' ').take(self.pending_indentation as usize)); self.pending_indentation = 0; + self.out.push_str(string); + self.space -= string.len() as isize; } // Convenience functions to talk to the printer. From 9e794d7de3adcf4e91b04e1a05d4f84c86b54f66 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:51:07 -0800 Subject: [PATCH 10/13] Eliminate offset number from Fits frames PrintStackElems with pbreak=PrintStackBreak::Fits always carried a meaningless value offset=0. We can combine the two types PrintStackElem + PrintStackBreak into one PrintFrame enum that stores offset only for Broken frames. --- compiler/rustc_ast_pretty/src/pp.rs | 47 ++++++++++++----------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 11114b5322077..de57820d2613d 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -176,15 +176,9 @@ impl Token { } #[derive(Copy, Clone)] -enum PrintStackBreak { +enum PrintFrame { Fits, - Broken(Breaks), -} - -#[derive(Copy, Clone)] -struct PrintStackElem { - offset: isize, - pbreak: PrintStackBreak, + Broken { offset: isize, breaks: Breaks }, } const SIZE_INFINITY: isize = 0xffff; @@ -209,7 +203,7 @@ pub struct Printer { /// advancing. scan_stack: VecDeque, /// Stack of blocks-in-progress being flushed by print - print_stack: Vec, + print_stack: Vec, /// Buffered indentation to avoid writing trailing whitespace pending_indentation: isize, /// The token most recently popped from the left boundary of the @@ -380,21 +374,19 @@ impl Printer { self.pending_indentation += amount; } - fn get_top(&self) -> PrintStackElem { - *self.print_stack.last().unwrap_or({ - &PrintStackElem { offset: 0, pbreak: PrintStackBreak::Broken(Breaks::Inconsistent) } - }) + fn get_top(&self) -> PrintFrame { + *self + .print_stack + .last() + .unwrap_or(&PrintFrame::Broken { offset: 0, breaks: Breaks::Inconsistent }) } fn print_begin(&mut self, token: BeginToken, size: isize) { if size > self.space { let col = self.margin - self.space + token.offset; - self.print_stack.push(PrintStackElem { - offset: col, - pbreak: PrintStackBreak::Broken(token.breaks), - }); + self.print_stack.push(PrintFrame::Broken { offset: col, breaks: token.breaks }); } else { - self.print_stack.push(PrintStackElem { offset: 0, pbreak: PrintStackBreak::Fits }); + self.print_stack.push(PrintFrame::Fits); } } @@ -403,20 +395,19 @@ impl Printer { } fn print_break(&mut self, token: BreakToken, size: isize) { - let top = self.get_top(); - match top.pbreak { - PrintStackBreak::Fits => { - self.space -= token.blank_space; + match self.get_top() { + PrintFrame::Fits => { self.indent(token.blank_space); + self.space -= token.blank_space; } - PrintStackBreak::Broken(Breaks::Consistent) => { - self.print_newline(top.offset + token.offset); - self.space = self.margin - (top.offset + token.offset); + PrintFrame::Broken { offset, breaks: Breaks::Consistent } => { + self.print_newline(offset + token.offset); + self.space = self.margin - (offset + token.offset); } - PrintStackBreak::Broken(Breaks::Inconsistent) => { + PrintFrame::Broken { offset, breaks: Breaks::Inconsistent } => { if size > self.space { - self.print_newline(top.offset + token.offset); - self.space = self.margin - (top.offset + token.offset); + self.print_newline(offset + token.offset); + self.space = self.margin - (offset + token.offset); } else { self.indent(token.blank_space); self.space -= token.blank_space; From 224536f4fee75a9825e7da0a8a332786cc1f52f8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:55:24 -0800 Subject: [PATCH 11/13] Inline indent function --- compiler/rustc_ast_pretty/src/pp.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index de57820d2613d..726c114b10b1f 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -366,12 +366,7 @@ impl Printer { fn print_newline(&mut self, amount: isize) { self.out.push('\n'); - self.pending_indentation = 0; - self.indent(amount); - } - - fn indent(&mut self, amount: isize) { - self.pending_indentation += amount; + self.pending_indentation = amount; } fn get_top(&self) -> PrintFrame { @@ -397,7 +392,7 @@ impl Printer { fn print_break(&mut self, token: BreakToken, size: isize) { match self.get_top() { PrintFrame::Fits => { - self.indent(token.blank_space); + self.pending_indentation += token.blank_space; self.space -= token.blank_space; } PrintFrame::Broken { offset, breaks: Breaks::Consistent } => { @@ -409,7 +404,7 @@ impl Printer { self.print_newline(offset + token.offset); self.space = self.margin - (offset + token.offset); } else { - self.indent(token.blank_space); + self.pending_indentation += token.blank_space; self.space -= token.blank_space; } } From 51eeb82d9db71bfb90ed6e0149d032787bcb3ee6 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:56:12 -0800 Subject: [PATCH 12/13] Inline print_newline function --- compiler/rustc_ast_pretty/src/pp.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 726c114b10b1f..9fc9282ac8056 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -364,11 +364,6 @@ impl Printer { } } - fn print_newline(&mut self, amount: isize) { - self.out.push('\n'); - self.pending_indentation = amount; - } - fn get_top(&self) -> PrintFrame { *self .print_stack @@ -396,12 +391,14 @@ impl Printer { self.space -= token.blank_space; } PrintFrame::Broken { offset, breaks: Breaks::Consistent } => { - self.print_newline(offset + token.offset); + self.out.push('\n'); + self.pending_indentation = offset + token.offset; self.space = self.margin - (offset + token.offset); } PrintFrame::Broken { offset, breaks: Breaks::Inconsistent } => { if size > self.space { - self.print_newline(offset + token.offset); + self.out.push('\n'); + self.pending_indentation = offset + token.offset; self.space = self.margin - (offset + token.offset); } else { self.pending_indentation += token.blank_space; From 21c1571e79e7f25f1a683befee33758c91693b6a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 18:58:33 -0800 Subject: [PATCH 13/13] Deduplicate branches of print_break implementation --- compiler/rustc_ast_pretty/src/pp.rs | 33 ++++++++++++----------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 9fc9282ac8056..82c40868d18f5 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -385,26 +385,21 @@ impl Printer { } fn print_break(&mut self, token: BreakToken, size: isize) { - match self.get_top() { - PrintFrame::Fits => { - self.pending_indentation += token.blank_space; - self.space -= token.blank_space; - } - PrintFrame::Broken { offset, breaks: Breaks::Consistent } => { - self.out.push('\n'); - self.pending_indentation = offset + token.offset; - self.space = self.margin - (offset + token.offset); - } - PrintFrame::Broken { offset, breaks: Breaks::Inconsistent } => { - if size > self.space { - self.out.push('\n'); - self.pending_indentation = offset + token.offset; - self.space = self.margin - (offset + token.offset); - } else { - self.pending_indentation += token.blank_space; - self.space -= token.blank_space; + let break_offset = + match self.get_top() { + PrintFrame::Fits => None, + PrintFrame::Broken { offset, breaks: Breaks::Consistent } => Some(offset), + PrintFrame::Broken { offset, breaks: Breaks::Inconsistent } => { + if size > self.space { Some(offset) } else { None } } - } + }; + if let Some(offset) = break_offset { + self.out.push('\n'); + self.pending_indentation = offset + token.offset; + self.space = self.margin - (offset + token.offset); + } else { + self.pending_indentation += token.blank_space; + self.space -= token.blank_space; } }