Skip to content

Commit ae0b03b

Browse files
committed
Auto merge of #88262 - klensy:pprust-cow, r=nagisa
Cow'ify some pprust methods Reduce number of potential needless de/allocations by using `Cow<'static, str>` instead of explicit `String` type.
2 parents daa4dc9 + c565339 commit ae0b03b

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

compiler/rustc_ast_pretty/src/pprust/mod.rs

+4-2
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

+46-46
Original file line numberDiff line numberDiff line change
@@ -685,80 +685,80 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
685685
}
686686

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

692692
fn token_kind_to_string_ext(
693693
&self,
694694
tok: &TokenKind,
695695
convert_dollar_crate: Option<Span>,
696-
) -> String {
696+
) -> Cow<'static, str> {
697697
match *tok {
698-
token::Eq => "=".to_string(),
699-
token::Lt => "<".to_string(),
700-
token::Le => "<=".to_string(),
701-
token::EqEq => "==".to_string(),
702-
token::Ne => "!=".to_string(),
703-
token::Ge => ">=".to_string(),
704-
token::Gt => ">".to_string(),
705-
token::Not => "!".to_string(),
706-
token::Tilde => "~".to_string(),
707-
token::OrOr => "||".to_string(),
708-
token::AndAnd => "&&".to_string(),
709-
token::BinOp(op) => binop_to_string(op).to_string(),
710-
token::BinOpEq(op) => format!("{}=", binop_to_string(op)),
698+
token::Eq => "=".into(),
699+
token::Lt => "<".into(),
700+
token::Le => "<=".into(),
701+
token::EqEq => "==".into(),
702+
token::Ne => "!=".into(),
703+
token::Ge => ">=".into(),
704+
token::Gt => ">".into(),
705+
token::Not => "!".into(),
706+
token::Tilde => "~".into(),
707+
token::OrOr => "||".into(),
708+
token::AndAnd => "&&".into(),
709+
token::BinOp(op) => binop_to_string(op).into(),
710+
token::BinOpEq(op) => format!("{}=", binop_to_string(op)).into(),
711711

712712
/* Structural symbols */
713-
token::At => "@".to_string(),
714-
token::Dot => ".".to_string(),
715-
token::DotDot => "..".to_string(),
716-
token::DotDotDot => "...".to_string(),
717-
token::DotDotEq => "..=".to_string(),
718-
token::Comma => ",".to_string(),
719-
token::Semi => ";".to_string(),
720-
token::Colon => ":".to_string(),
721-
token::ModSep => "::".to_string(),
722-
token::RArrow => "->".to_string(),
723-
token::LArrow => "<-".to_string(),
724-
token::FatArrow => "=>".to_string(),
725-
token::OpenDelim(token::Paren) => "(".to_string(),
726-
token::CloseDelim(token::Paren) => ")".to_string(),
727-
token::OpenDelim(token::Bracket) => "[".to_string(),
728-
token::CloseDelim(token::Bracket) => "]".to_string(),
729-
token::OpenDelim(token::Brace) => "{".to_string(),
730-
token::CloseDelim(token::Brace) => "}".to_string(),
731-
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".to_string(),
732-
token::Pound => "#".to_string(),
733-
token::Dollar => "$".to_string(),
734-
token::Question => "?".to_string(),
735-
token::SingleQuote => "'".to_string(),
713+
token::At => "@".into(),
714+
token::Dot => ".".into(),
715+
token::DotDot => "..".into(),
716+
token::DotDotDot => "...".into(),
717+
token::DotDotEq => "..=".into(),
718+
token::Comma => ",".into(),
719+
token::Semi => ";".into(),
720+
token::Colon => ":".into(),
721+
token::ModSep => "::".into(),
722+
token::RArrow => "->".into(),
723+
token::LArrow => "<-".into(),
724+
token::FatArrow => "=>".into(),
725+
token::OpenDelim(token::Paren) => "(".into(),
726+
token::CloseDelim(token::Paren) => ")".into(),
727+
token::OpenDelim(token::Bracket) => "[".into(),
728+
token::CloseDelim(token::Bracket) => "]".into(),
729+
token::OpenDelim(token::Brace) => "{".into(),
730+
token::CloseDelim(token::Brace) => "}".into(),
731+
token::OpenDelim(token::NoDelim) | token::CloseDelim(token::NoDelim) => "".into(),
732+
token::Pound => "#".into(),
733+
token::Dollar => "$".into(),
734+
token::Question => "?".into(),
735+
token::SingleQuote => "'".into(),
736736

737737
/* Literals */
738-
token::Literal(lit) => literal_to_string(lit),
738+
token::Literal(lit) => literal_to_string(lit).into(),
739739

740740
/* Name components */
741741
token::Ident(s, is_raw) => {
742-
IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string()
742+
IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string().into()
743743
}
744-
token::Lifetime(s) => s.to_string(),
744+
token::Lifetime(s) => s.to_string().into(),
745745

746746
/* Other */
747747
token::DocComment(comment_kind, attr_style, data) => {
748-
doc_comment_to_string(comment_kind, attr_style, data)
748+
doc_comment_to_string(comment_kind, attr_style, data).into()
749749
}
750-
token::Eof => "<eof>".to_string(),
750+
token::Eof => "<eof>".into(),
751751

752-
token::Interpolated(ref nt) => self.nonterminal_to_string(nt),
752+
token::Interpolated(ref nt) => self.nonterminal_to_string(nt).into(),
753753
}
754754
}
755755

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

761-
fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> String {
761+
fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> {
762762
let convert_dollar_crate = convert_dollar_crate.then_some(token.span);
763763
self.token_kind_to_string_ext(&token.kind, convert_dollar_crate)
764764
}

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
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

+1-1
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

+1-1
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)