Skip to content

explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls#157587

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:size-of-no-dce
Jun 8, 2026
Merged

explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls#157587
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:size-of-no-dce

Conversation

@RalfJung
Copy link
Copy Markdown
Member

@RalfJung RalfJung commented Jun 7, 2026

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

#![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.
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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 7, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk changed the title explain that the size_of constant also serves to avoid optimizing awa… explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls Jun 8, 2026
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 8, 2026

📌 Commit daaf367 has been approved by oli-obk

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 8, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 8, 2026
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
rust-bors Bot pushed a commit that referenced this pull request Jun 8, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #147302 (asm! support for the Xtensa architecture)
 - #148820 (Add very basic "comptime" fn implementation)
 - #157299 (Fix unstable diagnostics in tests)
 - #143511 (Improve TLS codegen by marking the panic/init path as cold)
 - #154608 (Add `_value` API for number literals in proc-macro)
 - #156762 (xfs support in `test_rename_directory_to_non_empty_directory`)
 - #157300 (Relax test requirements for consistency)
 - #157383 (tests: codegen-llvm: Ignore BPF targets in c-variadic-opt)
 - #157413 (fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option)
 - #157578 (Fix diagnostics for non-exhaustive destructuring assignments (#157553))
 - #157587 (explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls)
 - #157596 (test: remove ineffective link-extern-crate-with-drop-type test)
 - #157602 (rustdoc: Remove unnecessary fast path)
@rust-bors rust-bors Bot merged commit c9da864 into rust-lang:main Jun 8, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 8, 2026
rust-timer added a commit that referenced this pull request Jun 8, 2026
Rollup merge of #157587 - RalfJung:size-of-no-dce, r=oli-obk

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants