Skip to content

Commit

Permalink
Rollup merge of #126232 - RalfJung:dyn-trait-equality, r=oli-obk
Browse files Browse the repository at this point in the history
interpret: dyn trait metadata check: equate traits in a proper way

Hopefully fixes #3541... unfortunately we don't have a testcase.

The first commit is just a refactor without functional change.

r? `@oli-obk`
  • Loading branch information
GuillaumeGomez committed Jun 12, 2024
2 parents 3973980 + dda28dc commit 5e9d377
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/fail/dyn-call-trait-mismatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Validation stops this too early.
//@compile-flags: -Zmiri-disable-validation
// Validation and SB stop this too early.
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

trait T1 {
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![feature(ptr_metadata)]
// This test is the result of minimizing the `emplacable` crate to reproduce
// <https://github.com/rust-lang/miri/issues/3541>.

use std::{ops::FnMut, ptr::Pointee};

pub type EmplacerFn<'a, T> = dyn for<'b> FnMut(<T as Pointee>::Metadata) + 'a;

#[repr(transparent)]
pub struct Emplacer<'a, T>(EmplacerFn<'a, T>)
where
T: ?Sized;

impl<'a, T> Emplacer<'a, T>
where
T: ?Sized,
{
pub unsafe fn from_fn<'b>(emplacer_fn: &'b mut EmplacerFn<'a, T>) -> &'b mut Self {
// This used to trigger:
// constructing invalid value: wrong trait in wide pointer vtable: expected
// `std::ops::FnMut(<[std::boxed::Box<i32>] as std::ptr::Pointee>::Metadata)`, but encountered
// `std::ops::FnMut<(usize,)>`.
unsafe { &mut *((emplacer_fn as *mut EmplacerFn<'a, T>) as *mut Self) }
}
}

pub fn box_new_with<T>()
where
T: ?Sized,
{
let emplacer_closure = &mut |_meta| {
unreachable!();
};

unsafe { Emplacer::<T>::from_fn(emplacer_closure) };
}

fn main() {
box_new_with::<[Box<i32>]>();
}

0 comments on commit 5e9d377

Please sign in to comment.