Skip to content

Commit 6bcdf8a

Browse files
authored
Rollup merge of #101520 - oli-obk:transmute_lifetimes, r=compiler-errors
Allow transmutes between the same types after erasing lifetimes r? ````@compiler-errors```` on the impl fixes #101081 See discussion in the issue and at https://rust-lang.zulipchat.com/#narrow/stream/326866-t-types.2Fnominated/topic/.23101081.3A.20Regression.20transmuting.20.60RwLockReadGuard.3CT.3A.20.3FSized.3E.E2.80.A6 I think this may need lang team signoff as its implications may go beyond the jurisdiction of T-types I'll write up a proper summary later
2 parents a688a03 + d90d055 commit 6bcdf8a

File tree

6 files changed

+43
-27
lines changed

6 files changed

+43
-27
lines changed

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4444
pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
4545
let tcx = self.tcx;
4646
let span = tcx.hir().span(hir_id);
47-
let convert = |ty: Ty<'tcx>| {
47+
let normalize = |ty| {
4848
let ty = self.resolve_vars_if_possible(ty);
49-
let ty = tcx.normalize_erasing_regions(self.param_env, ty);
50-
(SizeSkeleton::compute(ty, tcx, self.param_env), ty)
49+
self.tcx.normalize_erasing_regions(self.param_env, ty)
5150
};
52-
let (sk_from, from) = convert(from);
53-
let (sk_to, to) = convert(to);
51+
let from = normalize(from);
52+
let to = normalize(to);
53+
trace!(?from, ?to);
54+
55+
// Transmutes that are only changing lifetimes are always ok.
56+
if from == to {
57+
return;
58+
}
59+
60+
let skel = |ty| SizeSkeleton::compute(ty, tcx, self.param_env);
61+
let sk_from = skel(from);
62+
let sk_to = skel(to);
63+
trace!(?sk_from, ?sk_to);
5464

5565
// Check for same size using the skeletons.
5666
if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// check-pass
2+
13
trait Foo {
24
type Bar;
35
}
46

57
unsafe fn noop<F: Foo>(foo: F::Bar) -> F::Bar {
6-
::std::mem::transmute(foo) //~ ERROR cannot transmute between types of different sizes
8+
::std::mem::transmute(foo)
79
}
810

911
fn main() {}

src/test/ui/transmute-equal-assoc-types.stderr

-11
This file was deleted.

src/test/ui/transmute/lifetimes.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
3+
use std::ptr::NonNull;
4+
5+
struct Foo<'a, T: ?Sized>(&'a (), NonNull<T>);
6+
7+
fn foo<'a, 'b, T: ?Sized>(x: Foo<'a, T>) -> Foo<'b, T> {
8+
unsafe { std::mem::transmute(x) }
9+
}
10+
11+
struct Bar<'a, T: ?Sized>(&'a T);
12+
13+
fn bar<'a, 'b, T: ?Sized>(x: Bar<'a, T>) -> Bar<'b, T> {
14+
unsafe { std::mem::transmute(x) }
15+
}
16+
17+
struct Boo<'a, T: ?Sized>(&'a T, u32);
18+
19+
fn boo<'a, 'b, T: ?Sized>(x: Boo<'a, T>) -> Boo<'b, T> {
20+
unsafe { std::mem::transmute(x) }
21+
}
22+
23+
fn main() {}

src/test/ui/transmute/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub trait TypeConstructor<'a> {
1010
unsafe fn transmute_lifetime<'a, 'b, C>(x: <C as TypeConstructor<'a>>::T)
1111
-> <C as TypeConstructor<'b>>::T
1212
where for<'z> C: TypeConstructor<'z> {
13-
transmute(x) //~ ERROR cannot transmute between types of different sizes
13+
transmute(x)
1414
}
1515

1616
unsafe fn sizes() {

src/test/ui/transmute/main.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2-
--> $DIR/main.rs:13:5
3-
|
4-
LL | transmute(x)
5-
| ^^^^^^^^^
6-
|
7-
= note: `<C as TypeConstructor<'_>>::T` does not have a fixed size
8-
91
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
102
--> $DIR/main.rs:17:17
113
|
@@ -33,6 +25,6 @@ LL | let x: Foo = transmute(10);
3325
= note: source type: `i32` (32 bits)
3426
= note: target type: `Foo` (0 bits)
3527

36-
error: aborting due to 4 previous errors
28+
error: aborting due to 3 previous errors
3729

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

0 commit comments

Comments
 (0)