1
Fork 0

Convert some functions to return Cow<'static,str> instead of String to reduce potential reallocations

This commit is contained in:
klensy 2021-08-22 19:55:45 +03:00
parent 2ad56d5c90
commit c565339c37
5 changed files with 53 additions and 51 deletions

View file

@ -8,17 +8,19 @@ use rustc_ast as ast;
use rustc_ast::token::{Nonterminal, Token, TokenKind}; use rustc_ast::token::{Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::tokenstream::{TokenStream, TokenTree};
use std::borrow::Cow;
pub fn nonterminal_to_string(nt: &Nonterminal) -> String { pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
State::new().nonterminal_to_string(nt) State::new().nonterminal_to_string(nt)
} }
/// Print the token kind precisely, without converting `$crate` into its respective crate name. /// Print the token kind precisely, without converting `$crate` into its respective crate name.
pub fn token_kind_to_string(tok: &TokenKind) -> String { 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)
} }
/// Print the token precisely, without converting `$crate` into its respective crate name. /// Print the token precisely, without converting `$crate` into its respective crate name.
pub fn token_to_string(token: &Token) -> String { pub fn token_to_string(token: &Token) -> Cow<'static, str> {
State::new().token_to_string(token) State::new().token_to_string(token)
} }

View file

@ -658,7 +658,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
} }
/// Print the token kind precisely, without converting `$crate` into its respective crate name. /// Print the token kind precisely, without converting `$crate` into its respective crate name.
fn token_kind_to_string(&self, tok: &TokenKind) -> String { fn token_kind_to_string(&self, tok: &TokenKind) -> Cow<'static, str> {
self.token_kind_to_string_ext(tok, None) self.token_kind_to_string_ext(tok, None)
} }
@ -666,72 +666,72 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
&self, &self,
tok: &TokenKind, tok: &TokenKind,
convert_dollar_crate: Option<Span>, convert_dollar_crate: Option<Span>,
) -> String { ) -> Cow<'static, str> {
match *tok { match *tok {
token::Eq => "=".to_string(), token::Eq => "=".into(),
token::Lt => "<".to_string(), token::Lt => "<".into(),
token::Le => "<=".to_string(), token::Le => "<=".into(),
token::EqEq => "==".to_string(), token::EqEq => "==".into(),
token::Ne => "!=".to_string(), token::Ne => "!=".into(),
token::Ge => ">=".to_string(), token::Ge => ">=".into(),
token::Gt => ">".to_string(), token::Gt => ">".into(),
token::Not => "!".to_string(), token::Not => "!".into(),
token::Tilde => "~".to_string(), token::Tilde => "~".into(),
token::OrOr => "||".to_string(), token::OrOr => "||".into(),
token::AndAnd => "&&".to_string(), token::AndAnd => "&&".into(),
token::BinOp(op) => binop_to_string(op).to_string(), token::BinOp(op) => binop_to_string(op).into(),
token::BinOpEq(op) => format!("{}=", binop_to_string(op)), token::BinOpEq(op) => format!("{}=", binop_to_string(op)).into(),
/* Structural symbols */ /* Structural symbols */
token::At => "@".to_string(), token::At => "@".into(),
token::Dot => ".".to_string(), token::Dot => ".".into(),
token::DotDot => "..".to_string(), token::DotDot => "..".into(),
token::DotDotDot => "...".to_string(), token::DotDotDot => "...".into(),
token::DotDotEq => "..=".to_string(), token::DotDotEq => "..=".into(),
token::Comma => ",".to_string(), token::Comma => ",".into(),
token::Semi => ";".to_string(), token::Semi => ";".into(),
token::Colon => ":".to_string(), token::Colon => ":".into(),
token::ModSep => "::".to_string(), token::ModSep => "::".into(),
token::RArrow => "->".to_string(), token::RArrow => "->".into(),
token::LArrow => "<-".to_string(), token::LArrow => "<-".into(),
token::FatArrow => "=>".to_string(), token::FatArrow => "=>".into(),
token::OpenDelim(token::Paren) => "(".to_string(), token::OpenDelim(token::Paren) => "(".into(),
token::CloseDelim(token::Paren) => ")".to_string(), token::CloseDelim(token::Paren) => ")".into(),
token::OpenDelim(token::Bracket) => "[".to_string(), token::OpenDelim(token::Bracket) => "[".into(),
token::CloseDelim(token::Bracket) => "]".to_string(), token::CloseDelim(token::Bracket) => "]".into(),
token::OpenDelim(token::Brace) => "{".to_string(), token::OpenDelim(token::Brace) => "{".into(),
token::CloseDelim(token::Brace) => "}".to_string(), token::CloseDelim(token::Brace) => "}".into(),
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".to_string(), token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".into(),
token::Pound => "#".to_string(), token::Pound => "#".into(),
token::Dollar => "$".to_string(), token::Dollar => "$".into(),
token::Question => "?".to_string(), token::Question => "?".into(),
token::SingleQuote => "'".to_string(), token::SingleQuote => "'".into(),
/* Literals */ /* Literals */
token::Literal(lit) => literal_to_string(lit), token::Literal(lit) => literal_to_string(lit).into(),
/* Name components */ /* Name components */
token::Ident(s, is_raw) => { token::Ident(s, is_raw) => {
IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string() IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string().into()
} }
token::Lifetime(s) => s.to_string(), token::Lifetime(s) => s.to_string().into(),
/* Other */ /* Other */
token::DocComment(comment_kind, attr_style, data) => { token::DocComment(comment_kind, attr_style, data) => {
doc_comment_to_string(comment_kind, attr_style, data) doc_comment_to_string(comment_kind, attr_style, data).into()
} }
token::Eof => "<eof>".to_string(), token::Eof => "<eof>".into(),
token::Interpolated(ref nt) => self.nonterminal_to_string(nt), token::Interpolated(ref nt) => self.nonterminal_to_string(nt).into(),
} }
} }
/// Print the token precisely, without converting `$crate` into its respective crate name. /// Print the token precisely, without converting `$crate` into its respective crate name.
fn token_to_string(&self, token: &Token) -> String { fn token_to_string(&self, token: &Token) -> Cow<'static, str> {
self.token_to_string_ext(token, false) self.token_to_string_ext(token, false)
} }
fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> String { fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
let convert_dollar_crate = convert_dollar_crate.then_some(token.span); 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_ext(&token.kind, convert_dollar_crate)
} }

View file

@ -1221,7 +1221,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String { fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
match *tt { match *tt {
mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token), mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token).into(),
mbe::TokenTree::MetaVar(_, name) => format!("${}", name), mbe::TokenTree::MetaVar(_, name) => format!("${}", name),
mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${}:{}", name, kind), mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${}:{}", name, kind),
mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${}:", name), mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${}:", name),

View file

@ -1528,7 +1528,7 @@ impl<'a> Parser<'a> {
.span_suggestion( .span_suggestion(
token.span, token.span,
"must have an integer part", "must have an integer part",
pprust::token_to_string(token), pprust::token_to_string(token).into(),
Applicability::MachineApplicable, Applicability::MachineApplicable,
) )
.emit(); .emit();

View file

@ -806,7 +806,7 @@ impl<'a> Parser<'a> {
.span_suggestion_short( .span_suggestion_short(
sp, sp,
&format!("missing `{}`", token_str), &format!("missing `{}`", token_str),
token_str, token_str.into(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
) )
.emit(); .emit();