about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2023-01-13 19:21:23 +0100
committerMara Bos <m-ou.se@m-ou.se>2023-03-16 11:21:50 +0100
commit6a535dfff409438c4266a2b222f1dbdfeda9d372 (patch)
tree5cdafde08b1af309512aae0a9b8842ecff2559f8
parentdf8c14ca61833335fa6229b4d828fcc481a57432 (diff)
downloadrust-6a535dfff409438c4266a2b222f1dbdfeda9d372.tar.gz
rust-6a535dfff409438c4266a2b222f1dbdfeda9d372.zip
Also inline integer literals into format_args!().
-rw-r--r--compiler/rustc_ast_lowering/src/format.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs
index acfc0099db7..8a8318312d2 100644
--- a/compiler/rustc_ast_lowering/src/format.rs
+++ b/compiler/rustc_ast_lowering/src/format.rs
@@ -97,11 +97,11 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
 ///
 /// Turns
 ///
-/// `format_args!("Hello, {}! {}", "World", 123)`
+/// `format_args!("Hello, {}! {} {}", "World", 123, x)`
 ///
 /// into
 ///
-/// `format_args!("Hello, World! {}", 123)`.
+/// `format_args!("Hello, World! 123 {}", x)`.
 fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
     let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
     let mut inlined_anything = false;
@@ -109,18 +109,31 @@ fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
     for i in 0..fmt.template.len() {
         let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
         let Ok(arg_index) = placeholder.argument.index else { continue };
+
+        let mut literal = None;
+
         if let FormatTrait::Display = placeholder.format_trait
             && placeholder.format_options == Default::default()
             && let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
             && let ExprKind::Lit(lit) = arg.kind
-            && let token::LitKind::Str | token::LitKind::StrRaw(_) = lit.kind
-            && let Ok(LitKind::Str(s, _)) = LitKind::from_token_lit(lit)
         {
+            if let token::LitKind::Str | token::LitKind::StrRaw(_) = lit.kind
+                && let Ok(LitKind::Str(s, _)) = LitKind::from_token_lit(lit)
+            {
+                literal = Some(s);
+            } else if let token::LitKind::Integer = lit.kind
+                && let Ok(LitKind::Int(n, _)) = LitKind::from_token_lit(lit)
+            {
+                literal = Some(Symbol::intern(&n.to_string()));
+            }
+        }
+
+        if let Some(literal) = literal {
             // Now we need to mutate the outer FormatArgs.
             // If this is the first time, this clones the outer FormatArgs.
             let fmt = fmt.to_mut();
             // Replace the placeholder with the literal.
-            fmt.template[i] = FormatArgsPiece::Literal(s);
+            fmt.template[i] = FormatArgsPiece::Literal(literal);
             was_inlined[arg_index] = true;
             inlined_anything = true;
         }