Missed optimization for format whose argument is a known static string
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
pub fn test() -> String {
let input = "abc";
format!("pre:{}", input)
}
I expected/hoped this would be optimized down to "pre:abc".to_string(), as it does when a string literal is written inline as an argument to format!. However, the assembly output shows "abc" and "pre:" as separate strings. (Note that in the inline case, there is no string at all; the optimized form inlines the string data as integer constants.)
(Occurs in both stable 1.88.0 and nightly 2025-07-21.)
Motivated by using std::any::type_name for logging and being mildly surprised it wasn't statically resolved, though I realize that may have a few layers beyond this one.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal Rust example in the issue and compare its release assembly with the inline-literal form on stable and nightly. Trace how format! handles arguments and whether the known static string is propagated. Done means the local-string form receives the same optimization as the inline form, with a regression test covering the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100