pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
let tcx = self.tcx;
let span = tcx.hir().span(hir_id);
- let convert = |ty: Ty<'tcx>| {
+ let normalize = |ty| {
let ty = self.resolve_vars_if_possible(ty);
- let ty = tcx.normalize_erasing_regions(self.param_env, ty);
- (SizeSkeleton::compute(ty, tcx, self.param_env), ty)
+ self.tcx.normalize_erasing_regions(self.param_env, ty)
};
- let (sk_from, from) = convert(from);
- let (sk_to, to) = convert(to);
+ let from = normalize(from);
+ let to = normalize(to);
+ trace!(?from, ?to);
+
+ // Transmutes that are only changing lifetimes are always ok.
+ if from == to {
+ return;
+ }
+
+ let skel = |ty| SizeSkeleton::compute(ty, tcx, self.param_env);
+ let sk_from = skel(from);
+ let sk_to = skel(to);
+ trace!(?sk_from, ?sk_to);
// Check for same size using the skeletons.
if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
+// check-pass
+
trait Foo {
type Bar;
}
unsafe fn noop<F: Foo>(foo: F::Bar) -> F::Bar {
- ::std::mem::transmute(foo) //~ ERROR cannot transmute between types of different sizes
+ ::std::mem::transmute(foo)
}
fn main() {}
+++ /dev/null
-error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
- --> $DIR/transmute-equal-assoc-types.rs:6:5
- |
-LL | ::std::mem::transmute(foo)
- | ^^^^^^^^^^^^^^^^^^^^^
- |
- = note: `<F as Foo>::Bar` does not have a fixed size
-
-error: aborting due to previous error
-
-For more information about this error, try `rustc --explain E0512`.
--- /dev/null
+// check-pass
+
+use std::ptr::NonNull;
+
+struct Foo<'a, T: ?Sized>(&'a (), NonNull<T>);
+
+fn foo<'a, 'b, T: ?Sized>(x: Foo<'a, T>) -> Foo<'b, T> {
+ unsafe { std::mem::transmute(x) }
+}
+
+struct Bar<'a, T: ?Sized>(&'a T);
+
+fn bar<'a, 'b, T: ?Sized>(x: Bar<'a, T>) -> Bar<'b, T> {
+ unsafe { std::mem::transmute(x) }
+}
+
+struct Boo<'a, T: ?Sized>(&'a T, u32);
+
+fn boo<'a, 'b, T: ?Sized>(x: Boo<'a, T>) -> Boo<'b, T> {
+ unsafe { std::mem::transmute(x) }
+}
+
+fn main() {}
unsafe fn transmute_lifetime<'a, 'b, C>(x: <C as TypeConstructor<'a>>::T)
-> <C as TypeConstructor<'b>>::T
where for<'z> C: TypeConstructor<'z> {
- transmute(x) //~ ERROR cannot transmute between types of different sizes
+ transmute(x)
}
unsafe fn sizes() {
-error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
- --> $DIR/main.rs:13:5
- |
-LL | transmute(x)
- | ^^^^^^^^^
- |
- = note: `<C as TypeConstructor<'_>>::T` does not have a fixed size
-
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> $DIR/main.rs:17:17
|
= note: source type: `i32` (32 bits)
= note: target type: `Foo` (0 bits)
-error: aborting due to 4 previous errors
+error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0512`.