Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion compiler/rustc_const_eval/src/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let mut receiver = args[0].clone();
let receiver_place = loop {
match receiver.layout.ty.kind() {
ty::Ref(..) | ty::RawPtr(..) => break self.deref_operand(&receiver)?,
ty::Ref(..) | ty::RawPtr(..) => {
// We do *not* use `deref_operand` here: we don't want to conceptually
// create a place that must be dereferenceable, since the receiver might
// be a raw pointer and (for `*const dyn Trait`) we don't need to
// actually access memory to resolve this method.
// Also see <https://github.com/rust-lang/miri/issues/2786>.
let val = self.read_immediate(&receiver)?;
break self.ref_to_mplace(&val)?;
}
ty::Dynamic(.., ty::Dyn) => break receiver.assert_mem_place(), // no immediate unsized values
ty::Dynamic(.., ty::DynStar) => {
// Not clear how to handle this, so far we assume the receiver is always a pointer.
Expand Down
27 changes: 27 additions & 0 deletions src/tools/miri/tests/pass/dyn-arbitrary-self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,35 @@ fn pointers_and_wrappers() {
assert_eq!(wpw.wrapper_ptr_wrapper(), 7);
}

fn raw_ptr_receiver() {
use std::ptr;

trait Foo {
fn foo(self: *const Self) -> &'static str;
}

impl Foo for i32 {
fn foo(self: *const Self) -> &'static str {
"I'm an i32!"
}
}

impl Foo for u32 {
fn foo(self: *const Self) -> &'static str {
"I'm a u32!"
}
}

let null_i32 = ptr::null::<i32>() as *const dyn Foo;
let null_u32 = ptr::null::<u32>() as *const dyn Foo;

assert_eq!("I'm an i32!", null_i32.foo());
assert_eq!("I'm a u32!", null_u32.foo());
}

fn main() {
pin_box_dyn();
stdlib_pointers();
pointers_and_wrappers();
raw_ptr_receiver();
}