about summary refs log tree commit diff
path: root/tests/ui/traits
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/traits')
-rw-r--r--tests/ui/traits/dyn-drop-principal.rs62
-rw-r--r--tests/ui/traits/dyn-drop-principal.run.stdout4
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/ui/traits/dyn-drop-principal.rs b/tests/ui/traits/dyn-drop-principal.rs
new file mode 100644
index 00000000000..eeff0550fcb
--- /dev/null
+++ b/tests/ui/traits/dyn-drop-principal.rs
@@ -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);
diff --git a/tests/ui/traits/dyn-drop-principal.run.stdout b/tests/ui/traits/dyn-drop-principal.run.stdout
new file mode 100644
index 00000000000..edd99a114a1
--- /dev/null
+++ b/tests/ui/traits/dyn-drop-principal.run.stdout
@@ -0,0 +1,4 @@
+before
+goodbye
+before
+goodbye