mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-02 01:40:02 +00:00
explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls
During a discussion at the all-hands I got worried that we might do uncanny optimizations on code like this which would lead to that code actually compiling
```rust
#![crate_type = "lib"]
#[unsafe(no_mangle)] // ensure this gets monomorphized
pub fn f() {
assert_valid_type::<[u32; 1 << 62]>();
}
pub fn assert_valid_type<T>() {
std::mem::size_of::<T>();
}
```
We do something like that for [even more cursed similar code](https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=41b84c5e705c9d86a05a919e2135e2d3).
But turns out that for `size_of`, we are good! Ever since that was made a constant, the required_consts system ensures that we do indeed evaluate the constant even if we do not use its results.
r? @oli-obk