rust-lang / rust-lang/rust

size_of_val is allowed on an uninitialized static

Open
#142,900 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-docs T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I don't know if this is a bug or working as intended. So I'm filing an issue just in case.

trait Trait {}
impl Trait for usize {}

static X: usize = std::mem::size_of_val(&X as &dyn Trait);

fn main() {
    println!("{X}");
}

This code compiles and prints 8. I'm not sure if this code is supposed to compile.

See also https://github.com/rust-lang/rust/issues/142532 and https://github.com/rust-lang/rust/issues/142404 for shenanigans with uninitialized statics.

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (be19eda0d 2025-06-22)
binary: rustc
commit-hash: be19eda0dc4c22c5cf5f1b48fd163acf9bd4b0a6
commit-date: 2025-06-22
host: aarch64-apple-darwin
release: 1.89.0-nightly
LLVM version: 20.1.7

@rustbot labels +A-const-eval

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

Start by compiling the supplied Rust reproduction with the reported nightly version and compare its behavior with issues #142532 and #142404. The issue names no source file or test; progress would require establishing whether this const-evaluation behavior is valid and documenting a clear expected outcome.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.