From c4bce0b8b13f2ae39bfd01e55bf98a556b2d703f Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Wed, 10 Jul 2024 13:00:36 -0400 Subject: [PATCH] Add more tests --- src/tools/miri/tests/pass/dyn-upcast.rs | 51 +++++++++++++++++++ src/tools/miri/tests/pass/dyn-upcast.stdout | 4 ++ tests/ui/traits/dyn-drop-principal.rs | 8 ++- tests/ui/traits/dyn-star-drop-principal.rs | 12 +++++ .../ui/traits/dyn-star-drop-principal.stderr | 11 ++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/tools/miri/tests/pass/dyn-upcast.stdout create mode 100644 tests/ui/traits/dyn-star-drop-principal.rs create mode 100644 tests/ui/traits/dyn-star-drop-principal.stderr diff --git a/src/tools/miri/tests/pass/dyn-upcast.rs b/src/tools/miri/tests/pass/dyn-upcast.rs index ff995f38196..306e9ab9c67 100644 --- a/src/tools/miri/tests/pass/dyn-upcast.rs +++ b/src/tools/miri/tests/pass/dyn-upcast.rs @@ -9,6 +9,7 @@ fn main() { struct_(); replace_vptr(); vtable_nop_cast(); + drop_principal(); } fn vtable_nop_cast() { @@ -430,3 +431,53 @@ fn invoke_outer(c: &dyn C) { let s = S(42); invoke_outer(&s); } + +fn drop_principal() { + use std::{alloc::Layout, any::Any}; + + const fn yeet_principal(x: Box) -> Box { + x + } + + trait Bar: Send + Sync {} + + impl Bar for T {} + + const fn yeet_principal_2(x: Box) -> Box { + x + } + + struct CallMe(Option); + + impl CallMe { + fn new(f: F) -> Self { + CallMe(Some(f)) + } + } + + impl Drop for CallMe { + fn drop(&mut self) { + (self.0.take().unwrap())(); + } + } + + fn goodbye() { + println!("goodbye"); + } + + let x = Box::new(CallMe::new(goodbye)) as Box; + 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; + 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); +} diff --git a/src/tools/miri/tests/pass/dyn-upcast.stdout b/src/tools/miri/tests/pass/dyn-upcast.stdout new file mode 100644 index 00000000000..edd99a114a1 --- /dev/null +++ b/src/tools/miri/tests/pass/dyn-upcast.stdout @@ -0,0 +1,4 @@ +before +goodbye +before +goodbye diff --git a/tests/ui/traits/dyn-drop-principal.rs b/tests/ui/traits/dyn-drop-principal.rs index eeff0550fcb..c233127e43d 100644 --- a/tests/ui/traits/dyn-drop-principal.rs +++ b/tests/ui/traits/dyn-drop-principal.rs @@ -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) -> Box { x @@ -35,12 +35,18 @@ fn goodbye() { fn main() { let x = Box::new(CallMe::new(goodbye)) as Box; + 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; + 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); } diff --git a/tests/ui/traits/dyn-star-drop-principal.rs b/tests/ui/traits/dyn-star-drop-principal.rs new file mode 100644 index 00000000000..1ad99070339 --- /dev/null +++ b/tests/ui/traits/dyn-star-drop-principal.rs @@ -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 +} diff --git a/tests/ui/traits/dyn-star-drop-principal.stderr b/tests/ui/traits/dyn-star-drop-principal.stderr new file mode 100644 index 00000000000..721ae7e191e --- /dev/null +++ b/tests/ui/traits/dyn-star-drop-principal.stderr @@ -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`.