Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jul 11, 2019
1 parent af26e7f commit e381065
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -187,8 +187,9 @@ pub fn literal_to_string(lit: token::Lit) -> String {
out
}

fn ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
ident_to_string_ext(ident.name, is_raw, Some(ident.span))
/// Print an ident from AST, `$crate` is converted into its respective crate name.
fn ast_ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
ident_to_string(ident.name, is_raw, Some(ident.span))
}

// AST pretty-printer is used as a fallback for turning AST structures into token streams for
Expand All @@ -202,9 +203,7 @@ fn ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
// but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
// so we should not perform this lossy conversion if the top level call to the pretty-printer was
// done for a token stream or a single token.
fn ident_to_string_ext(
name: ast::Name, is_raw: bool, convert_dollar_crate: Option<Span>
) -> String {
fn ident_to_string(name: ast::Name, is_raw: bool, convert_dollar_crate: Option<Span>) -> String {
if is_raw {
format!("r#{}", name)
} else {
Expand All @@ -222,6 +221,7 @@ fn ident_to_string_ext(
}
}

/// Print the token kind precisely, without converting `$crate` into its respective crate name.
pub fn token_kind_to_string(tok: &TokenKind) -> String {
token_kind_to_string_ext(tok, None)
}
Expand Down Expand Up @@ -272,7 +272,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
token::Literal(lit) => literal_to_string(lit),

/* Name components */
token::Ident(s, is_raw) => ident_to_string_ext(s, is_raw, convert_dollar_crate),
token::Ident(s, is_raw) => ident_to_string(s, is_raw, convert_dollar_crate),
token::Lifetime(s) => s.to_string(),

/* Other */
Expand All @@ -286,6 +286,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
}
}

/// Print the token precisely, without converting `$crate` into its respective crate name.
pub fn token_to_string(token: &Token) -> String {
token_to_string_ext(token, false)
}
Expand All @@ -305,7 +306,7 @@ crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
token::NtBlock(ref e) => block_to_string(e),
token::NtStmt(ref e) => stmt_to_string(e),
token::NtPat(ref e) => pat_to_string(e),
token::NtIdent(e, is_raw) => ident_to_string(e, is_raw),
token::NtIdent(e, is_raw) => ast_ident_to_string(e, is_raw),
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(ref e) => expr_to_string(e),
token::NtTT(ref tree) => tt_to_string(tree.clone()),
Expand Down Expand Up @@ -601,7 +602,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
self.word("::");
}
if segment.ident.name != kw::PathRoot {
self.word(ident_to_string(segment.ident, segment.ident.is_raw_guess()));
self.word(ast_ident_to_string(segment.ident, segment.ident.is_raw_guess()));
}
}
}
Expand Down Expand Up @@ -2205,7 +2206,7 @@ impl<'a> State<'a> {
}

crate fn print_ident(&mut self, ident: ast::Ident) {
self.s.word(ident_to_string(ident, ident.is_raw_guess()));
self.s.word(ast_ident_to_string(ident, ident.is_raw_guess()));
self.ann.post(self, AnnNode::Ident(&ident))
}

Expand Down

0 comments on commit e381065

Please sign in to comment.