Skip to content

Regression: transmuting between raw pointer and Option<NonNull> no longer works #155330

@yshui

Description

@yshui

Code

I tried this code:

use std::ptr::NonNull;
pub const fn is_null<'a, T: ?Sized>(ptr: *const T) -> bool {
    unsafe {
        matches!(
            core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr),
            None
        )
    }
}

I expected to see this happen: Code compiles

Instead, this happened:

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/lib.rs:5:13
  |
5 |             core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr),
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: source type: `*const T` (pointer to `T`)
  = note: target type: `Option<NonNull<T>>` (size can vary because of <T as Pointee>::Metadata)

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

Bisected to 338dff3 #136006

Metadata

Metadata

Assignees

Labels

A-layoutArea: Memory layout of typesC-bugCategory: This is a bug.I-prioritizeIssue: Indicates that prioritization has been requested for this issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions