Skip to content

Commit 8773102

Browse files
committed
handle/hack for arbitrary-self dyn receivers
1 parent 583f73a commit 8773102

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
357357
ty::RawPtr(mt) => mt.ty,
358358
// We should only accept `Box` with the default allocator.
359359
// It's hard to test for that though so we accept every 1-ZST allocator.
360+
// We need to also accept all `DispatchFromDyn` pointer types. (`DispatchFromDyn` is
361+
// magic and checks that those satisfy the `repr(transparent)` conditions.)
362+
// For now we hard-code `Rc` and `Arc`.
363+
// FIXME: find a more systematic way to handle that.
360364
ty::Adt(def, args)
361-
if def.is_box()
365+
if (def.is_box()
366+
|| self.tcx.is_diagnostic_item(sym::Rc, def.did())
367+
|| self.tcx.is_diagnostic_item(sym::Arc, def.did()))
362368
&& self
363369
.layout_of(args[1].as_type().unwrap())
364370
.is_ok_and(|l| l.is_1zst()) =>
365371
{
366372
args[0].as_type().unwrap()
367373
}
374+
368375
_ => return None,
369376
})
370377
};

src/tools/miri/tests/pass/dyn-arbitrary-self.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn pointers_and_wrappers() {
6565
ops::{CoerceUnsized, Deref, DispatchFromDyn},
6666
};
6767

68+
#[repr(transparent)] // FIXME without this, Miri complains about ABI mismatches
6869
struct Ptr<T: ?Sized>(Box<T>);
6970

7071
impl<T: ?Sized> Deref for Ptr<T> {
@@ -78,6 +79,7 @@ fn pointers_and_wrappers() {
7879
impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
7980
impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
8081

82+
#[repr(transparent)] // FIXME without this, Miri complains about ABI mismatches
8183
struct Wrapper<T: ?Sized>(T);
8284

8385
impl<T: ?Sized> Deref for Wrapper<T> {

0 commit comments

Comments
 (0)