Skip to content

Turning TypeId into references in const complains about null pointers #146748

@theemathas

Description

@theemathas

Code

const A: [&(); 2] = unsafe { std::mem::transmute(std::any::TypeId::of::<i32>()) };

Current output

error[E0080]: constructing invalid value at [0]: encountered a null reference
 --> src/lib.rs:1:1
  |
1 | const A: [&(); 2] = unsafe { std::mem::transmute(std::any::TypeId::of::<i32>()) };
  | ^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
  |
  = note: the rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
  = note: the raw bytes of the constant (size: 16, align: 8) {
              ╾a5+0x50bb9674fa2df013╼ ╾a5+0x56ced5e4a15bd890╼ │ ╾──────╼╾──────╼
          }

For more information about this error, try `rustc --explain E0080`.

Desired output

Mention TypeId instead of null pointers

Rationale and extra context

No response

Other cases

Rust Version

Reproducible on the playground with version 1.92.0-nightly (2025-09-18 7c275d09ea6b953d2cca)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsT-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