Skip to content

Commit

Permalink
Auto merge of rust-lang#117928 - nnethercote:rustc_ast_pretty, r=fee1…
Browse files Browse the repository at this point in the history
…-dead

`rustc_ast_pretty` cleanups

Some improvements I found while looking at this code.

r? `@fee1-dead`
  • Loading branch information
bors committed Nov 22, 2023
2 parents 739d556 + 6686221 commit cc4bb0d
Show file tree
Hide file tree
Showing 27 changed files with 183 additions and 240 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2060,9 +2060,9 @@ dependencies = [

[[package]]
name = "itertools"
version = "0.10.5"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
Expand Down Expand Up @@ -3471,6 +3471,7 @@ dependencies = [
name = "rustc_ast_pretty"
version = "0.0.0"
dependencies = [
"itertools",
"rustc_ast",
"rustc_span",
"thin-vec",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
itertools = "0.10.1"
itertools = "0.11"
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_attr = { path = "../rustc_attr" }
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_pretty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
itertools = "0.11"
rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
thin-vec = "0.2.12"
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#![doc(rust_logo)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
#![feature(associated_type_bounds)]
#![feature(box_patterns)]
#![feature(with_negative_coherence)]
#![recursion_limit = "256"]

mod helpers;
Expand Down
17 changes: 8 additions & 9 deletions compiler/rustc_ast_pretty/src/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,20 @@ enum IndentStyle {
}

#[derive(Clone, Copy, Default, PartialEq)]
pub struct BreakToken {
pub(crate) struct BreakToken {
offset: isize,
blank_space: isize,
pre_break: Option<char>,
}

#[derive(Clone, Copy, PartialEq)]
pub struct BeginToken {
pub(crate) struct BeginToken {
indent: IndentStyle,
breaks: Breaks,
}

#[derive(Clone, PartialEq)]
pub enum Token {
#[derive(PartialEq)]
pub(crate) enum Token {
// In practice a string token contains either a `&'static str` or a
// `String`. `Cow` is overkill for this because we never modify the data,
// but it's more convenient than rolling our own more specialized type.
Expand Down Expand Up @@ -229,7 +229,6 @@ pub struct Printer {
last_printed: Option<Token>,
}

#[derive(Clone)]
struct BufEntry {
token: Token,
size: isize,
Expand All @@ -251,16 +250,16 @@ impl Printer {
}
}

pub fn last_token(&self) -> Option<&Token> {
pub(crate) fn last_token(&self) -> Option<&Token> {
self.last_token_still_buffered().or_else(|| self.last_printed.as_ref())
}

pub fn last_token_still_buffered(&self) -> Option<&Token> {
pub(crate) fn last_token_still_buffered(&self) -> Option<&Token> {
self.buf.last().map(|last| &last.token)
}

/// Be very careful with this!
pub fn replace_last_token_still_buffered(&mut self, token: Token) {
pub(crate) fn replace_last_token_still_buffered(&mut self, token: Token) {
self.buf.last_mut().unwrap().token = token;
}

Expand Down Expand Up @@ -314,7 +313,7 @@ impl Printer {
}
}

pub fn offset(&mut self, offset: isize) {
pub(crate) fn offset(&mut self, offset: isize) {
if let Some(BufEntry { token: Token::Break(token), .. }) = &mut self.buf.last_mut() {
token.offset += offset;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pp/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Printer {
}
}

pub fn hardbreak_tok_offset(off: isize) -> Token {
pub(crate) fn hardbreak_tok_offset(off: isize) -> Token {
Token::Break(BreakToken {
offset: off,
blank_space: SIZE_INFINITY,
Expand Down
Loading

0 comments on commit cc4bb0d

Please sign in to comment.