about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-05-24 14:17:54 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-06-19 08:28:31 +0000
commitcbadf786bc482baf467f601d03a074000c422914 (patch)
tree4164c88c9e771a72639f909c176fc6b80608d61e
parent4dcb70b8cf3a12bb3bf0ef38cd05e6ab3720402c (diff)
downloadrust-cbadf786bc482baf467f601d03a074000c422914.tar.gz
rust-cbadf786bc482baf467f601d03a074000c422914.zip
Add tests
-rw-r--r--tests/ui/impl-trait/trait_upcasting.rs26
-rw-r--r--tests/ui/impl-trait/trait_upcasting.stderr29
2 files changed, 55 insertions, 0 deletions
diff --git a/tests/ui/impl-trait/trait_upcasting.rs b/tests/ui/impl-trait/trait_upcasting.rs
new file mode 100644
index 00000000000..cb3c17e87b4
--- /dev/null
+++ b/tests/ui/impl-trait/trait_upcasting.rs
@@ -0,0 +1,26 @@
+//! Test that we allow unsizing `Trait<Concrete>` to `Trait<Opaque>` and vice versa
+
+trait Trait<T> {}
+
+impl<T, U> Trait<T> for U {}
+
+fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
+    if false {
+        let x = hello();
+        let _: &'static dyn Trait<()> = x;
+        //~^ ERROR: mismatched types
+    }
+    todo!()
+}
+
+fn bye() -> &'static dyn Trait<impl Sized> {
+    if false {
+        let mut x = bye();
+        let y: &'static (dyn Trait<()> + Send) = &();
+        x = y;
+        //~^ ERROR: mismatched types
+    }
+    todo!()
+}
+
+fn main() {}
diff --git a/tests/ui/impl-trait/trait_upcasting.stderr b/tests/ui/impl-trait/trait_upcasting.stderr
new file mode 100644
index 00000000000..2c1aa4bbf80
--- /dev/null
+++ b/tests/ui/impl-trait/trait_upcasting.stderr
@@ -0,0 +1,29 @@
+error[E0308]: mismatched types
+  --> $DIR/trait_upcasting.rs:10:41
+   |
+LL | fn hello() -> &'static (dyn Trait<impl Sized> + Send) {
+   |                                   ---------- the found opaque type
+...
+LL |         let _: &'static dyn Trait<()> = x;
+   |                ----------------------   ^ expected trait `Trait<()>`, found trait `Trait<impl Sized> + Send`
+   |                |
+   |                expected due to this
+   |
+   = note: expected reference `&'static (dyn Trait<()> + 'static)`
+              found reference `&dyn Trait<impl Sized> + Send`
+
+error[E0308]: mismatched types
+  --> $DIR/trait_upcasting.rs:20:13
+   |
+LL | fn bye() -> &'static dyn Trait<impl Sized> {
+   |                                ---------- the expected opaque type
+...
+LL |         x = y;
+   |             ^ expected trait `Trait<impl Sized>`, found trait `Trait<()> + Send`
+   |
+   = note: expected reference `&dyn Trait<impl Sized>`
+              found reference `&'static (dyn Trait<()> + Send + 'static)`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.