summaryrefslogtreecommitdiffstats
path: root/tests/ui/traits/wf-object
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/traits/wf-object')
-rw-r--r--tests/ui/traits/wf-object/maybe-bound.rs18
-rw-r--r--tests/ui/traits/wf-object/maybe-bound.stderr32
-rw-r--r--tests/ui/traits/wf-object/no-duplicates.rs33
-rw-r--r--tests/ui/traits/wf-object/no-duplicates.stderr58
-rw-r--r--tests/ui/traits/wf-object/only-maybe-bound.rs7
-rw-r--r--tests/ui/traits/wf-object/only-maybe-bound.stderr15
-rw-r--r--tests/ui/traits/wf-object/reverse-order.rs15
7 files changed, 178 insertions, 0 deletions
diff --git a/tests/ui/traits/wf-object/maybe-bound.rs b/tests/ui/traits/wf-object/maybe-bound.rs
new file mode 100644
index 000000000..17771e976
--- /dev/null
+++ b/tests/ui/traits/wf-object/maybe-bound.rs
@@ -0,0 +1,18 @@
+// Test that `dyn ... + ?Sized + ...` is okay (though `?Sized` has no effect in trait objects).
+
+trait Foo {}
+
+type _0 = dyn ?Sized + Foo;
+//~^ ERROR `?Trait` is not permitted in trait object types
+
+type _1 = dyn Foo + ?Sized;
+//~^ ERROR `?Trait` is not permitted in trait object types
+
+type _2 = dyn Foo + ?Sized + ?Sized;
+//~^ ERROR `?Trait` is not permitted in trait object types
+//~| ERROR `?Trait` is not permitted in trait object types
+
+type _3 = dyn ?Sized + Foo;
+//~^ ERROR `?Trait` is not permitted in trait object types
+
+fn main() {}
diff --git a/tests/ui/traits/wf-object/maybe-bound.stderr b/tests/ui/traits/wf-object/maybe-bound.stderr
new file mode 100644
index 000000000..2fe3f0fc3
--- /dev/null
+++ b/tests/ui/traits/wf-object/maybe-bound.stderr
@@ -0,0 +1,32 @@
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/maybe-bound.rs:5:15
+ |
+LL | type _0 = dyn ?Sized + Foo;
+ | ^^^^^^
+
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/maybe-bound.rs:8:21
+ |
+LL | type _1 = dyn Foo + ?Sized;
+ | ^^^^^^
+
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/maybe-bound.rs:11:21
+ |
+LL | type _2 = dyn Foo + ?Sized + ?Sized;
+ | ^^^^^^
+
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/maybe-bound.rs:11:30
+ |
+LL | type _2 = dyn Foo + ?Sized + ?Sized;
+ | ^^^^^^
+
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/maybe-bound.rs:15:15
+ |
+LL | type _3 = dyn ?Sized + Foo;
+ | ^^^^^^
+
+error: aborting due to 5 previous errors
+
diff --git a/tests/ui/traits/wf-object/no-duplicates.rs b/tests/ui/traits/wf-object/no-duplicates.rs
new file mode 100644
index 000000000..678ede582
--- /dev/null
+++ b/tests/ui/traits/wf-object/no-duplicates.rs
@@ -0,0 +1,33 @@
+// The purpose of this test is to demonstrate that duplicating object safe traits
+// that are not auto-traits is rejected even though one could reasonably accept this.
+
+// Some arbitrary object-safe trait:
+trait Obj {}
+
+// Demonstrate that recursive expansion of trait aliases doesn't affect stable behavior:
+type _0 = dyn Obj + Obj;
+//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
+
+// Some variations:
+
+type _1 = dyn Send + Obj + Obj;
+//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
+
+type _2 = dyn Obj + Send + Obj;
+//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
+
+type _3 = dyn Obj + Send + Send; // But it is OK to duplicate auto traits.
+
+// Take higher ranked types into account.
+
+// Note that `'a` and `'b` are intentionally different to make sure we consider
+// them semantically the same.
+trait ObjL<'l> {}
+type _4 = dyn for<'a> ObjL<'a> + for<'b> ObjL<'b>;
+//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
+
+trait ObjT<T> {}
+type _5 = dyn ObjT<for<'a> fn(&'a u8)> + ObjT<for<'b> fn(&'b u8)>;
+//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
+
+fn main() {}
diff --git a/tests/ui/traits/wf-object/no-duplicates.stderr b/tests/ui/traits/wf-object/no-duplicates.stderr
new file mode 100644
index 000000000..50dfcf956
--- /dev/null
+++ b/tests/ui/traits/wf-object/no-duplicates.stderr
@@ -0,0 +1,58 @@
+error[E0225]: only auto traits can be used as additional traits in a trait object
+ --> $DIR/no-duplicates.rs:8:21
+ |
+LL | type _0 = dyn Obj + Obj;
+ | --- ^^^ additional non-auto trait
+ | |
+ | first non-auto trait
+ |
+ = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}`
+ = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
+
+error[E0225]: only auto traits can be used as additional traits in a trait object
+ --> $DIR/no-duplicates.rs:13:28
+ |
+LL | type _1 = dyn Send + Obj + Obj;
+ | --- ^^^ additional non-auto trait
+ | |
+ | first non-auto trait
+ |
+ = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}`
+ = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
+
+error[E0225]: only auto traits can be used as additional traits in a trait object
+ --> $DIR/no-duplicates.rs:16:28
+ |
+LL | type _2 = dyn Obj + Send + Obj;
+ | --- ^^^ additional non-auto trait
+ | |
+ | first non-auto trait
+ |
+ = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}`
+ = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
+
+error[E0225]: only auto traits can be used as additional traits in a trait object
+ --> $DIR/no-duplicates.rs:26:34
+ |
+LL | type _4 = dyn for<'a> ObjL<'a> + for<'b> ObjL<'b>;
+ | ---------------- ^^^^^^^^^^^^^^^^ additional non-auto trait
+ | |
+ | first non-auto trait
+ |
+ = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: for<'a> ObjL<'a> + for<'b> ObjL<'b> {}`
+ = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
+
+error[E0225]: only auto traits can be used as additional traits in a trait object
+ --> $DIR/no-duplicates.rs:30:42
+ |
+LL | type _5 = dyn ObjT<for<'a> fn(&'a u8)> + ObjT<for<'b> fn(&'b u8)>;
+ | ------------------------ ^^^^^^^^^^^^^^^^^^^^^^^^ additional non-auto trait
+ | |
+ | first non-auto trait
+ |
+ = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjT<for<'a> fn(&'a u8)> + ObjT<for<'b> fn(&'b u8)> {}`
+ = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
+
+error: aborting due to 5 previous errors
+
+For more information about this error, try `rustc --explain E0225`.
diff --git a/tests/ui/traits/wf-object/only-maybe-bound.rs b/tests/ui/traits/wf-object/only-maybe-bound.rs
new file mode 100644
index 000000000..3e6db3e99
--- /dev/null
+++ b/tests/ui/traits/wf-object/only-maybe-bound.rs
@@ -0,0 +1,7 @@
+// Test that `dyn ?Sized` (i.e., a trait object with only a maybe buond) is not allowed.
+
+type _0 = dyn ?Sized;
+//~^ ERROR at least one trait is required for an object type [E0224]
+//~| ERROR ?Trait` is not permitted in trait object types
+
+fn main() {}
diff --git a/tests/ui/traits/wf-object/only-maybe-bound.stderr b/tests/ui/traits/wf-object/only-maybe-bound.stderr
new file mode 100644
index 000000000..cbc41feec
--- /dev/null
+++ b/tests/ui/traits/wf-object/only-maybe-bound.stderr
@@ -0,0 +1,15 @@
+error: `?Trait` is not permitted in trait object types
+ --> $DIR/only-maybe-bound.rs:3:15
+ |
+LL | type _0 = dyn ?Sized;
+ | ^^^^^^
+
+error[E0224]: at least one trait is required for an object type
+ --> $DIR/only-maybe-bound.rs:3:11
+ |
+LL | type _0 = dyn ?Sized;
+ | ^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0224`.
diff --git a/tests/ui/traits/wf-object/reverse-order.rs b/tests/ui/traits/wf-object/reverse-order.rs
new file mode 100644
index 000000000..4f676cbe3
--- /dev/null
+++ b/tests/ui/traits/wf-object/reverse-order.rs
@@ -0,0 +1,15 @@
+// run-pass
+
+// Ensure that `dyn $($AutoTrait)+ ObjSafe` is well-formed.
+
+use std::marker::Unpin;
+
+// Some arbitrary object-safe trait:
+trait Obj {}
+
+type _0 = dyn Unpin;
+type _1 = dyn Send + Obj;
+type _2 = dyn Send + Unpin + Obj;
+type _3 = dyn Send + Unpin + Sync + Obj;
+
+fn main() {}