about summary refs log tree commit diff
path: root/tests/ui/auto-traits
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/auto-traits')
-rw-r--r--tests/ui/auto-traits/auto-trait-phantom-data-bounds.rs30
-rw-r--r--tests/ui/auto-traits/auto-trait-phantom-data-bounds.stderr68
2 files changed, 98 insertions, 0 deletions
diff --git a/tests/ui/auto-traits/auto-trait-phantom-data-bounds.rs b/tests/ui/auto-traits/auto-trait-phantom-data-bounds.rs
new file mode 100644
index 00000000000..6d1c4c87fad
--- /dev/null
+++ b/tests/ui/auto-traits/auto-trait-phantom-data-bounds.rs
@@ -0,0 +1,30 @@
+//! Ensure that auto trait checks `T` when it encounters a `PhantomData<T>` field, instead of
+//! checking the `PhantomData<T>` type itself (which almost always implements an auto trait).
+
+#![feature(auto_traits)]
+
+use std::marker::PhantomData;
+
+unsafe auto trait Zen {}
+
+unsafe impl<'a, T: 'a> Zen for &'a T where T: Sync {}
+
+struct Guard<'a, T: 'a> {
+    _marker: PhantomData<&'a T>,
+}
+
+struct Nested<T>(T);
+
+fn is_zen<T: Zen>(_: T) {}
+
+fn not_sync<T>(x: Guard<T>) {
+    is_zen(x)
+    //~^ ERROR `T` cannot be shared between threads safely [E0277]
+}
+
+fn nested_not_sync<T>(x: Nested<Guard<T>>) {
+    is_zen(x)
+    //~^ ERROR `T` cannot be shared between threads safely [E0277]
+}
+
+fn main() {}
diff --git a/tests/ui/auto-traits/auto-trait-phantom-data-bounds.stderr b/tests/ui/auto-traits/auto-trait-phantom-data-bounds.stderr
new file mode 100644
index 00000000000..56c2e8ff257
--- /dev/null
+++ b/tests/ui/auto-traits/auto-trait-phantom-data-bounds.stderr
@@ -0,0 +1,68 @@
+error[E0277]: `T` cannot be shared between threads safely
+  --> $DIR/auto-trait-phantom-data-bounds.rs:21:12
+   |
+LL |     is_zen(x)
+   |     ------ ^ `T` cannot be shared between threads safely
+   |     |
+   |     required by a bound introduced by this call
+   |
+note: required for `&T` to implement `Zen`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:10:24
+   |
+LL | unsafe impl<'a, T: 'a> Zen for &'a T where T: Sync {}
+   |                        ^^^     ^^^^^          ---- unsatisfied trait bound introduced here
+note: required because it appears within the type `PhantomData<&T>`
+  --> $SRC_DIR/core/src/marker.rs:LL:COL
+note: required because it appears within the type `Guard<'_, T>`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:12:8
+   |
+LL | struct Guard<'a, T: 'a> {
+   |        ^^^^^
+note: required by a bound in `is_zen`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:18:14
+   |
+LL | fn is_zen<T: Zen>(_: T) {}
+   |              ^^^ required by this bound in `is_zen`
+help: consider restricting type parameter `T` with trait `Sync`
+   |
+LL | fn not_sync<T: std::marker::Sync>(x: Guard<T>) {
+   |              +++++++++++++++++++
+
+error[E0277]: `T` cannot be shared between threads safely
+  --> $DIR/auto-trait-phantom-data-bounds.rs:26:12
+   |
+LL |     is_zen(x)
+   |     ------ ^ `T` cannot be shared between threads safely
+   |     |
+   |     required by a bound introduced by this call
+   |
+note: required for `&T` to implement `Zen`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:10:24
+   |
+LL | unsafe impl<'a, T: 'a> Zen for &'a T where T: Sync {}
+   |                        ^^^     ^^^^^          ---- unsatisfied trait bound introduced here
+note: required because it appears within the type `PhantomData<&T>`
+  --> $SRC_DIR/core/src/marker.rs:LL:COL
+note: required because it appears within the type `Guard<'_, T>`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:12:8
+   |
+LL | struct Guard<'a, T: 'a> {
+   |        ^^^^^
+note: required because it appears within the type `Nested<Guard<'_, T>>`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:16:8
+   |
+LL | struct Nested<T>(T);
+   |        ^^^^^^
+note: required by a bound in `is_zen`
+  --> $DIR/auto-trait-phantom-data-bounds.rs:18:14
+   |
+LL | fn is_zen<T: Zen>(_: T) {}
+   |              ^^^ required by this bound in `is_zen`
+help: consider restricting type parameter `T` with trait `Sync`
+   |
+LL | fn nested_not_sync<T: std::marker::Sync>(x: Nested<Guard<T>>) {
+   |                     +++++++++++++++++++
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.