rust-lang / rust-lang/rust

Codegen depends on let-variable ordering for unclear reasons

Open
#131,987 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM A-panic C-optimization I-heavy T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/Wx1rvE6vq

use std::borrow::Cow;

pub struct Error {
    message: Cow<'static, str>,
    cause: Option<Box<[u8]>>,
}
#[no_mangle]
pub fn declear_before(v: u64) -> Error {
    let s = format!("{v}");
    let mut e = Error {
        message: Cow::Borrowed(""),
        cause: None,
    };
    e.message = s.into();
    e
}

Declear s before Error produces better codegen than declear after.

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 two declaration-order variants from the linked Godbolt example and compare their generated code. Trace the compiler codegen path responsible for the difference; done means the ordering dependence is explained and, if confirmed unintended, covered by a regression test.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.