about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlcnr <rust@lcnr.de>2024-03-14 17:45:13 +0100
committerlcnr <rust@lcnr.de>2024-03-14 17:45:13 +0100
commitc8f0f17ed2182f6eca05e112e6ec7d2ceeebb8f5 (patch)
tree2734fb1a51a1ae7e0f4561825214744b9a828bb9
parent323069fd592c37f6663a41f4304bf99b3ccd3ecd (diff)
downloadrust-c8f0f17ed2182f6eca05e112e6ec7d2ceeebb8f5.tar.gz
rust-c8f0f17ed2182f6eca05e112e6ec7d2ceeebb8f5.zip
add tests
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.current.stderr22
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.next.stderr14
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.rs19
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.current.stderr22
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.next.stderr14
-rw-r--r--tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.rs37
6 files changed, 128 insertions, 0 deletions
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.current.stderr b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.current.stderr
new file mode 100644
index 00000000000..098ab71e946
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.current.stderr
@@ -0,0 +1,22 @@
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ok.rs:17:5
+   |
+LL |     x
+   |     ^ one type is more general than the other
+   |
+   = note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
+              found existential trait ref `for<'a> Supertrait<'a, 'a>`
+
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ok.rs:17:5
+   |
+LL |     x
+   |     ^ one type is more general than the other
+   |
+   = note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
+              found existential trait ref `for<'a> Supertrait<'a, 'a>`
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.next.stderr b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.next.stderr
new file mode 100644
index 00000000000..ac516fd6975
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.next.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ok.rs:17:5
+   |
+LL | fn ok(x: &dyn for<'a, 'b> Subtrait<'a, 'b>) -> &dyn for<'a> Supertrait<'a, 'a> {
+   |                                                ------------------------------- expected `&dyn for<'a> Supertrait<'a, 'a>` because of return type
+LL |     x
+   |     ^ expected trait `Supertrait`, found trait `Subtrait`
+   |
+   = note: expected reference `&dyn for<'a> Supertrait<'a, 'a>`
+              found reference `&dyn for<'a, 'b> Subtrait<'a, 'b>`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.rs b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.rs
new file mode 100644
index 00000000000..00743203179
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.rs
@@ -0,0 +1,19 @@
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+
+// We should be able to instantiate a binder during trait upcasting.
+// This test could be `check-pass`, but we should make sure that we
+// do so in both trait solvers.
+#![feature(trait_upcasting)]
+#![crate_type = "rlib"]
+trait Supertrait<'a, 'b> {}
+
+trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {}
+
+impl<'a> Supertrait<'a, 'a> for () {}
+impl<'a> Subtrait<'a, 'a> for () {}
+fn ok(x: &dyn for<'a, 'b> Subtrait<'a, 'b>) -> &dyn for<'a> Supertrait<'a, 'a> {
+    x //~ ERROR mismatched types
+    //[current]~^ ERROR mismatched types
+}
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.current.stderr b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.current.stderr
new file mode 100644
index 00000000000..bac82983268
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.current.stderr
@@ -0,0 +1,22 @@
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ub.rs:22:5
+   |
+LL |     x
+   |     ^ one type is more general than the other
+   |
+   = note: expected existential trait ref `for<'a> Supertrait<'a, 'a>`
+              found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
+
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ub.rs:22:5
+   |
+LL |     x
+   |     ^ one type is more general than the other
+   |
+   = note: expected existential trait ref `for<'a> Supertrait<'a, 'a>`
+              found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.next.stderr b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.next.stderr
new file mode 100644
index 00000000000..b82f1eef42b
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.next.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/higher-ranked-upcasting-ub.rs:22:5
+   |
+LL | fn unsound(x: &dyn for<'a> Subtrait<'a, 'a>) -> &dyn for<'a, 'b> Supertrait<'a, 'b> {
+   |                                                 ----------------------------------- expected `&dyn for<'a, 'b> Supertrait<'a, 'b>` because of return type
+LL |     x
+   |     ^ expected trait `Supertrait`, found trait `Subtrait`
+   |
+   = note: expected reference `&dyn for<'a, 'b> Supertrait<'a, 'b>`
+              found reference `&dyn for<'a> Subtrait<'a, 'a>`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.rs b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.rs
new file mode 100644
index 00000000000..2cf6fc75e77
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.rs
@@ -0,0 +1,37 @@
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+
+// We previously wrongly instantiated binders during trait upcasting,
+// allowing the super trait to be more generic than the sub trait.
+// This was unsound.
+#![feature(trait_upcasting)]
+trait Supertrait<'a, 'b> {
+    fn cast(&self, x: &'a str) -> &'b str;
+}
+
+trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {}
+
+impl<'a> Supertrait<'a, 'a> for () {
+    fn cast(&self, x: &'a str) -> &'a str {
+        x
+    }
+}
+impl<'a> Subtrait<'a, 'a> for () {}
+fn unsound(x: &dyn for<'a> Subtrait<'a, 'a>) -> &dyn for<'a, 'b> Supertrait<'a, 'b> {
+    x //~ ERROR mismatched types
+    //[current]~^ ERROR mismatched types
+}
+
+fn transmute<'a, 'b>(x: &'a str) -> &'b str {
+    unsound(&()).cast(x)
+}
+
+fn main() {
+    let x;
+    {
+        let mut temp = String::from("hello there");
+        x = transmute(temp.as_str());
+    }
+    println!("{x}");
+}