Skip to content

Commit c565339

Browse files
committed
Convert some functions to return Cow<'static,str> instead of String to reduce potential reallocations
1 parent 2ad56d5 commit c565339

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

compiler/rustc_ast_pretty/src/pprust/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ use rustc_ast as ast;
88
use rustc_ast::token::{Nonterminal, Token, TokenKind};
99
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1010

11+
use std::borrow::Cow;
12+
1113
pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
1214
State::new().nonterminal_to_string(nt)
1315
}
1416

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

2022
/// Print the token precisely, without converting `$crate` into its respective crate name.
21-
pub fn token_to_string(token: &Token) -> String {
23+
pub fn token_to_string(token: &Token) -> Cow<'static, str> {
2224
State::new().token_to_string(token)
2325
}
2426

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -658,80 +658,80 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
658658
}
659659

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

665665
fn token_kind_to_string_ext(
666666
&self,
667667
tok: &TokenKind,
668668
convert_dollar_crate: Option<Span>,
669-
) -> String {
669+
) -> Cow<'static, str> {
670670
match *tok {
671-
token::Eq => "=".to_string(),
672-
token::Lt => "<".to_string(),
673-
token::Le => "<=".to_string(),
674-
token::EqEq => "==".to_string(),
675-
token::Ne => "!=".to_string(),
676-
token::Ge => ">=".to_string(),
677-
token::Gt => ">".to_string(),
678-
token::Not => "!".to_string(),
679-
token::Tilde => "~".to_string(),
680-
token::OrOr => "||".to_string(),
681-
token::AndAnd => "&&".to_string(),
682-
token::BinOp(op) => binop_to_string(op).to_string(),
683-
token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
671+
token::Eq => "=".into(),
672+
token::Lt => "<".into(),
673+
token::Le => "<=".into(),
674+
token::EqEq => "==".into(),
675+
token::Ne => "!=".into(),
676+
token::Ge => ">=".into(),
677+
token::Gt => ">".into(),
678+
token::Not => "!".into(),
679+
token::Tilde => "~".into(),
680+
token::OrOr => "||".into(),
681+
token::AndAnd => "&&".into(),
682+
token::BinOp(op) => binop_to_string(op).into(),
683+
token::BinOpEq(op) => format!("{}=", binop_to_string(op)).into(),
684684

685685
/* Structural symbols */
686-
token::At => "@".to_string(),
687-
token::Dot => ".".to_string(),
688-
token::DotDot => "..".to_string(),
689-
token::DotDotDot => "...".to_string(),
690-
token::DotDotEq => "..=".to_string(),
691-
token::Comma => ",".to_string(),
692-
token::Semi => ";".to_string(),
693-
token::Colon => ":".to_string(),
694-
token::ModSep => "::".to_string(),
695-
token::RArrow => "->".to_string(),
696-
token::LArrow => "<-".to_string(),
697-
token::FatArrow => "=>".to_string(),
698-
token::OpenDelim(token::Paren) => "(".to_string(),
699-
token::CloseDelim(token::Paren) => ")".to_string(),
700-
token::OpenDelim(token::Bracket) => "[".to_string(),
701-
token::CloseDelim(token::Bracket) => "]".to_string(),
702-
token::OpenDelim(token::Brace) => "{".to_string(),
703-
token::CloseDelim(token::Brace) => "}".to_string(),
704-
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".to_string(),
705-
token::Pound => "#".to_string(),
706-
token::Dollar => "$".to_string(),
707-
token::Question => "?".to_string(),
708-
token::SingleQuote => "'".to_string(),
686+
token::At => "@".into(),
687+
token::Dot => ".".into(),
688+
token::DotDot => "..".into(),
689+
token::DotDotDot => "...".into(),
690+
token::DotDotEq => "..=".into(),
691+
token::Comma => ",".into(),
692+
token::Semi => ";".into(),
693+
token::Colon => ":".into(),
694+
token::ModSep => "::".into(),
695+
token::RArrow => "->".into(),
696+
token::LArrow => "<-".into(),
697+
token::FatArrow => "=>".into(),
698+
token::OpenDelim(token::Paren) => "(".into(),
699+
token::CloseDelim(token::Paren) => ")".into(),
700+
token::OpenDelim(token::Bracket) => "[".into(),
701+
token::CloseDelim(token::Bracket) => "]".into(),
702+
token::OpenDelim(token::Brace) => "{".into(),
703+
token::CloseDelim(token::Brace) => "}".into(),
704+
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".into(),
705+
token::Pound => "#".into(),
706+
token::Dollar => "$".into(),
707+
token::Question => "?".into(),
708+
token::SingleQuote => "'".into(),
709709

710710
/* Literals */
711-
token::Literal(lit) => literal_to_string(lit),
711+
token::Literal(lit) => literal_to_string(lit).into(),
712712

713713
/* Name components */
714714
token::Ident(s, is_raw) => {
715-
IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string()
715+
IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string().into()
716716
}
717-
token::Lifetime(s) => s.to_string(),
717+
token::Lifetime(s) => s.to_string().into(),
718718

719719
/* Other */
720720
token::DocComment(comment_kind, attr_style, data) => {
721-
doc_comment_to_string(comment_kind, attr_style, data)
721+
doc_comment_to_string(comment_kind, attr_style, data).into()
722722
}
723-
token::Eof => "<eof>".to_string(),
723+
token::Eof => "<eof>".into(),
724724

725-
token::Interpolated(ref nt) => self.nonterminal_to_string(nt),
725+
token::Interpolated(ref nt) => self.nonterminal_to_string(nt).into(),
726726
}
727727
}
728728

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

734-
fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> String {
734+
fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
735735
let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
736736
self.token_kind_to_string_ext(&token.kind, convert_dollar_crate)
737737
}

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
12211221

12221222
fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
12231223
match *tt {
1224-
mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token),
1224+
mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token).into(),
12251225
mbe::TokenTree::MetaVar(_, name) => format!("${}", name),
12261226
mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${}:{}", name, kind),
12271227
mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${}:", name),

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ impl<'a> Parser<'a> {
15281528
.span_suggestion(
15291529
token.span,
15301530
"must have an integer part",
1531-
pprust::token_to_string(token),
1531+
pprust::token_to_string(token).into(),
15321532
Applicability::MachineApplicable,
15331533
)
15341534
.emit();

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ impl<'a> Parser<'a> {
806806
.span_suggestion_short(
807807
sp,
808808
&format!("missing `{}`", token_str),
809-
token_str,
809+
token_str.into(),
810810
Applicability::MaybeIncorrect,
811811
)
812812
.emit();

0 commit comments

Comments
 (0)