Skip to content

Commit

Permalink
Streamline token printing functions.
Browse files Browse the repository at this point in the history
Remove `PrintState::token{,_kind}_to_string`, because they're very thin
wrappers for `PrintState::token{,_kind}_to_string_ext` that obscure more
than they clarify. And rename the latter two methods.
  • Loading branch information
nnethercote committed Jun 25, 2024
1 parent 2e4d547 commit c2f2b2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {

/// Print the token kind precisely, without converting `$crate` into its respective crate name.
pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {
State::new().token_kind_to_string(tok)
State::new().token_kind_to_string(tok, None)
}

/// Print the token precisely, without converting `$crate` into its respective crate name.
pub fn token_to_string(token: &Token) -> Cow<'static, str> {
State::new().token_to_string(token)
State::new().token_to_string(token, false)
}

pub fn ty_to_string(ty: &ast::Ty) -> String {
Expand Down
22 changes: 6 additions & 16 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) -> Spacing {
match tt {
TokenTree::Token(token, spacing) => {
let token_str = self.token_to_string_ext(token, convert_dollar_crate);
let token_str = self.token_to_string(token, convert_dollar_crate);
self.word(token_str);
if let token::DocComment(..) = token.kind {
self.hardbreak()
Expand Down Expand Up @@ -770,12 +770,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.bclose(span, empty);
}
delim => {
let token_str = self.token_kind_to_string(&token::OpenDelim(delim));
let token_str = self.token_kind_to_string(&token::OpenDelim(delim), None);
self.word(token_str);
self.ibox(0);
self.print_tts(tts, convert_dollar_crate);
self.end();
let token_str = self.token_kind_to_string(&token::CloseDelim(delim));
let token_str = self.token_kind_to_string(&token::CloseDelim(delim), None);
self.word(token_str);
}
}
Expand Down Expand Up @@ -884,12 +884,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.tts_to_string(&TokenStream::from_nonterminal_ast(nt))
}

/// Print the token kind precisely, without converting `$crate` into its respective crate name.
fn token_kind_to_string(&self, tok: &TokenKind) -> Cow<'static, str> {
self.token_kind_to_string_ext(tok, None)
}

fn token_kind_to_string_ext(
fn token_kind_to_string(
&self,
tok: &TokenKind,
convert_dollar_crate: Option<Span>,
Expand Down Expand Up @@ -959,14 +954,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

/// Print the token precisely, without converting `$crate` into its respective crate name.
fn token_to_string(&self, token: &Token) -> Cow<'static, str> {
self.token_to_string_ext(token, false)
}

fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
fn token_to_string(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
self.token_kind_to_string_ext(&token.kind, convert_dollar_crate)
self.token_kind_to_string(&token.kind, convert_dollar_crate)
}

fn ty_to_string(&self, ty: &ast::Ty) -> String {
Expand Down

0 comments on commit c2f2b2d

Please sign in to comment.