rust-lang / rust-lang/rust

`fmt::Arguments::as_statically_known_str` optimizes poorly

Open
#152,892 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-fmt A-LLVM C-optimization T-compiler T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I compiled this code with -Copt-level=3

#![feature(fmt_internals)]
#![expect(internal_features)]

#[unsafe(no_mangle)]
fn foo(a: std::fmt::Arguments<'_>) -> Option<&str> {
    a.as_statically_known_str()
}

Godbolt

I got the following assembly:

foo:
        mov     rdx, rsi
        shr     rdx
        xor     eax, eax
        ret

This function should just optimize to returning None, so the first two instructions seem extraneous.

I am unsure if this has any impact on code using the actual fmt public API.

Meta

Reproducible on godbolt with version:

rustc 1.95.0-nightly (7f99507f5 2026-02-19)
binary: rustc
commit-hash: 7f99507f57e6c4aa0dce3daf6a13cca8cd4dd312
commit-date: 2026-02-19
host: x86_64-unknown-linux-gnu
release: 1.95.0-nightly
LLVM version: 22.1.0
Internal compiler ID: nightly

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Rust 1.95.0-nightly example on Godbolt and inspect the generated assembly for fmt::Arguments::as_statically_known_str. Start by tracing that entry point through the compiler sources and its optimization behavior. Done means the unnecessary instructions are eliminated for this case, with relevant compiler tests covering the result.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.