Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet authored and WaffleLapkin committed Oct 17, 2024
1 parent e3800a1 commit c4bce0b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/tools/miri/tests/pass/dyn-upcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
struct_();
replace_vptr();
vtable_nop_cast();
drop_principal();
}

fn vtable_nop_cast() {
Expand Down Expand Up @@ -430,3 +431,53 @@ fn replace_vptr() {
let s = S(42);
invoke_outer(&s);
}

fn drop_principal() {
use std::{alloc::Layout, any::Any};

const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
x
}

trait Bar: Send + Sync {}

impl<T: Send + Sync> Bar for T {}

const fn yeet_principal_2(x: Box<dyn Bar>) -> Box<dyn Send> {
x
}

struct CallMe<F: FnOnce()>(Option<F>);

impl<F: FnOnce()> CallMe<F> {
fn new(f: F) -> Self {
CallMe(Some(f))
}
}

impl<F: FnOnce()> Drop for CallMe<F> {
fn drop(&mut self) {
(self.0.take().unwrap())();
}
}

fn goodbye() {
println!("goodbye");
}

let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>;
let x_layout = Layout::for_value(&*x);
let y = yeet_principal(x);
let y_layout = Layout::for_value(&*y);
assert_eq!(x_layout, y_layout);
println!("before");
drop(y);

let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
let x_layout = Layout::for_value(&*x);
let y = yeet_principal_2(x);
let y_layout = Layout::for_value(&*y);
assert_eq!(x_layout, y_layout);
println!("before");
drop(y);
}
4 changes: 4 additions & 0 deletions src/tools/miri/tests/pass/dyn-upcast.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
before
goodbye
before
goodbye
8 changes: 7 additions & 1 deletion tests/ui/traits/dyn-drop-principal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ run-pass
//@ check-run-results

use std::any::Any;
use std::{alloc::Layout, any::Any};

const fn yeet_principal(x: Box<dyn Any + Send>) -> Box<dyn Send> {
x
Expand Down Expand Up @@ -35,12 +35,18 @@ fn goodbye() {

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

let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
let x_layout = Layout::for_value(&*x);
let y = yeet_principal_2(x);
let y_layout = Layout::for_value(&*y);
assert_eq!(x_layout, y_layout);
println!("before");
drop(y);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/traits/dyn-star-drop-principal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(dyn_star)]
#![allow(incomplete_features)]

trait Trait {}
impl Trait for usize {}

fn main() {
// We allow &dyn Trait + Send -> &dyn Send (i.e. dropping principal),
// but we don't (currently?) allow the same for dyn*
let x: dyn* Trait + Send = 1usize;
x as dyn* Send; //~ error: `dyn* Trait + Send` needs to have the same ABI as a pointer
}
11 changes: 11 additions & 0 deletions tests/ui/traits/dyn-star-drop-principal.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0277]: `dyn* Trait + Send` needs to have the same ABI as a pointer
--> $DIR/dyn-star-drop-principal.rs:11:5
|
LL | x as dyn* Send;
| ^ `dyn* Trait + Send` needs to be a pointer-like type
|
= help: the trait `PointerLike` is not implemented for `dyn* Trait + Send`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit c4bce0b

Please sign in to comment.