Skip to content

size_of_val is allowed on an uninitialized static #142900

Open
@theemathas

Description

@theemathas

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 #142532 and #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions