Skip to content

layout errors in transmute checking don't get emitted #149588

@lcnr

Description

@lcnr

https://github.com/rust-lang/rust/blob/672388edbee9e93c35e5fdf7dac818a6612a5103/tests/ui/layout/normalization-failure.rs

use std::marker::PhantomData;

trait Project1 {
    type Assoc1;
}

impl<T> Project1 for T {
    type Assoc1 = ();
}

trait Project2 {
    type Assoc2;
    fn get(self) -> Self::Assoc2;
}

impl<T: Project1<Assoc1 = ()>> Project2 for PhantomData<T> {
    type Assoc2 = ();
    fn get(self) -> Self::Assoc2 {}
}

fn opaque<T>() -> impl Project2 {
    PhantomData::<T>
}

fn check<T: Project1>() {
    unsafe {
        std::mem::transmute::<_, ()>(opaque::<T>().get());
        //~^ ERROR: cannot transmute
        //~| NOTE: source type: `{type error}` (the type has an unknown layout)
        //~| NOTE: target type: `()` (0 bits)
    }
}

results in

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
  --> src/lib.rs:28:9
   |
28 |         std::mem::transmute::<_, ()>(opaque::<T>().get());
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: source type: `{type error}` (the type has an unknown layout)
   = note: target type: `()` (0 bits)

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

This error message is weird:

  • it references type error even though no other error has been emitted
  • we don't explain why the source error is a type error

We should have some LayoutError::NormalizationFailure here which should also be emitted and then don't emit the transmute error as one of the types is a type error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-layoutArea: Memory layout of typesC-bugCategory: This is a bug.T-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