Skip to content

Commit 5d95660

Browse files
Add more tests
1 parent 5fc4a79 commit 5d95660

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/tools/miri/tests/pass/dyn-upcast.rs

+51
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn main() {
99
struct_();
1010
replace_vptr();
1111
vtable_nop_cast();
12+
drop_principal();
1213
}
1314

1415
fn vtable_nop_cast() {
@@ -430,3 +431,53 @@ fn replace_vptr() {
430431
let s = S(42);
431432
invoke_outer(&s);
432433
}
434+
435+
fn drop_principal() {
436+
use std::{alloc::Layout, any::Any};
437+
438+
const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
439+
x
440+
}
441+
442+
trait Bar: Send + Sync {}
443+
444+
impl<T: Send + Sync> Bar for T {}
445+
446+
const fn yeet_principal_2(x: Box<dyn Bar>) -> Box<dyn Send> {
447+
x
448+
}
449+
450+
struct CallMe<F: FnOnce()>(Option<F>);
451+
452+
impl<F: FnOnce()> CallMe<F> {
453+
fn new(f: F) -> Self {
454+
CallMe(Some(f))
455+
}
456+
}
457+
458+
impl<F: FnOnce()> Drop for CallMe<F> {
459+
fn drop(&mut self) {
460+
(self.0.take().unwrap())();
461+
}
462+
}
463+
464+
fn goodbye() {
465+
println!("goodbye");
466+
}
467+
468+
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>;
469+
let x_layout = Layout::for_value(&*x);
470+
let y = yeet_principal(x);
471+
let y_layout = Layout::for_value(&*y);
472+
assert_eq!(x_layout, y_layout);
473+
println!("before");
474+
drop(y);
475+
476+
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
477+
let x_layout = Layout::for_value(&*x);
478+
let y = yeet_principal_2(x);
479+
let y_layout = Layout::for_value(&*y);
480+
assert_eq!(x_layout, y_layout);
481+
println!("before");
482+
drop(y);
483+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
before
2+
goodbye
3+
before
4+
goodbye

tests/ui/traits/dyn-drop-principal.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ run-pass
22
//@ check-run-results
33

4-
use std::any::Any;
4+
use std::{alloc::Layout, any::Any};
55

66
const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
77
x
@@ -35,12 +35,18 @@ fn goodbye() {
3535

3636
fn main() {
3737
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>;
38+
let x_layout = Layout::for_value(&*x);
3839
let y = yeet_principal(x);
40+
let y_layout = Layout::for_value(&*y);
41+
assert_eq!(x_layout, y_layout);
3942
println!("before");
4043
drop(y);
4144

4245
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
46+
let x_layout = Layout::for_value(&*x);
4347
let y = yeet_principal_2(x);
48+
let y_layout = Layout::for_value(&*y);
49+
assert_eq!(x_layout, y_layout);
4450
println!("before");
4551
drop(y);
4652
}

0 commit comments

Comments
 (0)