Skip to content

Attempting to transmute between two of the same associated type results in an error. #49793

Closed
@AlphaModder

Description

@AlphaModder

While attempting to store a set of values that differ in type but not layout together in a collection, I discovered that the following code, strangely, results in E0512 ("transmute called with types of different sizes").

trait Foo {
    type Bar;
}
unsafe fn noop<F: Foo>(foo: F::Bar) -> F::Bar {
    ::std::mem::transmute(foo)
}

Here is the exact error message produced:

error[E0512]: transmute called with types of different sizes
 --> src/main.rs:5:5
  |
5 |     ::std::mem::transmute(foo)
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: source type: <F as Foo>::Bar (this type's size can vary)
  = note: target type: <F as Foo>::Bar (this type's size can vary)

Either I'm missing something extremely subtle and non-intuitive here, or this a bug or blindspot of the compiler. If it is anything but an easily fixable bug, I would suggest that a special error message be added for this case in the meantime, as the current one is rather confusing.

Output of rustc --version --verbose:

rustc 1.25.0 (84203cac6 2018-03-25)
binary: rustc
commit-hash: 84203cac67e65ca8640b8392348411098c856985
commit-date: 2018-03-25
host: x86_64-pc-windows-msvc
release: 1.25.0
LLVM version: 6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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