Files
rust/compiler/rustc_span
bors 22057b88b0 Auto merge of #155452 - makai410:enum-debug-array, r=nnethercote
Implement `Debug` for C-like enums with a concatenated string



Fixes: rust-lang/rust#133945 

Related to: rust-lang/rust#88793 rust-lang/rust#114106

Continuation of: rust-lang/rust#109615 rust-lang/rust#114190 

This optimizes the expansion of `derive(Debug)` for C-like enums with 10 or more variants to:
```rust
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
    const __NAMES: &str =
        "AAAAABBBBCCDDDDDDDDEFFFFFFFFFFFFFGGGGGGHatsuneIIIIIIIJJJJJJJJJ";
    const __OFFSET: [usize; 11] =
        [0usize, 5usize, 9usize, 11usize, 19usize, 20usize, 33usize,
                39usize, 46usize, 53usize, 62usize];
    let __d = ::core::intrinsics::discriminant_value(self) as usize;
    ::core::fmt::Formatter::debug_c_like_enum_write_str(f, __NAMES,
        &__OFFSET, __d)
}
```

The number 10 was decided based on both [the rustc perf result](https://github.com/rust-lang/rust/pull/155452#issuecomment-5080104400) and the results from the dedicated perf tool for this scenario:

<img width="1920" height="1280" alt="chart-link" src="https://github.com/user-attachments/assets/1327ee63-ce05-482b-a1fd-c56e919250ea" />
<img width="1920" height="1280" alt="chart-obj" src="https://github.com/user-attachments/assets/1cb831d3-c9dc-469f-b87d-03cf633f8335" />


See https://codeberg.org/makai410/debug-fmt-perf for more details.

r? @ghost (I want to see the perf first)
2026-08-02 23:16:20 +00:00
..