rust-lang / rust-lang/rust

Bad codegen with functions returning `Result`.

Open
#156,423 6 comments 0 reactions 1 assignee View on GitHub

@el-ev is already working on this.

Since May 11, 2026.

A-LLVM C-bug I-slow llvm-fixed-upstream needs-triage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:
https://godbolt.org/z/WGK9WTKec

type Result<T> = core::result::Result<T, u32>;
//                                       ^--- note: all other integer types compile as expected

fn map<T, F, U>(value: Result<T>, op: F) -> Result<U>
where 
    F: FnOnce(T) -> U
{
    value.map(op)
}

pub fn assign(val: [u8; 4], out: &mut u32) -> Result<()> {
    let val = map(Ok(val), u32::from_le_bytes)?;

    *out = val;

    Ok(())
}

I expected to see this:

example[91f12ae00899f36c]::assign:
        mov     dword ptr [rsi], edi
        xor     eax, eax
        ret

Instead, this happened:

example[91f12ae00899f36c]::assign:
        mov     eax, edi
        shr     eax, 24     ; elaborate no-op
        shl     edi, 8      ; elaborate no-op
        shrd    edi, eax, 8 ; elaborate no-op
                            ; edi = edi (when the call started)
        mov     dword ptr [rsi], edi
        xor     eax, eax
        ret
Meta

rustc --version --verbose:

nightly on godbolt

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.