summaryrefslogtreecommitdiffstats
path: root/tests/ui/traits/trait-upcasting
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /tests/ui/traits/trait-upcasting
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/traits/trait-upcasting')
-rw-r--r--tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr1
-rw-r--r--tests/ui/traits/trait-upcasting/fewer-associated.rs25
-rw-r--r--tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.current.stderr14
-rw-r--r--tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.next.stderr14
-rw-r--r--tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.rs23
-rw-r--r--tests/ui/traits/trait-upcasting/issue-11515.current.stderr13
-rw-r--r--tests/ui/traits/trait-upcasting/issue-11515.next.stderr13
-rw-r--r--tests/ui/traits/trait-upcasting/issue-11515.rs11
-rw-r--r--tests/ui/traits/trait-upcasting/normalization.rs20
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr9
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-1.next.stderr9
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-1.rs4
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-1.stderr23
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-2.rs2
-rw-r--r--tests/ui/traits/trait-upcasting/type-checking-test-2.stderr37
-rw-r--r--tests/ui/traits/trait-upcasting/upcast-through-struct-tail.current.stderr13
-rw-r--r--tests/ui/traits/trait-upcasting/upcast-through-struct-tail.next.stderr13
-rw-r--r--tests/ui/traits/trait-upcasting/upcast-through-struct-tail.rs14
18 files changed, 200 insertions, 58 deletions
diff --git a/tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr b/tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr
index ca98e1831..62c732fb1 100644
--- a/tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr
+++ b/tests/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr
@@ -10,6 +10,7 @@ note: cycle used when collecting item types in top-level module
|
LL | trait A: B + A {}
| ^^^^^^^^^^^^^^^^^
+ = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
error: aborting due to previous error
diff --git a/tests/ui/traits/trait-upcasting/fewer-associated.rs b/tests/ui/traits/trait-upcasting/fewer-associated.rs
new file mode 100644
index 000000000..8228eea26
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/fewer-associated.rs
@@ -0,0 +1,25 @@
+// check-pass
+// issue: 114035
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
+#![feature(trait_upcasting)]
+
+trait A: B {
+ type Assoc;
+}
+
+trait B {}
+
+fn upcast(a: &dyn A<Assoc = i32>) -> &dyn B {
+ a
+}
+
+// Make sure that we can drop the existential projection `A::Assoc = i32`
+// when upcasting `dyn A<Assoc = i32>` to `dyn B`. Before, we used some
+// complicated algorithm which required rebuilding a new object type with
+// different bounds in order to test that an upcast was valid, but this
+// didn't allow upcasting to t that have fewer associated types
+// than the source type.
+
+fn main() {}
diff --git a/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.current.stderr b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.current.stderr
new file mode 100644
index 000000000..59c9d5737
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.current.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+ --> $DIR/illegal-upcast-from-impl.rs:16:66
+ |
+LL | fn illegal(x: &dyn Sub<Assoc = ()>) -> &dyn Super<Assoc = i32> { x }
+ | ----------------------- ^ expected trait `Super`, found trait `Sub`
+ | |
+ | expected `&dyn Super<Assoc = i32>` because of return type
+ |
+ = note: expected reference `&dyn Super<Assoc = i32>`
+ found reference `&dyn Sub<Assoc = ()>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.next.stderr b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.next.stderr
new file mode 100644
index 000000000..59c9d5737
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.next.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+ --> $DIR/illegal-upcast-from-impl.rs:16:66
+ |
+LL | fn illegal(x: &dyn Sub<Assoc = ()>) -> &dyn Super<Assoc = i32> { x }
+ | ----------------------- ^ expected trait `Super`, found trait `Sub`
+ | |
+ | expected `&dyn Super<Assoc = i32>` because of return type
+ |
+ = note: expected reference `&dyn Super<Assoc = i32>`
+ found reference `&dyn Sub<Assoc = ()>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.rs b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.rs
new file mode 100644
index 000000000..774474281
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/illegal-upcast-from-impl.rs
@@ -0,0 +1,23 @@
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
+#![feature(trait_upcasting)]
+
+trait Super {
+ type Assoc;
+}
+
+trait Sub: Super {}
+
+impl<T: ?Sized> Super for T {
+ type Assoc = i32;
+}
+
+fn illegal(x: &dyn Sub<Assoc = ()>) -> &dyn Super<Assoc = i32> { x }
+//~^ ERROR mismatched types
+
+// Want to make sure that we can't "upcast" to a supertrait that has a different
+// associated type that is instead provided by a blanket impl (and doesn't come
+// from the object bounds).
+
+fn main() {}
diff --git a/tests/ui/traits/trait-upcasting/issue-11515.current.stderr b/tests/ui/traits/trait-upcasting/issue-11515.current.stderr
new file mode 100644
index 000000000..97d66cccb
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/issue-11515.current.stderr
@@ -0,0 +1,13 @@
+error[E0658]: cannot cast `dyn Fn()` to `dyn FnMut()`, trait upcasting coercion is experimental
+ --> $DIR/issue-11515.rs:10:38
+ |
+LL | let test = Box::new(Test { func: closure });
+ | ^^^^^^^
+ |
+ = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
+ = help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
+ = note: required when coercing `Box<(dyn Fn() + 'static)>` into `Box<(dyn FnMut() + 'static)>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/traits/trait-upcasting/issue-11515.next.stderr b/tests/ui/traits/trait-upcasting/issue-11515.next.stderr
new file mode 100644
index 000000000..97d66cccb
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/issue-11515.next.stderr
@@ -0,0 +1,13 @@
+error[E0658]: cannot cast `dyn Fn()` to `dyn FnMut()`, trait upcasting coercion is experimental
+ --> $DIR/issue-11515.rs:10:38
+ |
+LL | let test = Box::new(Test { func: closure });
+ | ^^^^^^^
+ |
+ = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
+ = help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
+ = note: required when coercing `Box<(dyn Fn() + 'static)>` into `Box<(dyn FnMut() + 'static)>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/traits/trait-upcasting/issue-11515.rs b/tests/ui/traits/trait-upcasting/issue-11515.rs
new file mode 100644
index 000000000..723f3a24f
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/issue-11515.rs
@@ -0,0 +1,11 @@
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
+struct Test {
+ func: Box<dyn FnMut() + 'static>,
+}
+
+fn main() {
+ let closure: Box<dyn Fn() + 'static> = Box::new(|| ());
+ let test = Box::new(Test { func: closure }); //~ ERROR trait upcasting coercion is experimental [E0658]
+}
diff --git a/tests/ui/traits/trait-upcasting/normalization.rs b/tests/ui/traits/trait-upcasting/normalization.rs
new file mode 100644
index 000000000..c78338b0d
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/normalization.rs
@@ -0,0 +1,20 @@
+// check-pass
+// issue: 114113
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
+#![feature(trait_upcasting)]
+
+trait Mirror {
+ type Assoc;
+}
+impl<T> Mirror for T {
+ type Assoc = T;
+}
+
+trait Bar<T> {}
+trait Foo<T>: Bar<<T as Mirror>::Assoc> {}
+
+fn upcast<T>(x: &dyn Foo<T>) -> &dyn Bar<T> { x }
+
+fn main() {}
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr b/tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr
new file mode 100644
index 000000000..b612005fc
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr
@@ -0,0 +1,9 @@
+error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
+ --> $DIR/type-checking-test-1.rs:19:13
+ |
+LL | let _ = x as &dyn Bar<_>; // Ambiguous
+ | ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0605`.
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-1.next.stderr b/tests/ui/traits/trait-upcasting/type-checking-test-1.next.stderr
new file mode 100644
index 000000000..b612005fc
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/type-checking-test-1.next.stderr
@@ -0,0 +1,9 @@
+error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
+ --> $DIR/type-checking-test-1.rs:19:13
+ |
+LL | let _ = x as &dyn Bar<_>; // Ambiguous
+ | ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0605`.
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-1.rs b/tests/ui/traits/trait-upcasting/type-checking-test-1.rs
index 6bc9f4a75..afea8521e 100644
--- a/tests/ui/traits/trait-upcasting/type-checking-test-1.rs
+++ b/tests/ui/traits/trait-upcasting/type-checking-test-1.rs
@@ -1,3 +1,6 @@
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
#![feature(trait_upcasting)]
trait Foo: Bar<i32> + Bar<u32> {}
@@ -15,7 +18,6 @@ fn test_specific(x: &dyn Foo) {
fn test_unknown_version(x: &dyn Foo) {
let _ = x as &dyn Bar<_>; // Ambiguous
//~^ ERROR non-primitive cast
- //~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
}
fn test_infer_version(x: &dyn Foo) {
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-1.stderr b/tests/ui/traits/trait-upcasting/type-checking-test-1.stderr
deleted file mode 100644
index 82b4e9bd7..000000000
--- a/tests/ui/traits/trait-upcasting/type-checking-test-1.stderr
+++ /dev/null
@@ -1,23 +0,0 @@
-error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
- --> $DIR/type-checking-test-1.rs:16:13
- |
-LL | let _ = x as &dyn Bar<_>; // Ambiguous
- | ^^^^^^^^^^^^^^^^ invalid cast
- |
-help: consider borrowing the value
- |
-LL | let _ = &x as &dyn Bar<_>; // Ambiguous
- | +
-
-error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied
- --> $DIR/type-checking-test-1.rs:16:13
- |
-LL | let _ = x as &dyn Bar<_>; // Ambiguous
- | ^ the trait `Bar<_>` is not implemented for `&dyn Foo`
- |
- = note: required for the cast from `&&dyn Foo` to `&dyn Bar<_>`
-
-error: aborting due to 2 previous errors
-
-Some errors have detailed explanations: E0277, E0605.
-For more information about an error, try `rustc --explain E0277`.
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-2.rs b/tests/ui/traits/trait-upcasting/type-checking-test-2.rs
index 36b11dffd..b024b2775 100644
--- a/tests/ui/traits/trait-upcasting/type-checking-test-2.rs
+++ b/tests/ui/traits/trait-upcasting/type-checking-test-2.rs
@@ -18,13 +18,11 @@ fn test_specific2(x: &dyn Foo<u32>) {
fn test_specific3(x: &dyn Foo<i32>) {
let _ = x as &dyn Bar<u32>; // Error
//~^ ERROR non-primitive cast
- //~^^ ERROR the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
}
fn test_infer_arg(x: &dyn Foo<u32>) {
let a = x as &dyn Bar<_>; // Ambiguous
//~^ ERROR non-primitive cast
- //~^^ ERROR the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
let _ = a.bar();
}
diff --git a/tests/ui/traits/trait-upcasting/type-checking-test-2.stderr b/tests/ui/traits/trait-upcasting/type-checking-test-2.stderr
index 856303ef4..3e59b9d33 100644
--- a/tests/ui/traits/trait-upcasting/type-checking-test-2.stderr
+++ b/tests/ui/traits/trait-upcasting/type-checking-test-2.stderr
@@ -2,41 +2,14 @@ error[E0605]: non-primitive cast: `&dyn Foo<i32>` as `&dyn Bar<u32>`
--> $DIR/type-checking-test-2.rs:19:13
|
LL | let _ = x as &dyn Bar<u32>; // Error
- | ^^^^^^^^^^^^^^^^^^ invalid cast
- |
-help: consider borrowing the value
- |
-LL | let _ = &x as &dyn Bar<u32>; // Error
- | +
-
-error[E0277]: the trait bound `&dyn Foo<i32>: Bar<u32>` is not satisfied
- --> $DIR/type-checking-test-2.rs:19:13
- |
-LL | let _ = x as &dyn Bar<u32>; // Error
- | ^ the trait `Bar<u32>` is not implemented for `&dyn Foo<i32>`
- |
- = note: required for the cast from `&&dyn Foo<i32>` to `&dyn Bar<u32>`
+ | ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
error[E0605]: non-primitive cast: `&dyn Foo<u32>` as `&dyn Bar<_>`
- --> $DIR/type-checking-test-2.rs:25:13
+ --> $DIR/type-checking-test-2.rs:24:13
|
LL | let a = x as &dyn Bar<_>; // Ambiguous
- | ^^^^^^^^^^^^^^^^ invalid cast
- |
-help: consider borrowing the value
- |
-LL | let a = &x as &dyn Bar<_>; // Ambiguous
- | +
-
-error[E0277]: the trait bound `&dyn Foo<u32>: Bar<_>` is not satisfied
- --> $DIR/type-checking-test-2.rs:25:13
- |
-LL | let a = x as &dyn Bar<_>; // Ambiguous
- | ^ the trait `Bar<_>` is not implemented for `&dyn Foo<u32>`
- |
- = note: required for the cast from `&&dyn Foo<u32>` to `&dyn Bar<_>`
+ | ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
-error: aborting due to 4 previous errors
+error: aborting due to 2 previous errors
-Some errors have detailed explanations: E0277, E0605.
-For more information about an error, try `rustc --explain E0277`.
+For more information about this error, try `rustc --explain E0605`.
diff --git a/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.current.stderr b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.current.stderr
new file mode 100644
index 000000000..9f0993d65
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.current.stderr
@@ -0,0 +1,13 @@
+error[E0658]: cannot cast `dyn A` to `dyn B`, trait upcasting coercion is experimental
+ --> $DIR/upcast-through-struct-tail.rs:10:5
+ |
+LL | x
+ | ^
+ |
+ = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
+ = help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
+ = note: required when coercing `Box<Wrapper<(dyn A + 'a)>>` into `Box<Wrapper<(dyn B + 'a)>>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.next.stderr b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.next.stderr
new file mode 100644
index 000000000..9f0993d65
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.next.stderr
@@ -0,0 +1,13 @@
+error[E0658]: cannot cast `dyn A` to `dyn B`, trait upcasting coercion is experimental
+ --> $DIR/upcast-through-struct-tail.rs:10:5
+ |
+LL | x
+ | ^
+ |
+ = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
+ = help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
+ = note: required when coercing `Box<Wrapper<(dyn A + 'a)>>` into `Box<Wrapper<(dyn B + 'a)>>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.rs b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.rs
new file mode 100644
index 000000000..42495f45f
--- /dev/null
+++ b/tests/ui/traits/trait-upcasting/upcast-through-struct-tail.rs
@@ -0,0 +1,14 @@
+// revisions: current next
+//[next] compile-flags: -Ztrait-solver=next
+
+struct Wrapper<T: ?Sized>(T);
+
+trait A: B {}
+trait B {}
+
+fn test<'a>(x: Box<Wrapper<dyn A + 'a>>) -> Box<Wrapper<dyn B + 'a>> {
+ x
+ //~^ ERROR cannot cast `dyn A` to `dyn B`, trait upcasting coercion is experimental
+}
+
+fn main() {}