Skip to content

Commit cc66a7f

Browse files
committed
Eliminate eof token state
1 parent 6e8b060 commit cc66a7f

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

compiler/rustc_ast_pretty/src/pp.rs

+24-27
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,9 @@ pub enum Token {
167167
Break(BreakToken),
168168
Begin(BeginToken),
169169
End,
170-
Eof,
171170
}
172171

173172
impl Token {
174-
crate fn is_eof(&self) -> bool {
175-
matches!(self, Token::Eof)
176-
}
177-
178173
pub fn is_hardbreak_tok(&self) -> bool {
179174
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
180175
}
@@ -187,7 +182,6 @@ impl fmt::Display for Token {
187182
Token::Break(_) => f.write_str("BREAK"),
188183
Token::Begin(_) => f.write_str("BEGIN"),
189184
Token::End => f.write_str("END"),
190-
Token::Eof => f.write_str("EOF"),
191185
}
192186
}
193187
}
@@ -233,6 +227,9 @@ pub struct Printer {
233227
print_stack: Vec<PrintStackElem>,
234228
/// Buffered indentation to avoid writing trailing whitespace
235229
pending_indentation: isize,
230+
/// The token most recently popped from the left boundary of the
231+
/// ring-buffer for printing
232+
last_printed: Option<Token>,
236233
}
237234

238235
#[derive(Clone)]
@@ -241,39 +238,36 @@ struct BufEntry {
241238
size: isize,
242239
}
243240

244-
impl Default for BufEntry {
245-
fn default() -> Self {
246-
BufEntry { token: Token::Eof, size: 0 }
247-
}
248-
}
249-
250241
impl Printer {
251242
pub fn new() -> Self {
252243
let linewidth = 78;
253-
let mut buf = RingBuffer::new();
254-
buf.advance_right();
255244
Printer {
256245
out: String::new(),
257246
margin: linewidth as isize,
258247
space: linewidth as isize,
259248
left: 0,
260249
right: 0,
261-
buf,
250+
buf: RingBuffer::new(),
262251
left_total: 0,
263252
right_total: 0,
264253
scan_stack: VecDeque::new(),
265254
print_stack: Vec::new(),
266255
pending_indentation: 0,
256+
last_printed: None,
267257
}
268258
}
269259

270-
pub fn last_token(&self) -> Token {
271-
self.buf[self.right].token.clone()
260+
pub fn last_token(&self) -> Option<&Token> {
261+
self.last_token_still_buffered().or_else(|| self.last_printed.as_ref())
262+
}
263+
264+
pub fn last_token_still_buffered(&self) -> Option<&Token> {
265+
self.buf.last().map(|last| &last.token)
272266
}
273267

274268
/// Be very careful with this!
275-
pub fn replace_last_token(&mut self, t: Token) {
276-
self.buf[self.right].token = t;
269+
pub fn replace_last_token_still_buffered(&mut self, t: Token) {
270+
self.buf.last_mut().unwrap().token = t;
277271
}
278272

279273
fn scan_eof(&mut self) {
@@ -323,7 +317,7 @@ impl Printer {
323317

324318
fn scan_string(&mut self, s: Cow<'static, str>) {
325319
if self.scan_stack.is_empty() {
326-
self.print_string(s);
320+
self.print_string(&s);
327321
} else {
328322
self.right += 1;
329323
let len = s.len() as isize;
@@ -459,7 +453,7 @@ impl Printer {
459453
}
460454
}
461455

462-
fn print_string(&mut self, s: Cow<'static, str>) {
456+
fn print_string(&mut self, s: &str) {
463457
let len = s.len() as isize;
464458
// assert!(len <= space);
465459
self.space -= len;
@@ -473,21 +467,21 @@ impl Printer {
473467
self.out.reserve(self.pending_indentation as usize);
474468
self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
475469
self.pending_indentation = 0;
476-
self.out.push_str(&s);
470+
self.out.push_str(s);
477471
}
478472

479473
fn print(&mut self, token: Token, l: isize) {
480-
match token {
481-
Token::Begin(b) => self.print_begin(b, l),
474+
match &token {
475+
Token::Begin(b) => self.print_begin(*b, l),
482476
Token::End => self.print_end(),
483-
Token::Break(b) => self.print_break(b, l),
477+
Token::Break(b) => self.print_break(*b, l),
484478
Token::String(s) => {
485479
let len = s.len() as isize;
486480
assert_eq!(len, l);
487481
self.print_string(s);
488482
}
489-
Token::Eof => panic!(), // Eof should never get here.
490483
}
484+
self.last_printed = Some(token);
491485
}
492486

493487
// Convenience functions to talk to the printer.
@@ -542,7 +536,10 @@ impl Printer {
542536
}
543537

544538
pub fn is_beginning_of_line(&self) -> bool {
545-
self.last_token().is_eof() || self.last_token().is_hardbreak_tok()
539+
match self.last_token() {
540+
Some(last_token) => last_token.is_hardbreak_tok(),
541+
None => true,
542+
}
546543
}
547544

548545
pub fn hardbreak_tok_offset(off: isize) -> Token {

compiler/rustc_ast_pretty/src/pp/ring.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ impl<T> RingBuffer<T> {
2626
self.data.push_back(value);
2727
}
2828

29-
pub fn advance_right(&mut self)
30-
where
31-
T: Default,
32-
{
33-
self.data.push_back(T::default());
34-
}
35-
3629
pub fn advance_left(&mut self) {
3730
self.data.pop_front().unwrap();
3831
self.offset += 1;
@@ -41,6 +34,14 @@ impl<T> RingBuffer<T> {
4134
pub fn clear(&mut self) {
4235
self.data.clear();
4336
}
37+
38+
pub fn last(&self) -> Option<&T> {
39+
self.data.back()
40+
}
41+
42+
pub fn last_mut(&mut self) -> Option<&mut T> {
43+
self.data.back_mut()
44+
}
4445
}
4546

4647
impl<T> Index<usize> for RingBuffer<T> {

compiler/rustc_ast_pretty/src/pprust/state.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
329329
CommentStyle::BlankLine => {
330330
// We need to do at least one, possibly two hardbreaks.
331331
let twice = match self.last_token() {
332-
pp::Token::String(s) => ";" == s,
333-
pp::Token::Begin(_) => true,
334-
pp::Token::End => true,
332+
Some(pp::Token::String(s)) => ";" == s,
333+
Some(pp::Token::Begin(_)) => true,
334+
Some(pp::Token::End) => true,
335335
_ => false,
336336
};
337337
if twice {
@@ -687,11 +687,15 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
687687
fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
688688
if !self.is_beginning_of_line() {
689689
self.break_offset(n, off)
690-
} else if off != 0 && self.last_token().is_hardbreak_tok() {
691-
// We do something pretty sketchy here: tuck the nonzero
692-
// offset-adjustment we were going to deposit along with the
693-
// break into the previous hardbreak.
694-
self.replace_last_token(pp::Printer::hardbreak_tok_offset(off));
690+
} else if off != 0 {
691+
if let Some(last_token) = self.last_token_still_buffered() {
692+
if last_token.is_hardbreak_tok() {
693+
// We do something pretty sketchy here: tuck the nonzero
694+
// offset-adjustment we were going to deposit along with the
695+
// break into the previous hardbreak.
696+
self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off));
697+
}
698+
}
695699
}
696700
}
697701

0 commit comments

Comments
 (0)