Skip to content

Commit b3ab80c

Browse files
committed
Auto merge of #113175 - bryangarza:safe-transmute-rustc-coinductive, r=compiler-errors
Enable coinduction support for Safe Transmute This patch adds the `#[rustc_coinductive]` annotation to `BikeshedIntrinsicFrom`, so that it's possible to compute transmutability for recursive types. ## Motivation Safe Transmute currently already supports references (#110662). However, if a type is implemented recursively, it leads to an infinite loop when we try to check if transmutation is safe. A couple simple examples that one might want to write, that are currently not possible to check transmutability for: ```rs #[repr(C)] struct A(&'static B); #[repr(C)] struct B(&'static A); ``` ```rs #[repr(C)] enum IList<'a> { Nil, Cons(isize, &'a IList<'a>) } #[repr(C)] enum UList<'a> { Nil, Cons(usize, &'a UList<'a>) } ``` Previously, `@jswrenn` was considering writing a co-inductive solver from scratch, just for the `rustc_tranmsute` crate. Later on as I started working on Safe Transmute myself, I came across the `#[rustc_coinductive]` annotation, which is currently only being used for the `Sized` trait. Leveraging this trait actually solved the problem entirely, and it saves a lot of duplicate work that would have had to happen in `rustc_transmute`.
2 parents d8899c5 + 6b214bb commit b3ab80c

File tree

5 files changed

+5
-56
lines changed

5 files changed

+5
-56
lines changed

Diff for: library/core/src/mem/transmutability.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::marker::ConstParamTy;
99
#[lang = "transmute_trait"]
1010
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl(implement_via_object = false))]
1111
#[cfg_attr(bootstrap, rustc_deny_explicit_impl)]
12+
#[rustc_coinductive]
1213
pub unsafe trait BikeshedIntrinsicFrom<Src, Context, const ASSUME: Assume = { Assume::NOTHING }>
1314
where
1415
Src: ?Sized,

Diff for: tests/ui/transmutability/references/recursive-wrapper-types-bit-compatible.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// check-fail
2-
// FIXME(bryangarza): Change to check-pass when coinduction is supported for BikeshedIntrinsicFrom
1+
// check-pass
32
#![feature(transmutability)]
43

54
mod assert {
@@ -22,5 +21,5 @@ mod assert {
2221
fn main() {
2322
#[repr(C)] struct A(bool, &'static A);
2423
#[repr(C)] struct B(u8, &'static B);
25-
assert::is_maybe_transmutable::<&'static A, &'static B>(); //~ ERROR overflow evaluating the requirement
24+
assert::is_maybe_transmutable::<&'static A, &'static B>();
2625
}

Diff for: tests/ui/transmutability/references/recursive-wrapper-types-bit-compatible.stderr

-25
This file was deleted.

Diff for: tests/ui/transmutability/references/recursive-wrapper-types.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// check-fail
2-
// FIXME(bryangarza): Change to check-pass when coinduction is supported for BikeshedIntrinsicFrom
1+
// check-pass
32
#![feature(transmutability)]
43

54
mod assert {
@@ -22,6 +21,6 @@ mod assert {
2221
fn main() {
2322
#[repr(C)] struct A(&'static B);
2423
#[repr(C)] struct B(&'static A);
25-
assert::is_maybe_transmutable::<&'static A, &'static B>(); //~ overflow evaluating the requirement
24+
assert::is_maybe_transmutable::<&'static A, &'static B>();
2625
assert::is_maybe_transmutable::<&'static B, &'static A>();
2726
}

Diff for: tests/ui/transmutability/references/recursive-wrapper-types.stderr

-25
This file was deleted.

0 commit comments

Comments
 (0)