Allow dropping dyn principal
This commit is contained in:
parent
3a85d3fa78
commit
e3800a1a04
10 changed files with 83 additions and 35 deletions
62
tests/ui/traits/dyn-drop-principal.rs
Normal file
62
tests/ui/traits/dyn-drop-principal.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
//@ run-pass
|
||||
//@ check-run-results
|
||||
|
||||
use std::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");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Any + Send>;
|
||||
let y = yeet_principal(x);
|
||||
println!("before");
|
||||
drop(y);
|
||||
|
||||
let x = Box::new(CallMe::new(goodbye)) as Box<dyn Bar>;
|
||||
let y = yeet_principal_2(x);
|
||||
println!("before");
|
||||
drop(y);
|
||||
}
|
||||
|
||||
// Test that upcast works in `const`
|
||||
|
||||
const fn yeet_principal_3(x: &(dyn Any + Send + Sync)) -> &(dyn Send + Sync) {
|
||||
x
|
||||
}
|
||||
|
||||
#[used]
|
||||
pub static FOO: &(dyn Send + Sync) = yeet_principal_3(&false);
|
||||
|
||||
const fn yeet_principal_4(x: &dyn Bar) -> &(dyn Send + Sync) {
|
||||
x
|
||||
}
|
||||
|
||||
#[used]
|
||||
pub static BAR: &(dyn Send + Sync) = yeet_principal_4(&false);
|
||||
4
tests/ui/traits/dyn-drop-principal.run.stdout
Normal file
4
tests/ui/traits/dyn-drop-principal.run.stdout
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
before
|
||||
goodbye
|
||||
before
|
||||
goodbye
|
||||
Loading…
Add table
Add a link
Reference in a new issue