Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transmute doesn't work on arrays of generic types #80899

Closed
mbartlett21 opened this issue Jan 11, 2021 · 7 comments
Closed

Transmute doesn't work on arrays of generic types #80899

mbartlett21 opened this issue Jan 11, 2021 · 7 comments
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@mbartlett21
Copy link
Contributor

I tried transmuting an array of [T; 10] to itself, but rustc gives an error.

fn transmute_to_self<T: Sized>(v: [T; 10]) -> [T; 10] {
    unsafe { core::mem::transmute(v) }
}

Error:

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/lib.rs:4:14
  |
4 |     unsafe { transmute(v) }
  |              ^^^^^^^^^
  |
  = note: `[T; 10]` does not have a fixed size

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (e22670468 2020-12-30)
binary: rustc
commit-hash: e2267046859c9ceb932abc983561d53a117089f6
commit-date: 2020-12-30
host: x86_64-pc-windows-msvc
release: 1.51.0-nightly
@mbartlett21 mbartlett21 added the C-bug Category: This is a bug. label Jan 11, 2021
@camelid
Copy link
Member

camelid commented Jan 11, 2021

Hmm, that does seem weird.

@camelid camelid added I-prioritize Issue: Indicates that prioritization has been requested for this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 11, 2021
@camelid
Copy link
Member

camelid commented Jan 11, 2021

This compiles fine, so it's not that rustc thinks [T; 10] is unsized:

fn transmute_to_self<T: Sized>(v: [T; 10]) -> impl Sized {
    v
}

@mbartlett21
Copy link
Contributor Author

The error also comes from transmuting generics:

fn transmute_to_self<T>(t: T) -> T {
    unsafe { core::mem::transmute(t) }
}

gives the same error.

@camelid
Copy link
Member

camelid commented Jan 11, 2021

Looks like the code that does this check might not work on generic parameters:

fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
let sk_from = SizeSkeleton::compute(from, self.tcx, self.param_env);
let sk_to = SizeSkeleton::compute(to, self.tcx, self.param_env);

It does say that it's conservative:

/// Type size "skeleton", i.e., the only information determining a type's size.
/// While this is conservative, (aside from constant sizes, only pointers,
/// newtypes thereof and null pointer optimized enums are allowed), it is
/// enough to statically check common use cases of transmute.
#[derive(Copy, Clone, Debug)]
pub enum SizeSkeleton<'tcx> {
/// Any statically computable Layout.
Known(Size),
/// A potentially-fat pointer.
Pointer {
/// If true, this pointer is never null.
non_zero: bool,
/// The type which determines the unsized metadata, if any,
/// of this pointer. Either a type parameter or a projection
/// depending on one, with regions erased.
tail: Ty<'tcx>,
},
}

@DutchGhost
Copy link
Contributor

See #61956 for a previously opened issue

@mbartlett21
Copy link
Contributor Author

If and when the Project Safe Transmute's ext-layout-traits rfc goes through the process, transmute could be changed to have a bound where the types are SizeEq, which should stop this problem.

@JohnTitor
Copy link
Member

Closing as duplicate of #61956 and removing I-prioritize.

@JohnTitor JohnTitor removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants